Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/lifecycle/common/lifecycle.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CancellationToken } from '../../../../base/common/cancellation.js';
7
import { Event } from '../../../../base/common/event.js';
8
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
9
10
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
11
12
/**
13
* An event that is send out when the window is about to close. Clients have a chance to veto
14
* the closing by either calling veto with a boolean "true" directly or with a promise that
15
* resolves to a boolean. Returning a promise is useful in cases of long running operations
16
* on shutdown.
17
*
18
* Note: It is absolutely important to avoid long running promises if possible. Please try hard
19
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
20
*/
21
export interface BeforeShutdownEvent {
22
23
/**
24
* The reason why the application will be shutting down.
25
*/
26
readonly reason: ShutdownReason;
27
28
/**
29
* Allows to veto the shutdown. The veto can be a long running operation but it
30
* will block the application from closing.
31
*
32
* @param id to identify the veto operation in case it takes very long or never
33
* completes.
34
*/
35
veto(value: boolean | Promise<boolean>, id: string): void;
36
}
37
38
export interface InternalBeforeShutdownEvent extends BeforeShutdownEvent {
39
40
/**
41
* Allows to set a veto operation to run after all other
42
* vetos have been handled from the `BeforeShutdownEvent`
43
*
44
* This method is hidden from the API because it is intended
45
* to be only used once internally.
46
*/
47
finalVeto(vetoFn: () => boolean | Promise<boolean>, id: string): void;
48
}
49
50
/**
51
* An event that signals an error happened during `onBeforeShutdown` veto handling.
52
* In this case the shutdown operation will not proceed because this is an unexpected
53
* condition that is treated like a veto.
54
*/
55
export interface BeforeShutdownErrorEvent {
56
57
/**
58
* The reason why the application is shutting down.
59
*/
60
readonly reason: ShutdownReason;
61
62
/**
63
* The error that happened during shutdown handling.
64
*/
65
readonly error: Error;
66
}
67
68
export enum WillShutdownJoinerOrder {
69
70
/**
71
* Joiners to run before the `Last` joiners. This is the default order and best for
72
* most cases. You can be sure that services are still functional at this point.
73
*/
74
Default = 1,
75
76
/**
77
* The joiners to run last. This should ONLY be used in rare cases when you have no
78
* dependencies to workbench services or state. The workbench may be in a state where
79
* resources can no longer be accessed or changed.
80
*/
81
Last
82
}
83
84
export interface IWillShutdownEventJoiner {
85
readonly id: string;
86
readonly label: string;
87
readonly order?: WillShutdownJoinerOrder;
88
}
89
90
export interface IWillShutdownEventDefaultJoiner extends IWillShutdownEventJoiner {
91
readonly order?: WillShutdownJoinerOrder.Default;
92
}
93
94
export interface IWillShutdownEventLastJoiner extends IWillShutdownEventJoiner {
95
readonly order: WillShutdownJoinerOrder.Last;
96
}
97
98
/**
99
* An event that is send out when the window closes. Clients have a chance to join the closing
100
* by providing a promise from the join method. Returning a promise is useful in cases of long
101
* running operations on shutdown.
102
*
103
* Note: It is absolutely important to avoid long running promises if possible. Please try hard
104
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
105
*/
106
export interface WillShutdownEvent {
107
108
/**
109
* The reason why the application is shutting down.
110
*/
111
readonly reason: ShutdownReason;
112
113
/**
114
* A token that will signal cancellation when the
115
* shutdown was forced by the user.
116
*/
117
readonly token: CancellationToken;
118
119
/**
120
* Allows to join the shutdown. The promise can be a long running operation but it
121
* will block the application from closing.
122
*
123
* @param promise the promise to join the shutdown event.
124
* @param joiner to identify the join operation in case it takes very long or never
125
* completes.
126
*/
127
join(promise: Promise<void>, joiner: IWillShutdownEventDefaultJoiner): void;
128
129
/**
130
* Allows to join the shutdown at the end. The promise can be a long running operation but it
131
* will block the application from closing.
132
*
133
* @param promiseFn the promise to join the shutdown event.
134
* @param joiner to identify the join operation in case it takes very long or never
135
* completes.
136
*/
137
join(promiseFn: (() => Promise<void>), joiner: IWillShutdownEventLastJoiner): void;
138
139
/**
140
* Allows to access the joiners that have not finished joining this event.
141
*/
142
joiners(): IWillShutdownEventJoiner[];
143
144
/**
145
* Allows to enforce the shutdown, even when there are
146
* pending `join` operations to complete.
147
*/
148
force(): void;
149
}
150
151
export const enum ShutdownReason {
152
153
/**
154
* The window is closed.
155
*/
156
CLOSE = 1,
157
158
/**
159
* The window closes because the application quits.
160
*/
161
QUIT,
162
163
/**
164
* The window is reloaded.
165
*/
166
RELOAD,
167
168
/**
169
* The window is loaded into a different workspace context.
170
*/
171
LOAD
172
}
173
174
export const enum StartupKind {
175
NewWindow = 1,
176
ReloadedWindow = 3,
177
ReopenedWindow = 4
178
}
179
180
export function StartupKindToString(startupKind: StartupKind): string {
181
switch (startupKind) {
182
case StartupKind.NewWindow: return 'NewWindow';
183
case StartupKind.ReloadedWindow: return 'ReloadedWindow';
184
case StartupKind.ReopenedWindow: return 'ReopenedWindow';
185
}
186
}
187
188
export const enum LifecyclePhase {
189
190
/**
191
* The first phase signals that we are about to startup getting ready.
192
*
193
* Note: doing work in this phase blocks an editor from showing to
194
* the user, so please rather consider to use `Restored` phase.
195
*/
196
Starting = 1,
197
198
/**
199
* Services are ready and the window is about to restore its UI state.
200
*
201
* Note: doing work in this phase blocks an editor from showing to
202
* the user, so please rather consider to use `Restored` phase.
203
*/
204
Ready = 2,
205
206
/**
207
* Views, panels and editors have restored. Editors are given a bit of
208
* time to restore their contents.
209
*/
210
Restored = 3,
211
212
/**
213
* The last phase after views, panels and editors have restored and
214
* some time has passed (2-5 seconds).
215
*/
216
Eventually = 4
217
}
218
219
export function LifecyclePhaseToString(phase: LifecyclePhase): string {
220
switch (phase) {
221
case LifecyclePhase.Starting: return 'Starting';
222
case LifecyclePhase.Ready: return 'Ready';
223
case LifecyclePhase.Restored: return 'Restored';
224
case LifecyclePhase.Eventually: return 'Eventually';
225
}
226
}
227
228
/**
229
* A lifecycle service informs about lifecycle events of the
230
* application, such as shutdown.
231
*/
232
export interface ILifecycleService {
233
234
readonly _serviceBrand: undefined;
235
236
/**
237
* Value indicates how this window got loaded.
238
*/
239
readonly startupKind: StartupKind;
240
241
/**
242
* A flag indicating in what phase of the lifecycle we currently are.
243
*/
244
phase: LifecyclePhase;
245
246
/**
247
* Fired before shutdown happens. Allows listeners to veto against the
248
* shutdown to prevent it from happening.
249
*
250
* The event carries a shutdown reason that indicates how the shutdown was triggered.
251
*/
252
readonly onBeforeShutdown: Event<BeforeShutdownEvent>;
253
254
/**
255
* Fired when the shutdown was prevented by a component giving veto.
256
*/
257
readonly onShutdownVeto: Event<void>;
258
259
/**
260
* Fired when an error happened during `onBeforeShutdown` veto handling.
261
* In this case the shutdown operation will not proceed because this is
262
* an unexpected condition that is treated like a veto.
263
*
264
* The event carries a shutdown reason that indicates how the shutdown was triggered.
265
*/
266
readonly onBeforeShutdownError: Event<BeforeShutdownErrorEvent>;
267
268
/**
269
* Fired when no client is preventing the shutdown from happening (from `onBeforeShutdown`).
270
*
271
* This event can be joined with a long running operation via `WillShutdownEvent#join()` to
272
* handle long running shutdown operations.
273
*
274
* The event carries a shutdown reason that indicates how the shutdown was triggered.
275
*/
276
readonly onWillShutdown: Event<WillShutdownEvent>;
277
278
/**
279
* A flag indicating that we are about to shutdown without further veto.
280
*/
281
readonly willShutdown: boolean;
282
283
/**
284
* Fired when the shutdown is about to happen after long running shutdown operations
285
* have finished (from `onWillShutdown`).
286
*
287
* This event should be used to dispose resources.
288
*/
289
readonly onDidShutdown: Event<void>;
290
291
/**
292
* Returns a promise that resolves when a certain lifecycle phase
293
* has started.
294
*/
295
when(phase: LifecyclePhase): Promise<void>;
296
297
/**
298
* Triggers a shutdown of the workbench. Depending on native or web, this can have
299
* different implementations and behaviour.
300
*
301
* **Note:** this should normally not be called. See related methods in `IHostService`
302
* and `INativeHostService` to close a window or quit the application.
303
*/
304
shutdown(): Promise<void>;
305
}
306
307