UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

120 lines (118 loc) 4.2 kB
/** Signals to a CancellationToken that it should be canceled. */ export class CancellationTokenSource { timerHandle = 0; disposed = false; canceled = false; cancellationToken; /** * Initializes a new instance of the CancellationTokenSource class. * @param millisecondsDelay The optional time interval in milliseconds to wait before canceling. */ constructor(millisecondsDelay) { this.cancellationToken = new CancellationToken(this); if (millisecondsDelay) { this.cancelAfter(millisecondsDelay); } } /** Gets a value indicating whether cancellation has been requested. */ get isCancellationRequested() { return this.canceled; } /** Gets the CancellationToken associated with this CancellationTokenSource. */ get token() { return this.cancellationToken; } /** * Creates a CancellationTokenSource that will be in the canceled state when any of the source tokens * in the specified array are in the canceled state. * @param tokens An array that contains the cancellation token instances to observe. * @returns A CancellationTokenSource that is linked to the source tokens. */ static createLinkedTokenSource(...tokens) { const source = new CancellationTokenSource(); tokens.forEach((token) => { token.register(source.cancel.bind(this)); }); return source; } /** Communicates a request for cancellation. */ cancel() { this.canceled = true; this.cancelTimer(); } /** * Schedules a cancel operation on this CancellationTokenSource after the specified number of milliseconds. * @param millisecondsDelay The time span to wait before canceling this CancellationTokenSource. */ cancelAfter(millisecondsDelay) { this.cancelTimer(); this.timerHandle = setTimeout(this.cancel.bind(this), millisecondsDelay); } /** Releases all resources used by the current instance of the CancellationTokenSource class. */ dispose() { if (this.disposed) { return; } this.disposed = true; this.cancelTimer(); } cancelTimer() { if (this.timerHandle === 0) { return; } clearTimeout(this.timerHandle); this.timerHandle = 0; } } /** Propagates notification that operations should be canceled. */ export class CancellationToken { source; static none = null; parentCancel; callbacks = new Array(); /** * Initializes a new instance of the CancellationToken class. * @param source The underlying CancellationTokenSource. */ constructor(source) { this.source = source; // this is a 'bait & switch' because only a CTS is allowed to trigger cancellation const originalCancel = source.cancel; this.parentCancel = () => originalCancel.call(this.source); this.source.cancel = () => this.cancel(); } /** Returns an empty CancellationToken value. */ static get NONE() { if (CancellationToken.none === null) { CancellationToken.none = new CancellationTokenSource().token; } return CancellationToken.none; } /** Gets a value indicating whether cancellation has been requested for this token. */ get isCancellationRequested() { return this.source.isCancellationRequested; } /** Throws an Error if this token has had cancellation requested. */ throwIfCancellationRequested() { if (this.isCancellationRequested) { throw new Error('The operation was canceled.'); } } /** * Registers a function that will be called when this CancellationToken is canceled. * @param callback The function to be executed when the CancellationToken is canceled. */ register(callback) { this.callbacks.push(callback); } cancel() { if (this.isCancellationRequested) { return; } this.parentCancel(); this.callbacks.forEach((callback) => { callback(); }); } } //# sourceMappingURL=cancellation.js.map