mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge from master
This commit is contained in:
@@ -2,36 +2,59 @@
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { isThenable } from 'vs/base/common/async';
|
||||
|
||||
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
|
||||
|
||||
/**
|
||||
* An event that is send out when the window is about to close. Clients have a chance to veto the closing by either calling veto
|
||||
* with a boolean "true" directly or with a promise that resolves to a boolean. Returning a promise is useful
|
||||
* in cases of long running operations on shutdown.
|
||||
* An event that is send out when the window is about to close. Clients have a chance to veto
|
||||
* the closing by either calling veto with a boolean "true" directly or with a promise that
|
||||
* resolves to a boolean. Returning a promise is useful in cases of long running operations
|
||||
* on shutdown.
|
||||
*
|
||||
* Note: It is absolutely important to avoid long running promises on this call. Please try hard to return
|
||||
* a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
|
||||
* Note: It is absolutely important to avoid long running promises if possible. Please try hard
|
||||
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
|
||||
*/
|
||||
export interface ShutdownEvent {
|
||||
export interface BeforeShutdownEvent {
|
||||
|
||||
/**
|
||||
* Allows to veto the shutdown. The veto can be a long running operation.
|
||||
* Allows to veto the shutdown. The veto can be a long running operation but it
|
||||
* will block the application from closing.
|
||||
*/
|
||||
veto(value: boolean | TPromise<boolean>): void;
|
||||
veto(value: boolean | Thenable<boolean>): void;
|
||||
|
||||
/**
|
||||
* The reason why Code is shutting down.
|
||||
* The reason why the application will be shutting down.
|
||||
*/
|
||||
reason: ShutdownReason;
|
||||
}
|
||||
|
||||
export enum ShutdownReason {
|
||||
/**
|
||||
* An event that is send out when the window closes. Clients have a chance to join the closing
|
||||
* by providing a promise from the join method. Returning a promise is useful in cases of long
|
||||
* running operations on shutdown.
|
||||
*
|
||||
* Note: It is absolutely important to avoid long running promises if possible. Please try hard
|
||||
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
|
||||
*/
|
||||
export interface WillShutdownEvent {
|
||||
|
||||
/**
|
||||
* Allows to join the shutdown. The promise can be a long running operation but it
|
||||
* will block the application from closing.
|
||||
*/
|
||||
join(promise: Thenable<void>): void;
|
||||
|
||||
/**
|
||||
* The reason why the application is shutting down.
|
||||
*/
|
||||
reason: ShutdownReason;
|
||||
}
|
||||
|
||||
export const enum ShutdownReason {
|
||||
|
||||
/** Window is closed */
|
||||
CLOSE = 1,
|
||||
@@ -46,19 +69,54 @@ export enum ShutdownReason {
|
||||
LOAD = 4
|
||||
}
|
||||
|
||||
export enum StartupKind {
|
||||
export const enum StartupKind {
|
||||
NewWindow = 1,
|
||||
ReloadedWindow = 3,
|
||||
ReopenedWindow = 4,
|
||||
}
|
||||
|
||||
export enum LifecyclePhase {
|
||||
export function StartupKindToString(startupKind: StartupKind): string {
|
||||
switch (startupKind) {
|
||||
case StartupKind.NewWindow: return 'NewWindow';
|
||||
case StartupKind.ReloadedWindow: return 'ReloadedWindow';
|
||||
case StartupKind.ReopenedWindow: return 'ReopenedWindow';
|
||||
}
|
||||
}
|
||||
|
||||
export const enum LifecyclePhase {
|
||||
|
||||
/**
|
||||
* The first phase signals that we are about to startup getting ready.
|
||||
*/
|
||||
Starting = 1,
|
||||
Restoring = 2,
|
||||
Running = 3,
|
||||
|
||||
/**
|
||||
* Services are ready and the view is about to restore its state.
|
||||
*/
|
||||
Ready = 2,
|
||||
|
||||
/**
|
||||
* Views, panels and editors have restored. For editors this means, that
|
||||
* they show their contents fully.
|
||||
*/
|
||||
Restored = 3,
|
||||
|
||||
/**
|
||||
* The last phase after views, panels and editors have restored and
|
||||
* some time has passed (few seconds).
|
||||
*/
|
||||
Eventually = 4
|
||||
}
|
||||
|
||||
export function LifecyclePhaseToString(phase: LifecyclePhase) {
|
||||
switch (phase) {
|
||||
case LifecyclePhase.Starting: return 'Starting';
|
||||
case LifecyclePhase.Ready: return 'Ready';
|
||||
case LifecyclePhase.Restored: return 'Restored';
|
||||
case LifecyclePhase.Eventually: return 'Eventually';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A lifecycle service informs about lifecycle events of the
|
||||
* application, such as shutdown.
|
||||
@@ -77,53 +135,63 @@ export interface ILifecycleService {
|
||||
*/
|
||||
readonly phase: LifecyclePhase;
|
||||
|
||||
/**
|
||||
* Fired before shutdown happens. Allows listeners to veto against the
|
||||
* shutdown to prevent it from happening.
|
||||
*
|
||||
* The event carries a shutdown reason that indicates how the shutdown was triggered.
|
||||
*/
|
||||
readonly onBeforeShutdown: Event<BeforeShutdownEvent>;
|
||||
|
||||
/**
|
||||
* Fired when no client is preventing the shutdown from happening (from onBeforeShutdown).
|
||||
* Can be used to save UI state even if that is long running through the WillShutdownEvent#join()
|
||||
* method.
|
||||
*
|
||||
* The event carries a shutdown reason that indicates how the shutdown was triggered.
|
||||
*/
|
||||
readonly onWillShutdown: Event<WillShutdownEvent>;
|
||||
|
||||
/**
|
||||
* Fired when the shutdown is about to happen after long running shutdown operations
|
||||
* have finished (from onWillShutdown). This is the right place to dispose resources.
|
||||
*/
|
||||
readonly onShutdown: Event<void>;
|
||||
|
||||
/**
|
||||
* Returns a promise that resolves when a certain lifecycle phase
|
||||
* has started.
|
||||
*/
|
||||
when(phase: LifecyclePhase): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Fired before shutdown happens. Allows listeners to veto against the
|
||||
* shutdown.
|
||||
*/
|
||||
readonly onWillShutdown: Event<ShutdownEvent>;
|
||||
|
||||
/**
|
||||
* Fired when no client is preventing the shutdown from happening. Can be used to dispose heavy resources
|
||||
* like running processes. Can also be used to save UI state to storage.
|
||||
*
|
||||
* The event carries a shutdown reason that indicates how the shutdown was triggered.
|
||||
*/
|
||||
readonly onShutdown: Event<ShutdownReason>;
|
||||
}
|
||||
|
||||
export const NullLifecycleService: ILifecycleService = {
|
||||
_serviceBrand: null,
|
||||
phase: LifecyclePhase.Running,
|
||||
when() { return Promise.resolve(); },
|
||||
startupKind: StartupKind.NewWindow,
|
||||
onBeforeShutdown: Event.None,
|
||||
onWillShutdown: Event.None,
|
||||
onShutdown: Event.None
|
||||
onShutdown: Event.None,
|
||||
phase: LifecyclePhase.Restored,
|
||||
startupKind: StartupKind.NewWindow,
|
||||
when() { return Promise.resolve(); }
|
||||
};
|
||||
|
||||
// Shared veto handling across main and renderer
|
||||
export function handleVetos(vetos: (boolean | TPromise<boolean>)[], onError: (error: Error) => void): TPromise<boolean /* veto */> {
|
||||
export function handleVetos(vetos: (boolean | Thenable<boolean>)[], onError: (error: Error) => void): Promise<boolean /* veto */> {
|
||||
if (vetos.length === 0) {
|
||||
return TPromise.as(false);
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
const promises: TPromise<void>[] = [];
|
||||
const promises: Thenable<void>[] = [];
|
||||
let lazyValue = false;
|
||||
|
||||
for (let valueOrPromise of vetos) {
|
||||
|
||||
// veto, done
|
||||
if (valueOrPromise === true) {
|
||||
return TPromise.as(true);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
if (TPromise.is(valueOrPromise)) {
|
||||
if (isThenable(valueOrPromise)) {
|
||||
promises.push(valueOrPromise.then(value => {
|
||||
if (value) {
|
||||
lazyValue = true; // veto, done
|
||||
@@ -135,5 +203,5 @@ export function handleVetos(vetos: (boolean | TPromise<boolean>)[], onError: (er
|
||||
}
|
||||
}
|
||||
|
||||
return TPromise.join(promises).then(() => lazyValue);
|
||||
return Promise.all(promises).then(() => lazyValue);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { toErrorMessage } from 'vs/base/common/errorMessage';
|
||||
import { ILifecycleService, ShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase, handleVetos } from 'vs/platform/lifecycle/common/lifecycle';
|
||||
import { ILifecycleService, BeforeShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase, handleVetos, LifecyclePhaseToString, WillShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle';
|
||||
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
|
||||
import { ipcRenderer as ipc } from 'electron';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
@@ -15,59 +13,82 @@ import { mark } from 'vs/base/common/performance';
|
||||
import { Barrier } from 'vs/base/common/async';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { INotificationService } from 'vs/platform/notification/common/notification';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { onUnexpectedError } from 'vs/base/common/errors';
|
||||
|
||||
export class LifecycleService implements ILifecycleService {
|
||||
export class LifecycleService extends Disposable implements ILifecycleService {
|
||||
|
||||
private static readonly _lastShutdownReasonKey = 'lifecyle.lastShutdownReason';
|
||||
private static readonly LAST_SHUTDOWN_REASON_KEY = 'lifecyle.lastShutdownReason';
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private readonly _onWillShutdown = new Emitter<ShutdownEvent>();
|
||||
private readonly _onShutdown = new Emitter<ShutdownReason>();
|
||||
private readonly _onBeforeShutdown = this._register(new Emitter<BeforeShutdownEvent>());
|
||||
get onBeforeShutdown(): Event<BeforeShutdownEvent> { return this._onBeforeShutdown.event; }
|
||||
|
||||
private readonly _onWillShutdown = this._register(new Emitter<WillShutdownEvent>());
|
||||
get onWillShutdown(): Event<WillShutdownEvent> { return this._onWillShutdown.event; }
|
||||
|
||||
private readonly _onShutdown = this._register(new Emitter<void>());
|
||||
get onShutdown(): Event<void> { return this._onShutdown.event; }
|
||||
|
||||
private readonly _startupKind: StartupKind;
|
||||
get startupKind(): StartupKind { return this._startupKind; }
|
||||
|
||||
private _phase: LifecyclePhase = LifecyclePhase.Starting;
|
||||
private _phaseWhen = new Map<LifecyclePhase, Barrier>();
|
||||
get phase(): LifecyclePhase { return this._phase; }
|
||||
|
||||
private phaseWhen = new Map<LifecyclePhase, Barrier>();
|
||||
|
||||
private shutdownReason: ShutdownReason;
|
||||
|
||||
constructor(
|
||||
@INotificationService private readonly _notificationService: INotificationService,
|
||||
@IWindowService private readonly _windowService: IWindowService,
|
||||
@IStorageService private readonly _storageService: IStorageService,
|
||||
@ILogService private readonly _logService: ILogService
|
||||
@INotificationService private notificationService: INotificationService,
|
||||
@IWindowService private windowService: IWindowService,
|
||||
@IStorageService private storageService: IStorageService,
|
||||
@ILogService private logService: ILogService
|
||||
) {
|
||||
const lastShutdownReason = this._storageService.getInteger(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
|
||||
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
|
||||
if (lastShutdownReason === ShutdownReason.RELOAD) {
|
||||
this._startupKind = StartupKind.ReloadedWindow;
|
||||
} else if (lastShutdownReason === ShutdownReason.LOAD) {
|
||||
this._startupKind = StartupKind.ReopenedWindow;
|
||||
} else {
|
||||
this._startupKind = StartupKind.NewWindow;
|
||||
}
|
||||
super();
|
||||
|
||||
this._logService.trace(`lifecycle: starting up (startup kind: ${this._startupKind})`);
|
||||
this._startupKind = this.resolveStartupKind();
|
||||
|
||||
this._registerListeners();
|
||||
this.registerListeners();
|
||||
}
|
||||
|
||||
private _registerListeners(): void {
|
||||
const windowId = this._windowService.getCurrentWindowId();
|
||||
private resolveStartupKind(): StartupKind {
|
||||
const lastShutdownReason = this.storageService.getInteger(LifecycleService.LAST_SHUTDOWN_REASON_KEY, StorageScope.WORKSPACE);
|
||||
this.storageService.remove(LifecycleService.LAST_SHUTDOWN_REASON_KEY, StorageScope.WORKSPACE);
|
||||
|
||||
let startupKind: StartupKind;
|
||||
if (lastShutdownReason === ShutdownReason.RELOAD) {
|
||||
startupKind = StartupKind.ReloadedWindow;
|
||||
} else if (lastShutdownReason === ShutdownReason.LOAD) {
|
||||
startupKind = StartupKind.ReopenedWindow;
|
||||
} else {
|
||||
startupKind = StartupKind.NewWindow;
|
||||
}
|
||||
|
||||
this.logService.trace(`lifecycle: starting up (startup kind: ${this._startupKind})`);
|
||||
|
||||
return startupKind;
|
||||
}
|
||||
|
||||
private registerListeners(): void {
|
||||
const windowId = this.windowService.getCurrentWindowId();
|
||||
|
||||
// Main side indicates that window is about to unload, check for vetos
|
||||
ipc.on('vscode:onBeforeUnload', (event, reply: { okChannel: string, cancelChannel: string, reason: ShutdownReason }) => {
|
||||
this._logService.trace(`lifecycle: onBeforeUnload (reason: ${reply.reason})`);
|
||||
this.logService.trace(`lifecycle: onBeforeUnload (reason: ${reply.reason})`);
|
||||
|
||||
// store shutdown reason to retrieve next startup
|
||||
this._storageService.store(LifecycleService._lastShutdownReasonKey, JSON.stringify(reply.reason), StorageScope.WORKSPACE);
|
||||
|
||||
// trigger onWillShutdown events and veto collecting
|
||||
this.onBeforeUnload(reply.reason).done(veto => {
|
||||
// trigger onBeforeShutdown events and veto collecting
|
||||
this.handleBeforeShutdown(reply.reason).then(veto => {
|
||||
if (veto) {
|
||||
this._logService.trace('lifecycle: onBeforeUnload prevented via veto');
|
||||
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
|
||||
this.logService.trace('lifecycle: onBeforeUnload prevented via veto');
|
||||
|
||||
ipc.send(reply.cancelChannel, windowId);
|
||||
} else {
|
||||
this._logService.trace('lifecycle: onBeforeUnload continues without veto');
|
||||
this.logService.trace('lifecycle: onBeforeUnload continues without veto');
|
||||
|
||||
this.shutdownReason = reply.reason;
|
||||
ipc.send(reply.okChannel, windowId);
|
||||
}
|
||||
});
|
||||
@@ -75,28 +96,57 @@ export class LifecycleService implements ILifecycleService {
|
||||
|
||||
// Main side indicates that we will indeed shutdown
|
||||
ipc.on('vscode:onWillUnload', (event, reply: { replyChannel: string, reason: ShutdownReason }) => {
|
||||
this._logService.trace(`lifecycle: onWillUnload (reason: ${reply.reason})`);
|
||||
this.logService.trace(`lifecycle: onWillUnload (reason: ${reply.reason})`);
|
||||
|
||||
this._onShutdown.fire(reply.reason);
|
||||
ipc.send(reply.replyChannel, windowId);
|
||||
// trigger onWillShutdown events and joining
|
||||
return this.handleWillShutdown(reply.reason).then(() => {
|
||||
|
||||
// trigger onShutdown event now that we know we will quit
|
||||
this._onShutdown.fire();
|
||||
|
||||
// acknowledge to main side
|
||||
ipc.send(reply.replyChannel, windowId);
|
||||
});
|
||||
});
|
||||
|
||||
// Save shutdown reason to retrieve on next startup
|
||||
this.storageService.onWillSaveState(() => {
|
||||
this.storageService.store(LifecycleService.LAST_SHUTDOWN_REASON_KEY, this.shutdownReason, StorageScope.WORKSPACE);
|
||||
});
|
||||
}
|
||||
|
||||
private onBeforeUnload(reason: ShutdownReason): TPromise<boolean> {
|
||||
const vetos: (boolean | TPromise<boolean>)[] = [];
|
||||
private handleBeforeShutdown(reason: ShutdownReason): Promise<boolean> {
|
||||
const vetos: (boolean | Thenable<boolean>)[] = [];
|
||||
|
||||
this._onWillShutdown.fire({
|
||||
this._onBeforeShutdown.fire({
|
||||
veto(value) {
|
||||
vetos.push(value);
|
||||
},
|
||||
reason
|
||||
});
|
||||
|
||||
return handleVetos(vetos, err => this._notificationService.error(toErrorMessage(err)));
|
||||
return handleVetos(vetos, err => {
|
||||
this.notificationService.error(toErrorMessage(err));
|
||||
onUnexpectedError(err);
|
||||
});
|
||||
}
|
||||
|
||||
get phase(): LifecyclePhase {
|
||||
return this._phase;
|
||||
private handleWillShutdown(reason: ShutdownReason): Thenable<void> {
|
||||
const joiners: Thenable<void>[] = [];
|
||||
|
||||
this._onWillShutdown.fire({
|
||||
join(promise) {
|
||||
if (promise) {
|
||||
joiners.push(promise);
|
||||
}
|
||||
},
|
||||
reason
|
||||
});
|
||||
|
||||
return Promise.all(joiners).then(() => void 0, err => {
|
||||
this.notificationService.error(toErrorMessage(err));
|
||||
onUnexpectedError(err);
|
||||
});
|
||||
}
|
||||
|
||||
set phase(value: LifecyclePhase) {
|
||||
@@ -108,14 +158,14 @@ export class LifecycleService implements ILifecycleService {
|
||||
return;
|
||||
}
|
||||
|
||||
this._logService.trace(`lifecycle: phase changed (value: ${value})`);
|
||||
this.logService.trace(`lifecycle: phase changed (value: ${value})`);
|
||||
|
||||
this._phase = value;
|
||||
mark(`LifecyclePhase/${LifecyclePhase[value]}`);
|
||||
mark(`LifecyclePhase/${LifecyclePhaseToString(value)}`);
|
||||
|
||||
if (this._phaseWhen.has(this._phase)) {
|
||||
this._phaseWhen.get(this._phase).open();
|
||||
this._phaseWhen.delete(this._phase);
|
||||
if (this.phaseWhen.has(this._phase)) {
|
||||
this.phaseWhen.get(this._phase).open();
|
||||
this.phaseWhen.delete(this._phase);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,24 +174,12 @@ export class LifecycleService implements ILifecycleService {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
let barrier = this._phaseWhen.get(phase);
|
||||
let barrier = this.phaseWhen.get(phase);
|
||||
if (!barrier) {
|
||||
barrier = new Barrier();
|
||||
this._phaseWhen.set(phase, barrier);
|
||||
this.phaseWhen.set(phase, barrier);
|
||||
}
|
||||
|
||||
return barrier.wait();
|
||||
}
|
||||
|
||||
get startupKind(): StartupKind {
|
||||
return this._startupKind;
|
||||
}
|
||||
|
||||
get onWillShutdown(): Event<ShutdownEvent> {
|
||||
return this._onWillShutdown.event;
|
||||
}
|
||||
|
||||
get onShutdown(): Event<ShutdownReason> {
|
||||
return this._onShutdown.event;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,22 +3,20 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ipcMain as ipc, app } from 'electron';
|
||||
import { TPromise, TValueCallback } from 'vs/base/common/winjs.base';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { IStateService } from 'vs/platform/state/common/state';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { ICodeWindow } from 'vs/platform/windows/electron-main/windows';
|
||||
import { ReadyState } from 'vs/platform/windows/common/windows';
|
||||
import { handleVetos } from 'vs/platform/lifecycle/common/lifecycle';
|
||||
import { isMacintosh, isWindows } from 'vs/base/common/platform';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { always } from 'vs/base/common/async';
|
||||
|
||||
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
|
||||
|
||||
export enum UnloadReason {
|
||||
export const enum UnloadReason {
|
||||
CLOSE = 1,
|
||||
QUIT = 2,
|
||||
RELOAD = 3,
|
||||
@@ -28,7 +26,16 @@ export enum UnloadReason {
|
||||
export interface IWindowUnloadEvent {
|
||||
window: ICodeWindow;
|
||||
reason: UnloadReason;
|
||||
veto(value: boolean | TPromise<boolean>): void;
|
||||
veto(value: boolean | Thenable<boolean>): void;
|
||||
}
|
||||
|
||||
export interface ShutdownEvent {
|
||||
|
||||
/**
|
||||
* Allows to join the shutdown. The promise can be a long running operation but it
|
||||
* will block the application from closing.
|
||||
*/
|
||||
join(promise: Thenable<void>): void;
|
||||
}
|
||||
|
||||
export interface ILifecycleService {
|
||||
@@ -42,12 +49,11 @@ export interface ILifecycleService {
|
||||
/**
|
||||
* Will be true if the program was requested to quit.
|
||||
*/
|
||||
isQuitRequested: boolean;
|
||||
quitRequested: boolean;
|
||||
|
||||
/**
|
||||
* 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 onBeforeShutdown
|
||||
* handler in this module can be used and it is only called once on a shutdown sequence.
|
||||
* An event that fires when the application is about to shutdown before any window is closed.
|
||||
* The shutdown can still be prevented by any window that vetos this event.
|
||||
*/
|
||||
onBeforeShutdown: Event<void>;
|
||||
|
||||
@@ -56,67 +62,79 @@ export interface ILifecycleService {
|
||||
* vetoed the shutdown sequence. At this point listeners are ensured that the application will
|
||||
* quit without veto.
|
||||
*/
|
||||
onShutdown: Event<void>;
|
||||
onWillShutdown: Event<ShutdownEvent>;
|
||||
|
||||
/**
|
||||
* We provide our own event when we close a window because the general window.on('close')
|
||||
* is called even when the window prevents the closing. We want an event that truly fires
|
||||
* before the window gets closed for real.
|
||||
* An event that fires before a window closes. This event is fired after any veto has been dealt
|
||||
* with so that listeners know for sure that the window will close without veto.
|
||||
*/
|
||||
onBeforeWindowClose: Event<ICodeWindow>;
|
||||
|
||||
/**
|
||||
* An even that can be vetoed to prevent a window from being unloaded.
|
||||
* An event that fires before a window is about to unload. Listeners can veto this event to prevent
|
||||
* the window from unloading.
|
||||
*/
|
||||
onBeforeWindowUnload: Event<IWindowUnloadEvent>;
|
||||
|
||||
ready(): void;
|
||||
registerWindow(window: ICodeWindow): void;
|
||||
|
||||
unload(window: ICodeWindow, reason: UnloadReason): TPromise<boolean /* veto */>;
|
||||
/**
|
||||
* Unload a window for the provided reason. All lifecycle event handlers are triggered.
|
||||
*/
|
||||
unload(window: ICodeWindow, reason: UnloadReason): Thenable<boolean /* veto */>;
|
||||
|
||||
/**
|
||||
* Restart the application with optional arguments (CLI). All lifecycle event handlers are triggered.
|
||||
*/
|
||||
relaunch(options?: { addArgs?: string[], removeArgs?: string[] }): void;
|
||||
|
||||
quit(fromUpdate?: boolean): TPromise<boolean /* veto */>;
|
||||
/**
|
||||
* Shutdown the application normally. All lifecycle event handlers are triggered.
|
||||
*/
|
||||
quit(fromUpdate?: boolean): Thenable<boolean /* veto */>;
|
||||
|
||||
/**
|
||||
* Forcefully shutdown the application. No livecycle event handlers are triggered.
|
||||
*/
|
||||
kill(code?: number): void;
|
||||
}
|
||||
|
||||
export class LifecycleService implements ILifecycleService {
|
||||
export class LifecycleService extends Disposable implements ILifecycleService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private static readonly QUIT_FROM_RESTART_MARKER = 'quit.from.restart'; // use a marker to find out if the session was restarted
|
||||
|
||||
private windowToCloseRequest: { [windowId: string]: boolean };
|
||||
private quitRequested: boolean;
|
||||
private pendingQuitPromise: TPromise<boolean>;
|
||||
private pendingQuitPromiseComplete: TValueCallback<boolean>;
|
||||
private oneTimeListenerTokenGenerator: number;
|
||||
private _wasRestarted: boolean;
|
||||
private windowCounter: number;
|
||||
private windowToCloseRequest: { [windowId: string]: boolean } = Object.create(null);
|
||||
private oneTimeListenerTokenGenerator = 0;
|
||||
private windowCounter = 0;
|
||||
|
||||
private _onBeforeShutdown = new Emitter<void>();
|
||||
onBeforeShutdown: Event<void> = this._onBeforeShutdown.event;
|
||||
private pendingQuitPromise: Thenable<boolean> | null;
|
||||
private pendingQuitPromiseResolve: { (veto: boolean): void } | null;
|
||||
|
||||
private _onShutdown = new Emitter<void>();
|
||||
onShutdown: Event<void> = this._onShutdown.event;
|
||||
private pendingWillShutdownPromise: Thenable<void> | null;
|
||||
|
||||
private _onBeforeWindowClose = new Emitter<ICodeWindow>();
|
||||
onBeforeWindowClose: Event<ICodeWindow> = this._onBeforeWindowClose.event;
|
||||
private _quitRequested = false;
|
||||
get quitRequested(): boolean { return this._quitRequested; }
|
||||
|
||||
private _onBeforeWindowUnload = new Emitter<IWindowUnloadEvent>();
|
||||
onBeforeWindowUnload: Event<IWindowUnloadEvent> = this._onBeforeWindowUnload.event;
|
||||
private _wasRestarted: boolean = false;
|
||||
get wasRestarted(): boolean { return this._wasRestarted; }
|
||||
|
||||
private readonly _onBeforeShutdown = this._register(new Emitter<void>());
|
||||
readonly onBeforeShutdown: Event<void> = this._onBeforeShutdown.event;
|
||||
|
||||
private readonly _onWillShutdown = this._register(new Emitter<ShutdownEvent>());
|
||||
readonly onWillShutdown: Event<ShutdownEvent> = this._onWillShutdown.event;
|
||||
|
||||
private readonly _onBeforeWindowClose = this._register(new Emitter<ICodeWindow>());
|
||||
readonly onBeforeWindowClose: Event<ICodeWindow> = this._onBeforeWindowClose.event;
|
||||
|
||||
private readonly _onBeforeWindowUnload = this._register(new Emitter<IWindowUnloadEvent>());
|
||||
readonly onBeforeWindowUnload: Event<IWindowUnloadEvent> = this._onBeforeWindowUnload.event;
|
||||
|
||||
constructor(
|
||||
@ILogService private logService: ILogService,
|
||||
@IStateService private stateService: IStateService
|
||||
) {
|
||||
this.windowToCloseRequest = Object.create(null);
|
||||
this.quitRequested = false;
|
||||
this.oneTimeListenerTokenGenerator = 0;
|
||||
this._wasRestarted = false;
|
||||
this.windowCounter = 0;
|
||||
super();
|
||||
|
||||
this.handleRestarted();
|
||||
}
|
||||
@@ -129,55 +147,98 @@ export class LifecycleService implements ILifecycleService {
|
||||
}
|
||||
}
|
||||
|
||||
get wasRestarted(): boolean {
|
||||
return this._wasRestarted;
|
||||
}
|
||||
|
||||
get isQuitRequested(): boolean {
|
||||
return !!this.quitRequested;
|
||||
}
|
||||
|
||||
ready(): void {
|
||||
this.registerListeners();
|
||||
}
|
||||
|
||||
private registerListeners(): void {
|
||||
|
||||
// before-quit
|
||||
app.on('before-quit', e => {
|
||||
this.logService.trace('Lifecycle#before-quit');
|
||||
|
||||
if (this.quitRequested) {
|
||||
this.logService.trace('Lifecycle#before-quit - returning because quit was already requested');
|
||||
// before-quit: an event that is fired if application quit was
|
||||
// requested but before any window was closed.
|
||||
const beforeQuitListener = () => {
|
||||
if (this._quitRequested) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.quitRequested = true;
|
||||
this.logService.trace('Lifecycle#app.on(before-quit)');
|
||||
this._quitRequested = true;
|
||||
|
||||
// Emit event to indicate that we are about to shutdown
|
||||
this.logService.trace('Lifecycle#onBeforeShutdown.fire()');
|
||||
this._onBeforeShutdown.fire();
|
||||
|
||||
// macOS: can run without any window open. in that case we fire
|
||||
// the onShutdown() event directly because there is no veto to be expected.
|
||||
// the onWillShutdown() event directly because there is no veto
|
||||
// to be expected.
|
||||
if (isMacintosh && this.windowCounter === 0) {
|
||||
this.logService.trace('Lifecycle#onShutdown.fire()');
|
||||
this._onShutdown.fire();
|
||||
this.beginOnWillShutdown();
|
||||
}
|
||||
});
|
||||
};
|
||||
app.addListener('before-quit', beforeQuitListener);
|
||||
|
||||
// window-all-closed
|
||||
app.on('window-all-closed', () => {
|
||||
this.logService.trace('Lifecycle#window-all-closed');
|
||||
// window-all-closed: an event that only fires when the last window
|
||||
// was closed. We override this event to be in charge if app.quit()
|
||||
// should be called or not.
|
||||
const windowAllClosedListener = () => {
|
||||
this.logService.trace('Lifecycle#app.on(window-all-closed)');
|
||||
|
||||
// Windows/Linux: we quit when all windows have closed
|
||||
// Mac: we only quit when quit was requested
|
||||
if (this.quitRequested || process.platform !== 'darwin') {
|
||||
if (this._quitRequested || !isMacintosh) {
|
||||
app.quit();
|
||||
}
|
||||
};
|
||||
app.addListener('window-all-closed', windowAllClosedListener);
|
||||
|
||||
// will-quit: an event that is fired after all windows have been
|
||||
// closed, but before actually quitting.
|
||||
app.once('will-quit', e => {
|
||||
this.logService.trace('Lifecycle#app.on(will-quit)');
|
||||
|
||||
// Prevent the quit until the shutdown promise was resolved
|
||||
e.preventDefault();
|
||||
|
||||
// Start shutdown sequence
|
||||
const shutdownPromise = this.beginOnWillShutdown();
|
||||
|
||||
// Wait until shutdown is signaled to be complete
|
||||
always(shutdownPromise, () => {
|
||||
|
||||
// Resolve pending quit promise now without veto
|
||||
this.resolvePendingQuitPromise(false /* no veto */);
|
||||
|
||||
// Quit again, this time do not prevent this, since our
|
||||
// will-quit listener is only installed "once". Also
|
||||
// remove any listener we have that is no longer needed
|
||||
app.removeListener('before-quit', beforeQuitListener);
|
||||
app.removeListener('window-all-closed', windowAllClosedListener);
|
||||
app.quit();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private beginOnWillShutdown(): Thenable<void> {
|
||||
if (this.pendingWillShutdownPromise) {
|
||||
return this.pendingWillShutdownPromise; // shutdown is already running
|
||||
}
|
||||
|
||||
this.logService.trace('Lifecycle#onWillShutdown.fire()');
|
||||
|
||||
const joiners: Thenable<void>[] = [];
|
||||
|
||||
this._onWillShutdown.fire({
|
||||
join(promise) {
|
||||
if (promise) {
|
||||
joiners.push(promise);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.pendingWillShutdownPromise = Promise.all(joiners).then(null, err => this.logService.error(err));
|
||||
|
||||
return this.pendingWillShutdownPromise;
|
||||
}
|
||||
|
||||
registerWindow(window: ICodeWindow): void {
|
||||
|
||||
// track window count
|
||||
@@ -185,102 +246,110 @@ export class LifecycleService implements ILifecycleService {
|
||||
|
||||
// Window Before Closing: Main -> Renderer
|
||||
window.win.on('close', e => {
|
||||
const windowId = window.id;
|
||||
this.logService.trace('Lifecycle#window-before-close', windowId);
|
||||
|
||||
// The window already acknowledged to be closed
|
||||
const windowId = window.id;
|
||||
if (this.windowToCloseRequest[windowId]) {
|
||||
this.logService.trace('Lifecycle#window-close', windowId);
|
||||
|
||||
delete this.windowToCloseRequest[windowId];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.logService.trace(`Lifecycle#window.on('close') - window ID ${window.id}`);
|
||||
|
||||
// Otherwise prevent unload and handle it from window
|
||||
e.preventDefault();
|
||||
this.unload(window, UnloadReason.CLOSE).done(veto => {
|
||||
if (!veto) {
|
||||
this.windowToCloseRequest[windowId] = true;
|
||||
|
||||
this.logService.trace('Lifecycle#onBeforeWindowClose.fire()');
|
||||
this._onBeforeWindowClose.fire(window);
|
||||
|
||||
window.close();
|
||||
} else {
|
||||
this.quitRequested = false;
|
||||
this.unload(window, UnloadReason.CLOSE).then(veto => {
|
||||
if (veto) {
|
||||
delete this.windowToCloseRequest[windowId];
|
||||
return;
|
||||
}
|
||||
|
||||
this.windowToCloseRequest[windowId] = true;
|
||||
|
||||
// Fire onBeforeWindowClose before actually closing
|
||||
this.logService.trace(`Lifecycle#onBeforeWindowClose.fire() - window ID ${windowId}`);
|
||||
this._onBeforeWindowClose.fire(window);
|
||||
|
||||
// No veto, close window now
|
||||
window.close();
|
||||
});
|
||||
});
|
||||
|
||||
// Window After Closing
|
||||
window.win.on('closed', e => {
|
||||
const windowId = window.id;
|
||||
this.logService.trace('Lifecycle#window-closed', windowId);
|
||||
this.logService.trace(`Lifecycle#window.on('closed') - window ID ${window.id}`);
|
||||
|
||||
// update window count
|
||||
this.windowCounter--;
|
||||
|
||||
// if there are no more code windows opened, fire the onShutdown event, unless
|
||||
// if there are no more code windows opened, fire the onWillShutdown event, unless
|
||||
// we are on macOS where it is perfectly fine to close the last window and
|
||||
// the application continues running (unless quit was actually requested)
|
||||
if (this.windowCounter === 0 && (!isMacintosh || this.isQuitRequested)) {
|
||||
this.logService.trace('Lifecycle#onShutdown.fire()');
|
||||
this._onShutdown.fire();
|
||||
if (this.windowCounter === 0 && (!isMacintosh || this._quitRequested)) {
|
||||
this.beginOnWillShutdown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
unload(window: ICodeWindow, reason: UnloadReason): TPromise<boolean /* veto */> {
|
||||
unload(window: ICodeWindow, reason: UnloadReason): Thenable<boolean /* veto */> {
|
||||
|
||||
// Always allow to unload a window that is not yet ready
|
||||
if (window.readyState !== ReadyState.READY) {
|
||||
return TPromise.as<boolean>(false);
|
||||
if (!window.isReady) {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
this.logService.trace('Lifecycle#unload()', window.id);
|
||||
|
||||
const windowUnloadReason = this.quitRequested ? UnloadReason.QUIT : reason;
|
||||
this.logService.trace(`Lifecycle#unload() - window ID ${window.id}`);
|
||||
|
||||
// first ask the window itself if it vetos the unload
|
||||
const windowUnloadReason = this._quitRequested ? UnloadReason.QUIT : reason;
|
||||
return this.onBeforeUnloadWindowInRenderer(window, windowUnloadReason).then(veto => {
|
||||
if (veto) {
|
||||
this.logService.trace('Lifecycle#unload(): veto in renderer', window.id);
|
||||
this.logService.trace(`Lifecycle#unload() - veto in renderer (window ID ${window.id})`);
|
||||
|
||||
return this.handleVeto(veto);
|
||||
return this.handleWindowUnloadVeto(veto);
|
||||
}
|
||||
|
||||
// then check for vetos in the main side
|
||||
return this.onBeforeUnloadWindowInMain(window, windowUnloadReason).then(veto => {
|
||||
if (veto) {
|
||||
this.logService.trace('Lifecycle#unload(): veto in main', window.id);
|
||||
this.logService.trace(`Lifecycle#unload() - veto in main (window ID ${window.id})`);
|
||||
|
||||
return this.handleVeto(veto);
|
||||
} else {
|
||||
this.logService.trace('Lifecycle#unload(): unload continues without veto', window.id);
|
||||
return this.handleWindowUnloadVeto(veto);
|
||||
}
|
||||
|
||||
this.logService.trace(`Lifecycle#unload() - no veto (window ID ${window.id})`);
|
||||
|
||||
// finally if there are no vetos, unload the renderer
|
||||
return this.onWillUnloadWindowInRenderer(window, windowUnloadReason).then(() => false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private handleVeto(veto: boolean): boolean {
|
||||
|
||||
// Any cancellation also cancels a pending quit if present
|
||||
if (veto && this.pendingQuitPromiseComplete) {
|
||||
this.pendingQuitPromiseComplete(true /* veto */);
|
||||
this.pendingQuitPromiseComplete = null;
|
||||
this.pendingQuitPromise = null;
|
||||
private handleWindowUnloadVeto(veto: boolean): boolean {
|
||||
if (!veto) {
|
||||
return false; // no veto
|
||||
}
|
||||
|
||||
return veto;
|
||||
// a veto resolves any pending quit with veto
|
||||
this.resolvePendingQuitPromise(true /* veto */);
|
||||
|
||||
// a veto resets the pending quit request flag
|
||||
this._quitRequested = false;
|
||||
|
||||
return true; // veto
|
||||
}
|
||||
|
||||
private onBeforeUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): TPromise<boolean /* veto */> {
|
||||
return new TPromise<boolean>(c => {
|
||||
private resolvePendingQuitPromise(veto: boolean): void {
|
||||
if (this.pendingQuitPromiseResolve) {
|
||||
this.pendingQuitPromiseResolve(veto);
|
||||
this.pendingQuitPromiseResolve = null;
|
||||
this.pendingQuitPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
private onBeforeUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Thenable<boolean /* veto */> {
|
||||
return new Promise<boolean>(c => {
|
||||
const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;
|
||||
const okChannel = `vscode:ok${oneTimeEventToken}`;
|
||||
const cancelChannel = `vscode:cancel${oneTimeEventToken}`;
|
||||
@@ -297,8 +366,8 @@ export class LifecycleService implements ILifecycleService {
|
||||
});
|
||||
}
|
||||
|
||||
private onBeforeUnloadWindowInMain(window: ICodeWindow, reason: UnloadReason): TPromise<boolean /* veto */> {
|
||||
const vetos: (boolean | TPromise<boolean>)[] = [];
|
||||
private onBeforeUnloadWindowInMain(window: ICodeWindow, reason: UnloadReason): Thenable<boolean /* veto */> {
|
||||
const vetos: (boolean | Thenable<boolean>)[] = [];
|
||||
|
||||
this._onBeforeWindowUnload.fire({
|
||||
reason,
|
||||
@@ -311,12 +380,12 @@ export class LifecycleService implements ILifecycleService {
|
||||
return handleVetos(vetos, err => this.logService.error(err));
|
||||
}
|
||||
|
||||
private onWillUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): TPromise<void> {
|
||||
return new TPromise<void>(c => {
|
||||
private onWillUnloadWindowInRenderer(window: ICodeWindow, reason: UnloadReason): Thenable<void> {
|
||||
return new Promise<void>(resolve => {
|
||||
const oneTimeEventToken = this.oneTimeListenerTokenGenerator++;
|
||||
const replyChannel = `vscode:reply${oneTimeEventToken}`;
|
||||
|
||||
ipc.once(replyChannel, () => c(void 0));
|
||||
ipc.once(replyChannel, () => resolve());
|
||||
|
||||
window.send('vscode:onWillUnload', { replyChannel, reason });
|
||||
});
|
||||
@@ -326,48 +395,32 @@ export class LifecycleService implements ILifecycleService {
|
||||
* A promise that completes to indicate if the quit request has been veto'd
|
||||
* by the user or not.
|
||||
*/
|
||||
quit(fromUpdate?: boolean): TPromise<boolean /* veto */> {
|
||||
this.logService.trace('Lifecycle#quit()');
|
||||
|
||||
if (!this.pendingQuitPromise) {
|
||||
this.pendingQuitPromise = new TPromise<boolean>(c => {
|
||||
|
||||
// Store as field to access it from a window cancellation
|
||||
this.pendingQuitPromiseComplete = c;
|
||||
|
||||
// The will-quit event is fired when all windows have closed without veto
|
||||
app.once('will-quit', () => {
|
||||
this.logService.trace('Lifecycle#will-quit');
|
||||
|
||||
if (this.pendingQuitPromiseComplete) {
|
||||
if (fromUpdate) {
|
||||
this.stateService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true);
|
||||
}
|
||||
|
||||
this.pendingQuitPromiseComplete(false /* no veto */);
|
||||
this.pendingQuitPromiseComplete = null;
|
||||
this.pendingQuitPromise = null;
|
||||
}
|
||||
});
|
||||
|
||||
// Calling app.quit() will trigger the close handlers of each opened window
|
||||
// and only if no window vetoed the shutdown, we will get the will-quit event
|
||||
this.logService.trace('Lifecycle#quit() - calling app.quit()');
|
||||
app.quit();
|
||||
});
|
||||
} else {
|
||||
this.logService.trace('Lifecycle#quit() - a pending quit was found');
|
||||
quit(fromUpdate?: boolean): Thenable<boolean /* veto */> {
|
||||
if (this.pendingQuitPromise) {
|
||||
return this.pendingQuitPromise;
|
||||
}
|
||||
|
||||
this.logService.trace(`Lifecycle#quit() - from update: ${fromUpdate}`);
|
||||
|
||||
// Remember the reason for quit was to restart
|
||||
if (fromUpdate) {
|
||||
this.stateService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true);
|
||||
}
|
||||
|
||||
this.pendingQuitPromise = new Promise(resolve => {
|
||||
|
||||
// Store as field to access it from a window cancellation
|
||||
this.pendingQuitPromiseResolve = resolve;
|
||||
|
||||
// Calling app.quit() will trigger the close handlers of each opened window
|
||||
// and only if no window vetoed the shutdown, we will get the will-quit event
|
||||
this.logService.trace('Lifecycle#quit() - calling app.quit()');
|
||||
app.quit();
|
||||
});
|
||||
|
||||
return this.pendingQuitPromise;
|
||||
}
|
||||
|
||||
kill(code?: number): void {
|
||||
this.logService.trace('Lifecycle#kill()');
|
||||
|
||||
app.exit(code);
|
||||
}
|
||||
|
||||
relaunch(options?: { addArgs?: string[], removeArgs?: string[] }): void {
|
||||
this.logService.trace('Lifecycle#relaunch()');
|
||||
|
||||
@@ -385,9 +438,11 @@ export class LifecycleService implements ILifecycleService {
|
||||
}
|
||||
}
|
||||
|
||||
let vetoed = false;
|
||||
let quitVetoed = false;
|
||||
app.once('quit', () => {
|
||||
if (!vetoed) {
|
||||
if (!quitVetoed) {
|
||||
|
||||
// Remember the reason for quit was to restart
|
||||
this.stateService.setItem(LifecycleService.QUIT_FROM_RESTART_MARKER, true);
|
||||
|
||||
// Windows: we are about to restart and as such we need to restore the original
|
||||
@@ -396,18 +451,29 @@ export class LifecycleService implements ILifecycleService {
|
||||
// Code starts it will set it back to the installation directory again.
|
||||
try {
|
||||
if (isWindows) {
|
||||
process.chdir(process.env['VSCODE_CWD']);
|
||||
const vscodeCwd = process.env['VSCODE_CWD'];
|
||||
if (vscodeCwd) {
|
||||
process.chdir(vscodeCwd);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.logService.error(err);
|
||||
}
|
||||
|
||||
// relaunch after we are sure there is no veto
|
||||
this.logService.trace('Lifecycle#relaunch() - calling app.relaunch()');
|
||||
app.relaunch({ args });
|
||||
}
|
||||
});
|
||||
|
||||
this.quit().then(veto => {
|
||||
vetoed = veto;
|
||||
});
|
||||
// app.relaunch() does not quit automatically, so we quit first,
|
||||
// check for vetoes and then relaunch from the app.on('quit') event
|
||||
this.quit().then(veto => quitVetoed = veto);
|
||||
}
|
||||
|
||||
kill(code?: number): void {
|
||||
this.logService.trace('Lifecycle#kill()');
|
||||
|
||||
app.exit(code);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user