55
66import { TPromise } from 'vs/base/common/winjs.base' ;
77import { toErrorMessage } from 'vs/base/common/errorMessage' ;
8- import { ILifecycleService , ShutdownEvent , ShutdownReason , StartupKind , LifecyclePhase , handleVetos , LifecyclePhaseToString } from 'vs/platform/lifecycle/common/lifecycle' ;
8+ import { ILifecycleService , WillShutdownEvent , ShutdownReason , StartupKind , LifecyclePhase , handleVetos , LifecyclePhaseToString , ShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle' ;
99import { IStorageService , StorageScope } from 'vs/platform/storage/common/storage' ;
1010import { ipcRenderer as ipc } from 'electron' ;
1111import { Event , Emitter } from 'vs/base/common/event' ;
@@ -14,74 +14,95 @@ import { mark } from 'vs/base/common/performance';
1414import { Barrier } from 'vs/base/common/async' ;
1515import { ILogService } from 'vs/platform/log/common/log' ;
1616import { INotificationService } from 'vs/platform/notification/common/notification' ;
17+ import { Disposable } from 'vs/base/common/lifecycle' ;
18+ import { onUnexpectedError } from 'vs/base/common/errors' ;
1719
18- export class LifecycleService implements ILifecycleService {
20+ export class LifecycleService extends Disposable implements ILifecycleService {
1921
20- private static readonly _lastShutdownReasonKey = 'lifecyle.lastShutdownReason' ;
22+ private static readonly LAST_SHUTDOWN_REASON_KEY = 'lifecyle.lastShutdownReason' ;
2123
2224 _serviceBrand : any ;
2325
24- private readonly _onWillShutdown = new Emitter < ShutdownEvent > ( ) ;
25- private readonly _onShutdown = new Emitter < ShutdownReason > ( ) ;
26+ private readonly _onWillShutdown = this . _register ( new Emitter < WillShutdownEvent > ( ) ) ;
27+ get onWillShutdown ( ) : Event < WillShutdownEvent > { return this . _onWillShutdown . event ; }
28+
29+ private readonly _onShutdown = this . _register ( new Emitter < ShutdownEvent > ( ) ) ;
30+ get onShutdown ( ) : Event < ShutdownEvent > { return this . _onShutdown . event ; }
31+
2632 private readonly _startupKind : StartupKind ;
33+ get startupKind ( ) : StartupKind { return this . _startupKind ; }
2734
2835 private _phase : LifecyclePhase = LifecyclePhase . Starting ;
36+ get phase ( ) : LifecyclePhase { return this . _phase ; }
37+
2938 private _phaseWhen = new Map < LifecyclePhase , Barrier > ( ) ;
3039
3140 constructor (
32- @INotificationService private readonly _notificationService : INotificationService ,
33- @IWindowService private readonly _windowService : IWindowService ,
34- @IStorageService private readonly _storageService : IStorageService ,
35- @ILogService private readonly _logService : ILogService
41+ @INotificationService private notificationService : INotificationService ,
42+ @IWindowService private windowService : IWindowService ,
43+ @IStorageService private storageService : IStorageService ,
44+ @ILogService private logService : ILogService
3645 ) {
37- const lastShutdownReason = this . _storageService . getInteger ( LifecycleService . _lastShutdownReasonKey , StorageScope . WORKSPACE ) ;
38- this . _storageService . remove ( LifecycleService . _lastShutdownReasonKey , StorageScope . WORKSPACE ) ;
46+ super ( ) ;
47+
48+ this . _startupKind = this . resolveStartupKind ( ) ;
49+
50+ this . registerListeners ( ) ;
51+ }
52+
53+ private resolveStartupKind ( ) : StartupKind {
54+ const lastShutdownReason = this . storageService . getInteger ( LifecycleService . LAST_SHUTDOWN_REASON_KEY , StorageScope . WORKSPACE ) ;
55+ this . storageService . remove ( LifecycleService . LAST_SHUTDOWN_REASON_KEY , StorageScope . WORKSPACE ) ;
56+
57+ let startupKind : StartupKind ;
3958 if ( lastShutdownReason === ShutdownReason . RELOAD ) {
40- this . _startupKind = StartupKind . ReloadedWindow ;
59+ startupKind = StartupKind . ReloadedWindow ;
4160 } else if ( lastShutdownReason === ShutdownReason . LOAD ) {
42- this . _startupKind = StartupKind . ReopenedWindow ;
61+ startupKind = StartupKind . ReopenedWindow ;
4362 } else {
44- this . _startupKind = StartupKind . NewWindow ;
63+ startupKind = StartupKind . NewWindow ;
4564 }
4665
47- this . _logService . trace ( `lifecycle: starting up (startup kind: ${ this . _startupKind } )` ) ;
66+ this . logService . trace ( `lifecycle: starting up (startup kind: ${ this . _startupKind } )` ) ;
4867
49- this . _registerListeners ( ) ;
68+ return startupKind ;
5069 }
5170
52- private _registerListeners ( ) : void {
53- const windowId = this . _windowService . getCurrentWindowId ( ) ;
71+ private registerListeners ( ) : void {
72+ const windowId = this . windowService . getCurrentWindowId ( ) ;
5473
5574 // Main side indicates that window is about to unload, check for vetos
5675 ipc . on ( 'vscode:onBeforeUnload' , ( event , reply : { okChannel : string , cancelChannel : string , reason : ShutdownReason } ) => {
57- this . _logService . trace ( `lifecycle: onBeforeUnload (reason: ${ reply . reason } )` ) ;
76+ this . logService . trace ( `lifecycle: onBeforeUnload (reason: ${ reply . reason } )` ) ;
5877
5978 // store shutdown reason to retrieve next startup
60- this . _storageService . store ( LifecycleService . _lastShutdownReasonKey , JSON . stringify ( reply . reason ) , StorageScope . WORKSPACE ) ;
79+ this . storageService . store ( LifecycleService . LAST_SHUTDOWN_REASON_KEY , JSON . stringify ( reply . reason ) , StorageScope . WORKSPACE ) ;
6180
6281 // trigger onWillShutdown events and veto collecting
63- this . onBeforeUnload ( reply . reason ) . then ( veto => {
82+ this . handleWillShutdown ( reply . reason ) . then ( veto => {
6483 if ( veto ) {
65- this . _logService . trace ( 'lifecycle: onBeforeUnload prevented via veto' ) ;
66- this . _storageService . remove ( LifecycleService . _lastShutdownReasonKey , StorageScope . WORKSPACE ) ;
84+ this . logService . trace ( 'lifecycle: onBeforeUnload prevented via veto' ) ;
85+ this . storageService . remove ( LifecycleService . LAST_SHUTDOWN_REASON_KEY , StorageScope . WORKSPACE ) ;
6786 ipc . send ( reply . cancelChannel , windowId ) ;
6887 } else {
69- this . _logService . trace ( 'lifecycle: onBeforeUnload continues without veto' ) ;
88+ this . logService . trace ( 'lifecycle: onBeforeUnload continues without veto' ) ;
7089 ipc . send ( reply . okChannel , windowId ) ;
7190 }
7291 } ) ;
7392 } ) ;
7493
7594 // Main side indicates that we will indeed shutdown
7695 ipc . on ( 'vscode:onWillUnload' , ( event , reply : { replyChannel : string , reason : ShutdownReason } ) => {
77- this . _logService . trace ( `lifecycle: onWillUnload (reason: ${ reply . reason } )` ) ;
96+ this . logService . trace ( `lifecycle: onWillUnload (reason: ${ reply . reason } )` ) ;
7897
79- this . _onShutdown . fire ( reply . reason ) ;
80- ipc . send ( reply . replyChannel , windowId ) ;
98+ // trigger onShutdown events and joining
99+ return this . handleShutdown ( reply . reason ) . then ( ( ) => {
100+ ipc . send ( reply . replyChannel , windowId ) ;
101+ } ) ;
81102 } ) ;
82103 }
83104
84- private onBeforeUnload ( reason : ShutdownReason ) : TPromise < boolean > {
105+ private handleWillShutdown ( reason : ShutdownReason ) : TPromise < boolean > {
85106 const vetos : ( boolean | Thenable < boolean > ) [ ] = [ ] ;
86107
87108 this . _onWillShutdown . fire ( {
@@ -91,11 +112,28 @@ export class LifecycleService implements ILifecycleService {
91112 reason
92113 } ) ;
93114
94- return handleVetos ( vetos , err => this . _notificationService . error ( toErrorMessage ( err ) ) ) ;
115+ return handleVetos ( vetos , err => {
116+ this . notificationService . error ( toErrorMessage ( err ) ) ;
117+ onUnexpectedError ( err ) ;
118+ } ) ;
95119 }
96120
97- get phase ( ) : LifecyclePhase {
98- return this . _phase ;
121+ private handleShutdown ( reason : ShutdownReason ) : Thenable < void > {
122+ const joiners : Thenable < void > [ ] = [ ] ;
123+
124+ this . _onShutdown . fire ( {
125+ join ( promise ) {
126+ if ( promise ) {
127+ joiners . push ( promise ) ;
128+ }
129+ } ,
130+ reason
131+ } ) ;
132+
133+ return TPromise . join ( joiners ) . then ( ( ) => void 0 , err => {
134+ this . notificationService . error ( toErrorMessage ( err ) ) ;
135+ onUnexpectedError ( err ) ;
136+ } ) ;
99137 }
100138
101139 set phase ( value : LifecyclePhase ) {
@@ -107,7 +145,7 @@ export class LifecycleService implements ILifecycleService {
107145 return ;
108146 }
109147
110- this . _logService . trace ( `lifecycle: phase changed (value: ${ value } )` ) ;
148+ this . logService . trace ( `lifecycle: phase changed (value: ${ value } )` ) ;
111149
112150 this . _phase = value ;
113151 mark ( `LifecyclePhase/${ LifecyclePhaseToString ( value ) } ` ) ;
@@ -131,16 +169,4 @@ export class LifecycleService implements ILifecycleService {
131169
132170 return barrier . wait ( ) ;
133171 }
134-
135- get startupKind ( ) : StartupKind {
136- return this . _startupKind ;
137- }
138-
139- get onWillShutdown ( ) : Event < ShutdownEvent > {
140- return this . _onWillShutdown . event ;
141- }
142-
143- get onShutdown ( ) : Event < ShutdownReason > {
144- return this . _onShutdown . event ;
145- }
146172}
0 commit comments