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 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)