From 718f9450d6c7860b4516726725116af3e4a536dd Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Sat, 3 Jul 2021 03:38:41 +0100 Subject: [PATCH] Main: Remove 'openedWindows' variable Electron already has a built-in tracker for all opened windows, so it makes no sense to have custom code to handle it. One should always use what is given to you :^) --- src/main/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 6f2df386..fa59908a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -33,7 +33,6 @@ function runApp() { const isDev = process.env.NODE_ENV === 'development' const isDebug = process.argv.includes('--debug') let mainWindow - let openedWindows = [] let startupUrl // CORS somehow gets re-enabled in Electron v9.0.4 @@ -198,7 +197,7 @@ function runApp() { }, show: false }) - openedWindows.push(newWindow) + if (replaceMainWindow) { mainWindow = newWindow } @@ -253,13 +252,12 @@ function runApp() { newWindow.focus() }) - newWindow.on('closed', () => { - // Remove closed window - openedWindows = openedWindows.filter((window) => window !== newWindow) - if (newWindow === mainWindow) { + newWindow.once('closed', () => { + const allWindows = BrowserWindow.getAllWindows() + if (allWindows.length !== 0 && newWindow === mainWindow) { // Replace mainWindow to avoid accessing `mainWindow.webContents` // Which raises "Object has been destroyed" error - mainWindow = openedWindows[0] + mainWindow = allWindows[0] } console.log('closed') @@ -368,9 +366,11 @@ function runApp() { }) ipcMain.on('syncWindows', (event, payload) => { - const otherWindows = openedWindows.filter((window) => { - return window.webContents.id !== event.sender.id - }) + const otherWindows = BrowserWindow.getAllWindows().filter( + (window) => { + return window.webContents.id !== event.sender.id + } + ) for (const window of otherWindows) { window.webContents.send('syncWindows', payload)