/** * @module * This module contains the infrastructure for cooperative `Promise` cancellation. * @see {@link ICancellationToken} * @see {@link CancellationTokenSource} */ /** */ import { IDisposable } from "./disposable"; import { IConfigurablePromiseLike } from "./promiseLikeResolutionSource"; /** * An error that raises when the current promise has been rejected due to cancellation. * * @remarks * ES6 `Promise` does not have cancellation support, * so TPL uses `reject` with {@link PromiseCancelledError} to indicate a cancelled `Promise`. */ export declare class PromiseCancelledError extends Error { readonly name: string; constructor(message?: string); } /** * Represents subscribable a cancellation notification. * * Cancellation notification can be propagated by passing the same instance into cancellable callee async functions. * * @remarks * Like the concept of "Task cooperative cancellation" introduced in .NET Framework, * when the async function implementation wants to support cancellation, it should accept an * extra optional parameter of type {@link ICancellationToken}. * * In your function implementation, you should do at least one of the following * * * Check for cancellation with {@link ICancellationToken.isCancellationRequested} or {@link ICancellationToken.throwIfCancellationRequested}; * * Pass the cancellation token into the callee functions that supports cancellation with {@link ICancellationToken}; * * Subscribe for the cancellation event with {@link ICancellationToken.subscribe}. * * ```ts * async function doSomeWork(ct?: ICancellationToken) { * ct.throwIfCancellationRequested(); * // do something * await delay(1000, ct); * // do something else * } * ``` * @see {@link CancellationTokenSource} */ export interface ICancellationToken { /** * Whether the token owner has requested for cancellation. * * @remarks * Note that if this field returns `true`, * the `subscribe` callbacks are not guaranteed to have (all) executed. * * @see {@link ICancellationToken.throwIfCancellationRequested} */ readonly isCancellationRequested: boolean; /** * Gets a `Promise` that resolves when the token owner has requested for cancellation. */ readonly promiseLike: IConfigurablePromiseLike; /** * Throws a `PromiseCancelledError` if `isCancellationRequested` is `true`. * * @see {@link ICancellationToken.isCancellationRequested} */ throwIfCancellationRequested(): void; /** * Adds a callback function that is called when token owner has requested for cancellation. * * @param callback the function to be called *asynchronously* when the cancellation has been requested. * @returns an {@link IDisposable} used to unsubscribe the cancellation callback. * @remarks * if the cancellation token has already been cancelled when calling this function, * the callback is still guaranteed to be executed *asynchronously*. */ subscribe(callback: () => void): IDisposable; } /** * Represents an owner of {@link ICancellationToken} that can initiates the cancellation. */ export declare class CancellationTokenSource { private _cancellationToken; /** * @param cancelAfterDelay Automatically requests cancellation after the specified non-negative time in milliseconds. */ constructor(cancelAfterDelay?: number); /** Whether the cancellation has been requested. */ get isCancellationRequested(): boolean; /** * Initiates cancellation if it has not been requested yet. * Notifies the derived `ICancellationToken` of the cancellation. */ cancel(): void; /** * Initiates cancellation after the specified delay. * @param delay the non-negative time in milliseconds to wait before initiating the cancellation. */ cancelAfter(delay: number): void; /** Gets the derived `ICancellationToken` from this token source. */ get token(): ICancellationToken; /** * Gets a `CancellationTokenSource` that gets cancelled when any of the specified `ICancellationToken` gets cancelled. * @param cancellationTokens One or more underlying `ICancellationToken`. */ static race(...cancellationTokens: Array): CancellationTokenSource; } //# sourceMappingURL=cancellation.d.ts.map