From 503b1c51d2ae18517c1d78d6b08605579d6aad76 Mon Sep 17 00:00:00 2001 From: Eugene Olonov Date: Fri, 10 Mar 2023 14:23:59 -0800 Subject: [PATCH] Proper app teardown --- Composer/packages/electron-server/src/main.ts | 39 ++++++++++--------- extensions/localPublish/src/index.ts | 2 + 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Composer/packages/electron-server/src/main.ts b/Composer/packages/electron-server/src/main.ts index a3338f5b8a..d44e286519 100644 --- a/Composer/packages/electron-server/src/main.ts +++ b/Composer/packages/electron-server/src/main.ts @@ -209,9 +209,27 @@ async function main(show = false) { ); } - mainWindow.on('closed', () => { - ElectronWindow.destroy(); - }); + const quitApp = async () => { + const allWindowsClosed = new Promise((resolve) => app.once('window-all-closed', resolve)); + const webContentsDestroyed = new Promise((resolve) => + mainWindow.webContents.once('destroyed', () => { + ElectronWindow.destroy(); + resolve(); + }) + ); + + mainWindow.webContents.send('session-update', 'session-ended'); + mainWindow.webContents.send('cleanup'); + + await Promise.all([webContentsDestroyed, allWindowsClosed]); + // preserve app icon in the dock on MacOS + if (isMac()) return; + + process.emit('beforeExit', 0); + app.quit(); + }; + + mainWindow.on('close', quitApp); log('Rendered application.'); } } @@ -308,21 +326,6 @@ async function run() { mainWindow?.webContents.send('machine-info', { id: machineId, os: os.platform() }); }); - // Quit when all windows are closed. - app.on('window-all-closed', () => { - // On OS X it is common for applications and their menu bar - // to stay active until the user quits explicitly with Cmd + Q - if (!isMac()) { - app.quit(); - } - }); - - app.on('before-quit', () => { - const mainWindow = ElectronWindow.getInstance().browserWindow; - mainWindow?.webContents.send('session-update', 'session-ended'); - mainWindow?.webContents.send('cleanup'); - }); - app.on('activate', () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. diff --git a/extensions/localPublish/src/index.ts b/extensions/localPublish/src/index.ts index a5a3c55b57..a3b858cc6c 100644 --- a/extensions/localPublish/src/index.ts +++ b/extensions/localPublish/src/index.ts @@ -486,3 +486,5 @@ const cleanup = () => { (['SIGINT', 'SIGTERM', 'SIGQUIT'] as NodeJS.Signals[]).forEach((signal: NodeJS.Signals) => { process.on(signal, cleanup.bind(null, signal)); }); + +process.on('beforeExit', cleanup);