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 :^)
This commit is contained in:
Svallinn 2021-07-03 03:38:41 +01:00
parent 40d7278383
commit 718f9450d6
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 10 additions and 10 deletions

View File

@ -33,7 +33,6 @@ function runApp() {
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug') const isDebug = process.argv.includes('--debug')
let mainWindow let mainWindow
let openedWindows = []
let startupUrl let startupUrl
// CORS somehow gets re-enabled in Electron v9.0.4 // CORS somehow gets re-enabled in Electron v9.0.4
@ -198,7 +197,7 @@ function runApp() {
}, },
show: false show: false
}) })
openedWindows.push(newWindow)
if (replaceMainWindow) { if (replaceMainWindow) {
mainWindow = newWindow mainWindow = newWindow
} }
@ -253,13 +252,12 @@ function runApp() {
newWindow.focus() newWindow.focus()
}) })
newWindow.on('closed', () => { newWindow.once('closed', () => {
// Remove closed window const allWindows = BrowserWindow.getAllWindows()
openedWindows = openedWindows.filter((window) => window !== newWindow) if (allWindows.length !== 0 && newWindow === mainWindow) {
if (newWindow === mainWindow) {
// Replace mainWindow to avoid accessing `mainWindow.webContents` // Replace mainWindow to avoid accessing `mainWindow.webContents`
// Which raises "Object has been destroyed" error // Which raises "Object has been destroyed" error
mainWindow = openedWindows[0] mainWindow = allWindows[0]
} }
console.log('closed') console.log('closed')
@ -368,9 +366,11 @@ function runApp() {
}) })
ipcMain.on('syncWindows', (event, payload) => { ipcMain.on('syncWindows', (event, payload) => {
const otherWindows = openedWindows.filter((window) => { const otherWindows = BrowserWindow.getAllWindows().filter(
return window.webContents.id !== event.sender.id (window) => {
}) return window.webContents.id !== event.sender.id
}
)
for (const window of otherWindows) { for (const window of otherWindows) {
window.webContents.send('syncWindows', payload) window.webContents.send('syncWindows', payload)