Remove process handler when doing a hard reload

This commit is contained in:
Martin Fournier 2021-12-16 20:21:25 -05:00
parent dc21a5b87b
commit 63f7775804

@ -9,22 +9,12 @@ if (greenworks.init()) {
const debug = false; const debug = false;
let win;
let intervalID;
function createWindow(killall) { function createWindow(killall) {
win = new BrowserWindow({ const win = new BrowserWindow({
show: false, show: false,
backgroundThrottling: false, backgroundThrottling: false,
}); });
win.on('closed', function() {
clearInterval(intervalID);
win = null;
app.quit();
process.exit(0);
});
win.removeMenu(); win.removeMenu();
win.maximize(); win.maximize();
noScripts = killall ? { query: { noScripts: killall } } : {}; 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 // 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. // here. Hey if it works it works.
const achievements = greenworks.getAchievementNames(); const achievements = greenworks.getAchievementNames();
intervalID = setInterval(async () => { const intervalID = setInterval(async () => {
const achs = await win.webContents.executeJavaScript("document.achievements"); const achs = await win.webContents.executeJavaScript("document.achievements");
console.log(achs); console.log(achs);
for (const ach of achs) { for (const ach of achs) {
@ -55,6 +45,7 @@ function createWindow(killall) {
greenworks.activateAchievement(ach, () => undefined); greenworks.activateAchievement(ach, () => undefined);
} }
}, 1000); }, 1000);
win.achievementsIntervalID = intervalID;
// Create the Application's main menu // Create the Application's main menu
Menu.setApplicationMenu( Menu.setApplicationMenu(
@ -84,6 +75,7 @@ function createWindow(killall) {
{ {
label: "reload & kill all scripts", label: "reload & kill all scripts",
click: () => { click: () => {
setStopProcessHandler(app, win, false);
if (intervalID) clearInterval(intervalID); if (intervalID) clearInterval(intervalID);
win.webContents.forcefullyCrashRenderer(); win.webContents.forcefullyCrashRenderer();
win.close(); win.close();
@ -119,19 +111,35 @@ function createWindow(killall) {
}, },
]), ]),
); );
return win;
} }
app.on('window-all-closed', function () { function setStopProcessHandler(app, window, enabled) {
// On OS X it is common for applications and their menu bar const clearWindowHandler = () => {
// to stay active until the user quits explicitly with Cmd + Q if (window.achievementsIntervalID) {
clearInterval(window.achievementsIntervalID);
}
window = null;
}
const stopProcessHandler = () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
clearInterval(intervalID);
win = null;
app.quit() app.quit()
process.exit(0); 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(() => { app.whenReady().then(() => {
createWindow(false); const win = createWindow(false);
setStopProcessHandler(app, win, true);
}); });