Path: blob/main/src/vs/workbench/services/lifecycle/common/lifecycle.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CancellationToken } from '../../../../base/common/cancellation.js';6import { Event } from '../../../../base/common/event.js';7import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';89export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');1011/**12* An event that is send out when the window is about to close. Clients have a chance to veto13* the closing by either calling veto with a boolean "true" directly or with a promise that14* resolves to a boolean. Returning a promise is useful in cases of long running operations15* on shutdown.16*17* Note: It is absolutely important to avoid long running promises if possible. Please try hard18* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!19*/20export interface BeforeShutdownEvent {2122/**23* The reason why the application will be shutting down.24*/25readonly reason: ShutdownReason;2627/**28* Allows to veto the shutdown. The veto can be a long running operation but it29* will block the application from closing.30*31* @param id to identify the veto operation in case it takes very long or never32* completes.33*/34veto(value: boolean | Promise<boolean>, id: string): void;35}3637export interface InternalBeforeShutdownEvent extends BeforeShutdownEvent {3839/**40* Allows to set a veto operation to run after all other41* vetos have been handled from the `BeforeShutdownEvent`42*43* This method is hidden from the API because it is intended44* to be only used once internally.45*/46finalVeto(vetoFn: () => boolean | Promise<boolean>, id: string): void;47}4849/**50* An event that signals an error happened during `onBeforeShutdown` veto handling.51* In this case the shutdown operation will not proceed because this is an unexpected52* condition that is treated like a veto.53*/54export interface BeforeShutdownErrorEvent {5556/**57* The reason why the application is shutting down.58*/59readonly reason: ShutdownReason;6061/**62* The error that happened during shutdown handling.63*/64readonly error: Error;65}6667export enum WillShutdownJoinerOrder {6869/**70* Joiners to run before the `Last` joiners. This is the default order and best for71* most cases. You can be sure that services are still functional at this point.72*/73Default = 1,7475/**76* The joiners to run last. This should ONLY be used in rare cases when you have no77* dependencies to workbench services or state. The workbench may be in a state where78* resources can no longer be accessed or changed.79*/80Last81}8283export interface IWillShutdownEventJoiner {84readonly id: string;85readonly label: string;86readonly order?: WillShutdownJoinerOrder;87}8889export interface IWillShutdownEventDefaultJoiner extends IWillShutdownEventJoiner {90readonly order?: WillShutdownJoinerOrder.Default;91}9293export interface IWillShutdownEventLastJoiner extends IWillShutdownEventJoiner {94readonly order: WillShutdownJoinerOrder.Last;95}9697/**98* An event that is send out when the window closes. Clients have a chance to join the closing99* by providing a promise from the join method. Returning a promise is useful in cases of long100* running operations on shutdown.101*102* Note: It is absolutely important to avoid long running promises if possible. Please try hard103* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!104*/105export interface WillShutdownEvent {106107/**108* The reason why the application is shutting down.109*/110readonly reason: ShutdownReason;111112/**113* A token that will signal cancellation when the114* shutdown was forced by the user.115*/116readonly token: CancellationToken;117118/**119* Allows to join the shutdown. The promise can be a long running operation but it120* will block the application from closing.121*122* @param promise the promise to join the shutdown event.123* @param joiner to identify the join operation in case it takes very long or never124* completes.125*/126join(promise: Promise<void>, joiner: IWillShutdownEventDefaultJoiner): void;127128/**129* Allows to join the shutdown at the end. The promise can be a long running operation but it130* will block the application from closing.131*132* @param promiseFn the promise to join the shutdown event.133* @param joiner to identify the join operation in case it takes very long or never134* completes.135*/136join(promiseFn: (() => Promise<void>), joiner: IWillShutdownEventLastJoiner): void;137138/**139* Allows to access the joiners that have not finished joining this event.140*/141joiners(): IWillShutdownEventJoiner[];142143/**144* Allows to enforce the shutdown, even when there are145* pending `join` operations to complete.146*/147force(): void;148}149150export const enum ShutdownReason {151152/**153* The window is closed.154*/155CLOSE = 1,156157/**158* The window closes because the application quits.159*/160QUIT,161162/**163* The window is reloaded.164*/165RELOAD,166167/**168* The window is loaded into a different workspace context.169*/170LOAD171}172173export const enum StartupKind {174NewWindow = 1,175ReloadedWindow = 3,176ReopenedWindow = 4177}178179export function StartupKindToString(startupKind: StartupKind): string {180switch (startupKind) {181case StartupKind.NewWindow: return 'NewWindow';182case StartupKind.ReloadedWindow: return 'ReloadedWindow';183case StartupKind.ReopenedWindow: return 'ReopenedWindow';184}185}186187export const enum LifecyclePhase {188189/**190* The first phase signals that we are about to startup getting ready.191*192* Note: doing work in this phase blocks an editor from showing to193* the user, so please rather consider to use `Restored` phase.194*/195Starting = 1,196197/**198* Services are ready and the window is about to restore its UI state.199*200* Note: doing work in this phase blocks an editor from showing to201* the user, so please rather consider to use `Restored` phase.202*/203Ready = 2,204205/**206* Views, panels and editors have restored. Editors are given a bit of207* time to restore their contents.208*/209Restored = 3,210211/**212* The last phase after views, panels and editors have restored and213* some time has passed (2-5 seconds).214*/215Eventually = 4216}217218export function LifecyclePhaseToString(phase: LifecyclePhase): string {219switch (phase) {220case LifecyclePhase.Starting: return 'Starting';221case LifecyclePhase.Ready: return 'Ready';222case LifecyclePhase.Restored: return 'Restored';223case LifecyclePhase.Eventually: return 'Eventually';224}225}226227/**228* A lifecycle service informs about lifecycle events of the229* application, such as shutdown.230*/231export interface ILifecycleService {232233readonly _serviceBrand: undefined;234235/**236* Value indicates how this window got loaded.237*/238readonly startupKind: StartupKind;239240/**241* A flag indicating in what phase of the lifecycle we currently are.242*/243phase: LifecyclePhase;244245/**246* Fired before shutdown happens. Allows listeners to veto against the247* shutdown to prevent it from happening.248*249* The event carries a shutdown reason that indicates how the shutdown was triggered.250*/251readonly onBeforeShutdown: Event<BeforeShutdownEvent>;252253/**254* Fired when the shutdown was prevented by a component giving veto.255*/256readonly onShutdownVeto: Event<void>;257258/**259* Fired when an error happened during `onBeforeShutdown` veto handling.260* In this case the shutdown operation will not proceed because this is261* an unexpected condition that is treated like a veto.262*263* The event carries a shutdown reason that indicates how the shutdown was triggered.264*/265readonly onBeforeShutdownError: Event<BeforeShutdownErrorEvent>;266267/**268* Fired when no client is preventing the shutdown from happening (from `onBeforeShutdown`).269*270* This event can be joined with a long running operation via `WillShutdownEvent#join()` to271* handle long running shutdown operations.272*273* The event carries a shutdown reason that indicates how the shutdown was triggered.274*/275readonly onWillShutdown: Event<WillShutdownEvent>;276277/**278* A flag indicating that we are about to shutdown without further veto.279*/280readonly willShutdown: boolean;281282/**283* Fired when the shutdown is about to happen after long running shutdown operations284* have finished (from `onWillShutdown`).285*286* This event should be used to dispose resources.287*/288readonly onDidShutdown: Event<void>;289290/**291* Returns a promise that resolves when a certain lifecycle phase292* has started.293*/294when(phase: LifecyclePhase): Promise<void>;295296/**297* Triggers a shutdown of the workbench. Depending on native or web, this can have298* different implementations and behaviour.299*300* **Note:** this should normally not be called. See related methods in `IHostService`301* and `INativeHostService` to close a window or quit the application.302*/303shutdown(): Promise<void>;304}305306307