From dc21a5b87bced9a8eb2ba5553fdc5fef4c367156 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Thu, 16 Dec 2021 18:26:20 -0500 Subject: [PATCH 1/2] Attempt to fix processes that stay up after closing app --- electron/main.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/electron/main.js b/electron/main.js index 622676967..f155f1d54 100644 --- a/electron/main.js +++ b/electron/main.js @@ -9,12 +9,22 @@ if (greenworks.init()) { const debug = false; +let win; +let intervalID; + function createWindow(killall) { - const win = new BrowserWindow({ + win = new BrowserWindow({ show: false, backgroundThrottling: false, }); + win.on('closed', function() { + clearInterval(intervalID); + win = null; + app.quit(); + process.exit(0); + }); + win.removeMenu(); win.maximize(); noScripts = killall ? { query: { noScripts: killall } } : {}; @@ -37,7 +47,7 @@ function createWindow(killall) { // This is backward but the game fills in an array called `document.achievements` and we retrieve it from // here. Hey if it works it works. const achievements = greenworks.getAchievementNames(); - const intervalID = setInterval(async () => { + intervalID = setInterval(async () => { const achs = await win.webContents.executeJavaScript("document.achievements"); console.log(achs); for (const ach of achs) { @@ -111,6 +121,17 @@ function createWindow(killall) { ); } +app.on('window-all-closed', function () { + // On OS X it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') { + clearInterval(intervalID); + win = null; + app.quit() + process.exit(0); + } +}); + app.whenReady().then(() => { createWindow(false); }); From 63f7775804dd24ce45530e509c73b1974f72250b Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Thu, 16 Dec 2021 20:21:25 -0500 Subject: [PATCH 2/2] Remove process handler when doing a hard reload --- electron/main.js | 52 ++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/electron/main.js b/electron/main.js index f155f1d54..7cd6c619f 100644 --- a/electron/main.js +++ b/electron/main.js @@ -9,22 +9,12 @@ if (greenworks.init()) { const debug = false; -let win; -let intervalID; - function createWindow(killall) { - win = new BrowserWindow({ + const win = new BrowserWindow({ show: false, backgroundThrottling: false, }); - win.on('closed', function() { - clearInterval(intervalID); - win = null; - app.quit(); - process.exit(0); - }); - win.removeMenu(); win.maximize(); noScripts = killall ? { query: { noScripts: killall } } : {}; @@ -47,7 +37,7 @@ function createWindow(killall) { // This is backward but the game fills in an array called `document.achievements` and we retrieve it from // here. Hey if it works it works. const achievements = greenworks.getAchievementNames(); - intervalID = setInterval(async () => { + const intervalID = setInterval(async () => { const achs = await win.webContents.executeJavaScript("document.achievements"); console.log(achs); for (const ach of achs) { @@ -55,6 +45,7 @@ function createWindow(killall) { greenworks.activateAchievement(ach, () => undefined); } }, 1000); + win.achievementsIntervalID = intervalID; // Create the Application's main menu Menu.setApplicationMenu( @@ -84,6 +75,7 @@ function createWindow(killall) { { label: "reload & kill all scripts", click: () => { + setStopProcessHandler(app, win, false); if (intervalID) clearInterval(intervalID); win.webContents.forcefullyCrashRenderer(); win.close(); @@ -119,19 +111,35 @@ function createWindow(killall) { }, ]), ); + + return win; } -app.on('window-all-closed', function () { - // On OS X it is common for applications and their menu bar - // to stay active until the user quits explicitly with Cmd + Q - if (process.platform !== 'darwin') { - clearInterval(intervalID); - win = null; - app.quit() - process.exit(0); +function setStopProcessHandler(app, window, enabled) { + const clearWindowHandler = () => { + if (window.achievementsIntervalID) { + clearInterval(window.achievementsIntervalID); + } + window = null; } -}); + + const stopProcessHandler = () => { + if (process.platform !== 'darwin') { + app.quit() + process.exit(0); + } + } + + if (enabled) { + window.on('closed', clearWindowHandler); + app.on('window-all-closed', stopProcessHandler); + } else { + window.removeListener('closed', clearWindowHandler); + app.removeListener('window-all-closed', stopProcessHandler); + } +} app.whenReady().then(() => { - createWindow(false); + const win = createWindow(false); + setStopProcessHandler(app, win, true); });