1
>
/*---------------------------------------------------------------------------------------------
cancellation.ts
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 { Emitter, Event } from './event.js';
7
>
import { DisposableStore, IDisposable } from './lifecycle.js';
8
>
9
>
export interface CancellationToken {
10
>
11
>
/**
12
>
* A flag signalling is cancellation has been requested.
13
>
*/
14
>
readonly isCancellationRequested: boolean;
15
>
16
>
/**
17
>
* An event which fires when cancellation is requested. This event
18
>
* only ever fires `once` as cancellation can only happen once. Listeners
19
>
* that are registered after cancellation will be called (next event loop run),
20
>
* but also only once.
21
>
*
22
>
* @event
23
>
*/
24
>
readonly onCancellationRequested: (listener: (e: void) => unknown, thisArgs?: unknown, disposables?: IDisposable[]) => IDisposable;
25
>
}
26
>
27
>
const shortcutEvent: Event<void> = Object.freeze(function (callback, context?): IDisposable {
28
const handle = setTimeout(callback.bind(context), 0);
29
return { dispose() { clearTimeout(handle); } };
30
});
32
>
export namespace CancellationToken {
33
>
34
>
export function isCancellationToken(thing: unknown): thing is CancellationToken {
35
if (thing === CancellationToken.None || thing === CancellationToken.Cancelled) {
36
return true;