From 3cdca85a7d8dab1d463f7485c350aedeb0d4288b Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 5 Apr 2017 10:56:55 +0200 Subject: [PATCH] towards better quit handling (for #22138) --- src/vs/code/electron-main/lifecycle.ts | 25 +++++++++++++++++++++---- src/vs/code/electron-main/main.ts | 10 +++++----- src/vs/code/electron-main/windows.ts | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/vs/code/electron-main/lifecycle.ts b/src/vs/code/electron-main/lifecycle.ts index 41cb87053a1..7623a917086 100644 --- a/src/vs/code/electron-main/lifecycle.ts +++ b/src/vs/code/electron-main/lifecycle.ts @@ -47,10 +47,15 @@ export interface ILifecycleService { ready(): void; registerWindow(vscodeWindow: VSCodeWindow): void; + unload(vscodeWindow: VSCodeWindow, reason: UnloadReason): TPromise; + + relaunch(options?: { addArgs?: string[], removeArgs?: string[] }); + quit(fromUpdate?: boolean): TPromise; - relaunch(options: { addArgs?: string[], removeArgs?: string[] }); isQuitRequested(): boolean; + + kill(code?: number); } export class LifecycleService implements ILifecycleService { @@ -210,6 +215,7 @@ export class LifecycleService implements ILifecycleService { if (fromUpdate) { this.storageService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true); } + this.pendingQuitPromiseComplete(false /* no veto */); this.pendingQuitPromiseComplete = null; this.pendingQuitPromise = null; @@ -223,6 +229,10 @@ export class LifecycleService implements ILifecycleService { return this.pendingQuitPromise; } + public kill(code?: number): void { + app.exit(code); + } + public relaunch(options?: { addArgs?: string[], removeArgs?: string[] }): void { const args = process.argv.slice(1); if (options && options.addArgs) { @@ -238,10 +248,17 @@ export class LifecycleService implements ILifecycleService { } } - this.storageService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true); + let vetod = false; + app.once('quit', () => { + if (!vetod) { + this.storageService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true); + app.relaunch({ args }); + } + }); - app.quit(); - app.once('quit', () => app.relaunch({ args })); + this.quit().then(veto => { + vetod = veto; + }); } public isQuitRequested(): boolean { diff --git a/src/vs/code/electron-main/main.ts b/src/vs/code/electron-main/main.ts index cc7f81aad57..90b77b0a742 100644 --- a/src/vs/code/electron-main/main.ts +++ b/src/vs/code/electron-main/main.ts @@ -58,7 +58,6 @@ import product from 'vs/platform/node/product'; import pkg from 'vs/platform/node/package'; import * as fs from 'original-fs'; - ipc.on('vscode:fetchShellEnv', (event, windowId) => { const win = BrowserWindow.fromId(windowId); getShellEnvironment().then(shellEnv => { @@ -71,6 +70,7 @@ ipc.on('vscode:fetchShellEnv', (event, windowId) => { function quit(accessor: ServicesAccessor, errorOrMessage?: Error | string): void { const logService = accessor.get(ILogService); + const lifecycleService = accessor.get(ILifecycleService); let exitCode = 0; if (typeof errorOrMessage === 'string') { @@ -84,7 +84,7 @@ function quit(accessor: ServicesAccessor, errorOrMessage?: Error | string): void } } - process.exit(exitCode); // in main, process.exit === app.exit + lifecycleService.kill(exitCode); } // TODO@Joao wow this is huge, clean up! @@ -247,7 +247,7 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: platfo logService.log('IPC#vscode:exit', code); dispose(); - process.exit(code); // in main, process.exit === app.exit + lifecycleService.kill(code); }); // Lifecycle @@ -397,13 +397,13 @@ function start(): void { args = validatePaths(args); } catch (err) { console.error(err.message); - process.exit(1); + app.exit(1); + return; } const instantiationService = createServices(args); - return instantiationService.invokeFunction(accessor => { const environmentService = accessor.get(IEnvironmentService); const instanceEnv: typeof process.env = { diff --git a/src/vs/code/electron-main/windows.ts b/src/vs/code/electron-main/windows.ts index 02719389cda..e3b36c8f9fa 100644 --- a/src/vs/code/electron-main/windows.ts +++ b/src/vs/code/electron-main/windows.ts @@ -1318,7 +1318,7 @@ export class WindowsManager implements IWindowsMainService { // Otherwise: normal quit else { setTimeout(() => { - app.quit(); + this.lifecycleService.quit(); }, 10 /* delay to unwind callback stack (IPC) */); } } -- GitLab