Skip to content

Commit

Permalink
Proper app teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
OEvgeny committed Mar 10, 2023
1 parent 22aef38 commit 503b1c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 21 additions & 18 deletions Composer/packages/electron-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,27 @@ async function main(show = false) {
);
}

mainWindow.on('closed', () => {
ElectronWindow.destroy();
});
const quitApp = async () => {
const allWindowsClosed = new Promise<void>((resolve) => app.once('window-all-closed', resolve));
const webContentsDestroyed = new Promise<void>((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.');
}
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions extensions/localPublish/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 503b1c5

Please sign in to comment.