UNPKG

5.47 kBTypeScriptView Raw
1/*! *****************************************************************************
2Copyright (c) Microsoft Corporation.
3Licensed under the Apache License, Version 2.0.
4
5See LICENSE file in the project root for details.
6***************************************************************************** */
7import { Cancelable, CancelableSource, CancelSignal } from "@esfx/cancelable";
8import { Disposable } from "@esfx/disposable";
9/**
10 * Signals a CancellationToken that it should be canceled.
11 */
12export declare class CancellationTokenSource implements CancelableSource {
13 private _state;
14 private _token;
15 private _registrations;
16 private _linkingRegistrations;
17 /**
18 * Initializes a new instance of a CancellationTokenSource.
19 *
20 * @param linkedTokens An optional iterable of tokens to which to link this source.
21 */
22 constructor(linkedTokens?: Iterable<CancellationToken | Cancelable>);
23 /**
24 * Gets a CancellationToken linked to this source.
25 */
26 get token(): CancellationToken;
27 /**
28 * Cancels the source, evaluating any registered callbacks. If any callback raises an exception,
29 * the exception is propagated to a host specific unhanedle exception mechanism.
30 */
31 cancel(): void;
32 /**
33 * Closes the source, preventing the possibility of future cancellation.
34 */
35 close(): void;
36 /**
37 * Executes the provided callback.
38 *
39 * @param callback The callback to execute.
40 */
41 private _executeCallback;
42 /**
43 * Unlinks the source from any linked tokens.
44 */
45 private _unlink;
46 [Cancelable.cancelSignal](): Cancelable & CancelSignal;
47 [CancelableSource.cancel](): void;
48}
49/**
50 * Propagates notifications that operations should be canceled.
51 */
52export declare class CancellationToken implements Cancelable {
53 /**
54 * A token which will never be canceled.
55 */
56 static readonly none: CancellationToken;
57 /**
58 * A token that is already canceled.
59 */
60 static readonly canceled: CancellationToken;
61 private _source;
62 /**
63 * Gets a value indicating whether cancellation has been requested.
64 */
65 get cancellationRequested(): boolean;
66 /**
67 * Gets a value indicating whether the underlying source can be canceled.
68 */
69 get canBeCanceled(): boolean;
70 /**
71 * Adapts a CancellationToken-like primitive from a different library.
72 */
73 static from(cancelable: CancellationToken | VSCodeCancellationTokenLike | AbortSignalLike | Cancelable): CancellationToken;
74 /**
75 * Returns a CancellationToken that becomes canceled when **any** of the provided tokens are canceled.
76 * @param tokens An iterable of CancellationToken objects.
77 */
78 static race(tokens: Iterable<CancellationToken | Cancelable>): CancellationToken;
79 /**
80 * Returns a CancellationToken that becomes canceled when **all** of the provided tokens are canceled.
81 * @param tokens An iterable of CancellationToken objects.
82 */
83 static all(tokens: Iterable<CancellationToken | Cancelable>): CancellationToken;
84 /**
85 * Throws a CancelError if cancellation has been requested.
86 */
87 throwIfCancellationRequested(): void;
88 /**
89 * Registers a callback to execute when cancellation is requested.
90 *
91 * @param callback The callback to register.
92 */
93 register(callback: () => void): CancellationTokenRegistration;
94 [Cancelable.cancelSignal](): Cancelable & CancelSignal;
95}
96/**
97 * An error thrown when an operation is canceled.
98 */
99export declare class CancelError extends Error {
100 constructor(message?: string);
101}
102/**
103 * An object used to unregister a callback registered to a CancellationToken.
104 */
105export interface CancellationTokenRegistration extends Disposable {
106 /**
107 * Unregisters the callback
108 */
109 unregister(): void;
110}
111/**
112 * Describes a foreign cancellation primitive similar to the one provided by `vscode` for extensions.
113 */
114export interface VSCodeCancellationTokenLike {
115 readonly isCancellationRequested: boolean;
116 onCancellationRequested(listener: () => any): {
117 dispose(): any;
118 };
119}
120/**
121 * Describes a foreign cancellation primitive similar to the one used by the DOM.
122 */
123export interface AbortSignalLike {
124 readonly aborted: boolean;
125 addEventListener(type: "abort", callback: () => any): any;
126}
127/**
128 * An object that provides a CancellationToken that becomes cancelled when **all** of its
129 * containing tokens are canceled. This is similar to `CancellationToken.all`, except that you are
130 * able to add additional tokens.
131 */
132export declare class CancellationTokenCountdown {
133 private _addedCount;
134 private _signaledCount;
135 private _canBeSignaled;
136 private _source;
137 private _registrations;
138 constructor(iterable?: Iterable<CancellationToken | Cancelable>);
139 /**
140 * Gets the number of tokens added to the countdown.
141 */
142 get addedCount(): number;
143 /**
144 * Gets the number of tokens that have not yet been canceled.
145 */
146 get remainingCount(): number;
147 /**
148 * Gets the CancellationToken for the countdown.
149 */
150 get token(): CancellationToken;
151 /**
152 * Adds a CancellationToken to the countdown.
153 */
154 add(token: CancellationToken | Cancelable): this;
155 private _checkSignalState;
156}