From 1b0a9c19a233f3338e13785da2888efc02801e1d Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 9 Nov 2021 07:59:07 +0100 Subject: [PATCH] lifecycle - emit `onWillKill` when `app.exit` --- .../electron-main/lifecycleMainService.ts | 16 +++++++++++++++- .../electron-main/storageMainService.test.ts | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts b/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts index 7f3b15150ac..bfe95f931ef 100644 --- a/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts +++ b/src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts @@ -91,6 +91,13 @@ export interface ILifecycleMainService { */ readonly onBeforeCloseWindow: Event; + /** + * An event that fires in the rare cases where `app.exit` is triggered + * and thus we Forcefully shutdown the application. No other lifecycle + * event handlers are triggered. + */ + readonly onWillKill: Event; + /** * Make a `ICodeWindow` known to the lifecycle main service. */ @@ -117,7 +124,8 @@ export interface ILifecycleMainService { quit(willRestart?: boolean): Promise; /** - * Forcefully shutdown the application. No livecycle event handlers are triggered. + * Forcefully shutdown the application. The only lifecycle event handler + * that is triggered is `onWillKill`. */ kill(code?: number): Promise; @@ -169,6 +177,9 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe private readonly _onBeforeUnloadWindow = this._register(new Emitter()); readonly onBeforeUnloadWindow = this._onBeforeUnloadWindow.event; + private readonly _onWillKill = this._register(new Emitter()); + readonly onWillKill = this._onWillKill.event; + private _quitRequested = false; get quitRequested(): boolean { return this._quitRequested; } @@ -589,6 +600,9 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe async kill(code?: number): Promise { this.logService.trace('Lifecycle#kill()'); + // Event + this._onWillKill.fire(); + // The kill() method is only used in 2 situations: // - when an instance fails to start at all // - when extension tests run from CLI to report proper exit code diff --git a/src/vs/platform/storage/test/electron-main/storageMainService.test.ts b/src/vs/platform/storage/test/electron-main/storageMainService.test.ts index 459ce917e13..110a60eb77e 100644 --- a/src/vs/platform/storage/test/electron-main/storageMainService.test.ts +++ b/src/vs/platform/storage/test/electron-main/storageMainService.test.ts @@ -57,6 +57,7 @@ suite('StorageMainService', function () { onWillLoadWindow = Event.None; onBeforeCloseWindow = Event.None; onBeforeUnloadWindow = Event.None; + onWillKill = Event.None; wasRestarted = false; quitRequested = false;