From a87342ef0563c4d1c41dca3bb36d67ad2c096d90 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 18 Nov 2016 07:35:11 +0100 Subject: [PATCH] debt - more real events over eventemitter --- src/vs/code/electron-main/lifecycle.ts | 35 +++++-------------- .../test/electron-main/servicesTestUtils.ts | 15 ++++---- .../backup/electron-main/backupMainService.ts | 4 +-- .../lifecycle/common/mainLifecycle.ts | 16 +++++++-- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/vs/code/electron-main/lifecycle.ts b/src/vs/code/electron-main/lifecycle.ts index 6b3d5c43032..26ba280a546 100644 --- a/src/vs/code/electron-main/lifecycle.ts +++ b/src/vs/code/electron-main/lifecycle.ts @@ -5,7 +5,6 @@ 'use strict'; -import { EventEmitter } from 'events'; import { ipcMain as ipc, app } from 'electron'; import { TPromise, TValueCallback } from 'vs/base/common/winjs.base'; import { ReadyState, IVSCodeWindow } from 'vs/code/common/window'; @@ -13,11 +12,7 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment' import { ILogService } from 'vs/code/electron-main/log'; import { IStorageService } from 'vs/code/electron-main/storage'; import { ILifecycleMainService } from 'vs/platform/lifecycle/common/mainLifecycle'; - -const EventTypes = { - BEFORE_CLOSE: 'before-close', - BEFORE_QUIT: 'before-quit' -}; +import Event, { Emitter } from 'vs/base/common/event'; export class LifecycleService implements ILifecycleMainService { @@ -25,7 +20,6 @@ export class LifecycleService implements ILifecycleMainService { private static QUIT_FROM_UPDATE_MARKER = 'quit.from.update'; // use a marker to find out if an update was applied in the previous session - private eventEmitter = new EventEmitter(); private windowToCloseRequest: { [windowId: string]: boolean }; private quitRequested: boolean; private pendingQuitPromise: TPromise; @@ -33,6 +27,12 @@ export class LifecycleService implements ILifecycleMainService { private oneTimeListenerTokenGenerator: number; private _wasUpdated: boolean; + private _onBeforeUnload = new Emitter(); + onBeforeUnload: Event = this._onBeforeUnload.event; + + private _onBeforeQuit = new Emitter(); + onBeforeQuit: Event = this._onBeforeQuit.event; + constructor( @IEnvironmentService private environmentService: IEnvironmentService, @ILogService private logService: ILogService, @@ -58,23 +58,6 @@ export class LifecycleService implements ILifecycleMainService { return this._wasUpdated; } - /** - * Due to the way we handle lifecycle with eventing, the general app.on('before-quit') - * event cannot be used because it can be called twice on shutdown. Instead the onBeforeQuit - * handler in this module can be used and it is only called once on a shutdown sequence. - */ - onBeforeQuit(clb: () => void): () => void { - this.eventEmitter.addListener(EventTypes.BEFORE_QUIT, clb); - - return () => this.eventEmitter.removeListener(EventTypes.BEFORE_QUIT, clb); - } - - onAfterUnload(clb: (vscodeWindow: IVSCodeWindow) => void): () => void { - this.eventEmitter.addListener(EventTypes.BEFORE_CLOSE, clb); - - return () => this.eventEmitter.removeListener(EventTypes.BEFORE_CLOSE, clb); - } - public ready(): void { this.registerListeners(); } @@ -86,7 +69,7 @@ export class LifecycleService implements ILifecycleMainService { this.logService.log('Lifecycle#before-quit'); if (!this.quitRequested) { - this.eventEmitter.emit(EventTypes.BEFORE_QUIT); // only send this if this is the first quit request we have + this._onBeforeQuit.fire(); // only send this if this is the first quit request we have } this.quitRequested = true; @@ -150,7 +133,7 @@ export class LifecycleService implements ILifecycleMainService { const oneTimeCancelEvent = 'vscode:cancel' + oneTimeEventToken; ipc.once(oneTimeOkEvent, () => { - this.eventEmitter.emit(EventTypes.BEFORE_CLOSE, vscodeWindow); + this._onBeforeUnload.fire(vscodeWindow); c(false); // no veto }); diff --git a/src/vs/code/test/electron-main/servicesTestUtils.ts b/src/vs/code/test/electron-main/servicesTestUtils.ts index 5eb62ad9f9a..80065c7c62a 100644 --- a/src/vs/code/test/electron-main/servicesTestUtils.ts +++ b/src/vs/code/test/electron-main/servicesTestUtils.ts @@ -6,22 +6,21 @@ import { ILifecycleMainService } from 'vs/platform/lifecycle/common/mainLifecycle'; import { IVSCodeWindow } from 'vs/code/common/window'; import { TPromise } from 'vs/base/common/winjs.base'; +import Event, { Emitter } from 'vs/base/common/event'; export class TestLifecycleService implements ILifecycleMainService { public _serviceBrand: any; + private _onBeforeUnload = new Emitter(); + onBeforeUnload: Event = this._onBeforeUnload.event; + + private _onBeforeQuit = new Emitter(); + onBeforeQuit: Event = this._onBeforeQuit.event; + public get wasUpdated(): boolean { return false; } - public onBeforeQuit(clb: () => void): () => void { - return () => { }; - } - - public onAfterUnload(clb: (vscodeWindow: IVSCodeWindow) => void): () => void { - return () => { }; - } - public ready(): void { } diff --git a/src/vs/platform/backup/electron-main/backupMainService.ts b/src/vs/platform/backup/electron-main/backupMainService.ts index 9094003b4af..b999dd8adce 100644 --- a/src/vs/platform/backup/electron-main/backupMainService.ts +++ b/src/vs/platform/backup/electron-main/backupMainService.ts @@ -29,12 +29,12 @@ export class BackupMainService implements IBackupMainService { this.backupHome = environmentService.backupHome; this.workspacesJsonPath = environmentService.backupWorkspacesPath; - lifecycleService.onAfterUnload(this.onAfterUnloadWindow.bind(this)); + lifecycleService.onBeforeUnload(this.onBeforeUnloadWindow.bind(this)); this.loadSync(); } - private onAfterUnloadWindow(vscodeWindow: VSCodeWindow) { + private onBeforeUnloadWindow(vscodeWindow: VSCodeWindow) { if (vscodeWindow.openedWorkspacePath) { // Clear out workspace from workspaces.json if it doesn't have any backups const workspaceResource = Uri.file(vscodeWindow.openedWorkspacePath); diff --git a/src/vs/platform/lifecycle/common/mainLifecycle.ts b/src/vs/platform/lifecycle/common/mainLifecycle.ts index cbe34ac0169..7d931021a13 100644 --- a/src/vs/platform/lifecycle/common/mainLifecycle.ts +++ b/src/vs/platform/lifecycle/common/mainLifecycle.ts @@ -7,6 +7,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { IVSCodeWindow } from 'vs/code/common/window'; import { TPromise } from 'vs/base/common/winjs.base'; +import Event from 'vs/base/common/event'; export const ILifecycleMainService = createDecorator('lifecycleMainService'); @@ -18,8 +19,19 @@ export interface ILifecycleMainService { */ wasUpdated: boolean; - onBeforeQuit(clb: () => void): () => void; - onAfterUnload(clb: (vscodeWindow: IVSCodeWindow) => void): () => void; + /** + * Fired before the window unloads. This can either happen as a matter of closing the + * window or when the window is being reloaded. + */ + onBeforeUnload: Event; + + /** + * Due to the way we handle lifecycle with eventing, the general app.on('before-quit') + * event cannot be used because it can be called twice on shutdown. Instead the onBeforeQuit + * handler in this module can be used and it is only called once on a shutdown sequence. + */ + onBeforeQuit: Event; + ready(): void; registerWindow(vscodeWindow: IVSCodeWindow): void; unload(vscodeWindow: IVSCodeWindow): TPromise;