Merge pull request #2237 from MartinFournier/fix/electron-handlers

Fix: Use global references for electron event handlers
This commit is contained in:
hydroflame 2022-01-02 11:42:15 -05:00 committed by GitHub
commit 29aca108fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 21 deletions

@ -9,6 +9,7 @@ const api = require("./api-server");
const debug = process.argv.includes("--debug"); const debug = process.argv.includes("--debug");
async function createWindow(killall) { async function createWindow(killall) {
const setStopProcessHandler = global.app_handlers.stopProcess
const window = new BrowserWindow({ const window = new BrowserWindow({
show: false, show: false,
backgroundThrottling: false, backgroundThrottling: false,
@ -45,7 +46,7 @@ async function createWindow(killall) {
} }
menu.refreshMenu(window); menu.refreshMenu(window);
utils.setStopProcessHandler(app, window, true); setStopProcessHandler(app, window, true);
return window; return window;
} }

@ -6,7 +6,6 @@ const greenworks = require("./greenworks");
const api = require("./api-server"); const api = require("./api-server");
const gameWindow = require("./gameWindow"); const gameWindow = require("./gameWindow");
const achievements = require("./achievements"); const achievements = require("./achievements");
const utils = require("./utils");
log.catchErrors(); log.catchErrors();
log.info(`Started app: ${JSON.stringify(process.argv)}`); log.info(`Started app: ${JSON.stringify(process.argv)}`);
@ -48,7 +47,7 @@ function setStopProcessHandler(app, window, enabled) {
if (!canRunJS) { if (!canRunJS) {
// We're stuck, let's crash the process // We're stuck, let's crash the process
log.warn('Forcefully crashing the renderer process'); log.warn('Forcefully crashing the renderer process');
gameWindow.webContents.forcefullyCrashRenderer(); window.webContents.forcefullyCrashRenderer();
} }
log.debug('Destroying the window'); log.debug('Destroying the window');
@ -84,10 +83,12 @@ function startWindow(noScript) {
gameWindow.createWindow(noScript); gameWindow.createWindow(noScript);
} }
utils.initialize(setStopProcessHandler, startWindow); global.app_handlers = {
stopProcess: setStopProcessHandler,
createWindow: startWindow,
}
app.whenReady().then(async () => { app.whenReady().then(() => {
log.info('Application is ready!'); log.info('Application is ready!');
utils.initialize(setStopProcessHandler, startWindow); startWindow(process.argv.includes("--no-scripts"));
startWindow(process.argv.includes("--no-scripts"))
}); });

@ -5,19 +5,10 @@ const log = require("electron-log");
const achievements = require("./achievements"); const achievements = require("./achievements");
const api = require("./api-server"); const api = require("./api-server");
let setStopProcessHandler = () => {
// Will be overwritten by the initialize function called in main
}
let createWindowHandler = () => {
// Will be overwritten by the initialize function called in main
}
function initialize(stopHandler, createHandler) {
setStopProcessHandler = stopHandler;
createWindowHandler = createHandler
}
function reloadAndKill(window, killScripts) { function reloadAndKill(window, killScripts) {
const setStopProcessHandler = global.app_handlers.stopProcess
const createWindowHandler = global.app_handlers.createWindow;
log.info('Reloading & Killing all scripts...'); log.info('Reloading & Killing all scripts...');
setStopProcessHandler(app, window, false); setStopProcessHandler(app, window, false);
@ -72,7 +63,6 @@ function showErrorBox(title, error) {
} }
module.exports = { module.exports = {
initialize, setStopProcessHandler, reloadAndKill, showErrorBox, reloadAndKill, showErrorBox,
attachUnresponsiveAppHandler, detachUnresponsiveAppHandler, attachUnresponsiveAppHandler, detachUnresponsiveAppHandler,
} }