fix(electron): cleanup child windows by instance instead of name

This commit is contained in:
kuaifan 2025-12-04 11:18:47 +00:00
parent 01c7f7250b
commit ee9b6248bb

12
electron/electron.js vendored
View File

@ -497,6 +497,14 @@ function createChildWindow(args) {
const wind = childWindow.find(item => item.name == name); const wind = childWindow.find(item => item.name == name);
let browser = wind ? wind.browser : null; let browser = wind ? wind.browser : null;
let isPreload = false; let isPreload = false;
// 清理已销毁但仍被引用的窗口,避免对失效对象调用方法
if (browser && browser.isDestroyed && browser.isDestroyed()) {
const index = childWindow.findIndex(item => item.name == name);
if (index > -1) {
childWindow.splice(index, 1);
}
browser = null;
}
if (browser) { if (browser) {
browser.focus(); browser.focus();
if (args.force === false) { if (args.force === false) {
@ -533,7 +541,7 @@ function createChildWindow(args) {
options.parent = mainWindow options.parent = mainWindow
} }
if (preloadWindow && Object.keys(webPreferences).length === 0) { if (preloadWindow && !preloadWindow.isDestroyed?.() && Object.keys(webPreferences).length === 0) {
// 使用预加载窗口 // 使用预加载窗口
browser = preloadWindow; browser = preloadWindow;
preloadWindow = null; preloadWindow = null;
@ -579,7 +587,7 @@ function createChildWindow(args) {
}) })
browser.on('closed', () => { browser.on('closed', () => {
const index = childWindow.findIndex(item => item.name == name); const index = childWindow.findIndex(item => item.browser === browser);
if (index > -1) { if (index > -1) {
childWindow.splice(index, 1) childWindow.splice(index, 1)
} }