Comparing version
@@ -22,2 +22,4 @@ <!-- | ||
* [CancellationToken.from(token)](#cancellationtokenfromtoken) | ||
* [CancellationToken.race(tokens)](#cancellationtokenracetokens) | ||
* [CancellationToken.all(tokens)](#cancellationtokenalltokens) | ||
* [token.cancellationRequested](#tokencancellationrequested) | ||
@@ -31,2 +33,8 @@ * [token.canBeCanceled](#tokencanbecanceled) | ||
* [registration.unregister()](#registrationunregister) | ||
* [Class: CancellationTokenCountdown](#class-cancellationtokencountdown) | ||
* [new CancellationTokenCountdown(iterable?)](#new-cancellationtokencountdowniterable) | ||
* [countdown.addedCount](#countdownaddedcount) | ||
* [countdown.remainingCount](#countdownremainingcount) | ||
* [countdown.token](#countdowntoken) | ||
* [countdown.add(token)](#countdownaddtoken) | ||
* [Interface: VSCodeCancellationTokenLike](#interface-vscodecancellationtokenlike) | ||
@@ -86,2 +94,4 @@ * [Interface: AbortSignalLike](#interface-abortsignallike) | ||
static from(token: CancellationToken | VSCodeCancellationTokenLike | AbortSignalLike): CancellationToken; | ||
static race(tokens: Iterable<CancellationToken>): CancellationToken; | ||
static all(tokens: Iterable<CancellationToken>): CancellationToken; | ||
readonly cancellationRequested: boolean; | ||
@@ -108,4 +118,15 @@ readonly canBeCanceled: boolean; | ||
Adapts a CancellationToken-like primitive from a different library. | ||
* `token` <`any`> A foreign token to adapt. | ||
* Returns: [<CancellationToken>](#class-cancellationtoken) | ||
## CancellationToken.race(tokens) | ||
Returns a CancellationToken that becomes canceled when **any** of the provided tokens are canceled. | ||
* `tokens` [<Iterable>][Iterable] An iterable of tokens. | ||
* Returns: [<CancellationToken>](#class-cancellationtoken) | ||
## CancellationToken.all(tokens) | ||
Returns a CancellationToken that becomes canceled when **all** of the provided tokens are canceled. | ||
* `tokens` [<Iterable>][Iterable] An iterable of tokens. | ||
* Returns: [<CancellationToken>](#class-cancellationtoken) | ||
## token.cancellationRequested | ||
@@ -158,2 +179,38 @@ Gets a value indicating whether cancellation has been requested. | ||
# Class: CancellationTokenCountdown | ||
An object that provides a CancellationToken that becomes cancelled when **all** of its | ||
containing tokens are canceled. This is similar to `CancellationToken.all`, except that you are | ||
able to add additional tokens. | ||
### Syntax | ||
```ts | ||
export declare class CancellationTokenCountdown { | ||
constructor(iterable?: Iterable<CancellationToken>); | ||
readonly addedCount: number; | ||
readonly remainingCount: number; | ||
readonly token: CancellationToken; | ||
add(token: CancellationToken): this; | ||
} | ||
``` | ||
## new CancellationTokenCountdown(iterable?) | ||
Initializes a new instance of the CancellationTokenCountdown class. | ||
* `iterable` [<Iterable>][Iterable] An optional iterable of tokens to add to the countdown. | ||
## countdown.addedCount | ||
Gets the number of tokens added to the countdown. | ||
* Returns: [<Number>][Number] | ||
## countdown.remainingCount | ||
Gets the number of tokens that have not yet been canceled. | ||
* Returns: [<Number>][Number] | ||
## countdown.token | ||
Gets the CancellationToken for the countdown. | ||
* Returns: [<CancellationToken>](#class-cancellationtoken) | ||
## countdown.add(token) | ||
Adds a CancellationToken to the countdown. | ||
* Returns: [<CancellationTokenCountdown>](#class-cancellationtokencountdown) | ||
# Interface: VSCodeCancellationTokenLike | ||
@@ -165,3 +222,3 @@ Describes a foreign cancellation primitive similar to the one provided by `vscode` for extensions. | ||
export interface VSCodeCancellationTokenLike { | ||
isCancellationRequested: boolean; | ||
readonly isCancellationRequested: boolean; | ||
onCancellationRequested(listener: () => any): { dispose(): any; }; | ||
@@ -177,3 +234,3 @@ } | ||
export interface AbortSignalLike { | ||
aborted: boolean; | ||
readonly aborted: boolean; | ||
addEventListener(type: "abort", callback: () => any): any; | ||
@@ -185,2 +242,3 @@ } | ||
[Boolean]: http://ecma-international.org/ecma-262/6.0/index.html#sec-boolean-constructor | ||
[Number]: http://ecma-international.org/ecma-262/6.0/index.html#sec-number-constructor | ||
[Function]: http://ecma-international.org/ecma-262/6.0/index.html#sec-function-constructor | ||
@@ -187,0 +245,0 @@ [Error]: http://ecma-international.org/ecma-262/6.0/index.html#sec-error-constructor |
@@ -71,2 +71,12 @@ /*! ***************************************************************************** | ||
/** | ||
* Returns a CancellationToken that becomes canceled when **any** of the provided tokens are canceled. | ||
* @param tokens An iterable of CancellationToken objects. | ||
*/ | ||
static race(tokens: Iterable<CancellationToken>): CancellationToken; | ||
/** | ||
* Returns a CancellationToken that becomes canceled when **all** of the provided tokens are canceled. | ||
* @param tokens An iterable of CancellationToken objects. | ||
*/ | ||
static all(tokens: Iterable<CancellationToken>): CancellationToken; | ||
/** | ||
* Throws a CancelError if cancellation has been requested. | ||
@@ -101,3 +111,3 @@ */ | ||
export interface VSCodeCancellationTokenLike { | ||
isCancellationRequested: boolean; | ||
readonly isCancellationRequested: boolean; | ||
onCancellationRequested(listener: () => any): { | ||
@@ -111,4 +121,34 @@ dispose(): any; | ||
export interface AbortSignalLike { | ||
aborted: boolean; | ||
readonly aborted: boolean; | ||
addEventListener(type: "abort", callback: () => any): any; | ||
} | ||
/** | ||
* An object that provides a CancellationToken that becomes cancelled when **all** of its | ||
* containing tokens are canceled. This is similar to `CancellationToken.all`, except that you are | ||
* able to add additional tokens. | ||
*/ | ||
export declare class CancellationTokenCountdown { | ||
private _addedCount; | ||
private _signaledCount; | ||
private _canBeSignaled; | ||
private _source; | ||
private _registrations; | ||
constructor(iterable?: Iterable<CancellationToken>); | ||
/** | ||
* Gets the number of tokens added to the countdown. | ||
*/ | ||
readonly addedCount: number; | ||
/** | ||
* Gets the number of tokens that have not yet been canceled. | ||
*/ | ||
readonly remainingCount: number; | ||
/** | ||
* Gets the CancellationToken for the countdown. | ||
*/ | ||
readonly token: CancellationToken; | ||
/** | ||
* Adds a CancellationToken to the countdown. | ||
*/ | ||
add(token: CancellationToken): this; | ||
private _checkSignalState(); | ||
} |
@@ -236,2 +236,22 @@ "use strict"; | ||
/** | ||
* Returns a CancellationToken that becomes canceled when **any** of the provided tokens are canceled. | ||
* @param tokens An iterable of CancellationToken objects. | ||
*/ | ||
static race(tokens) { | ||
if (!utils_1.isIterable(tokens)) | ||
throw new TypeError("Object not iterable: iterable."); | ||
const tokensArray = Array.isArray(tokens) ? tokens : [...tokens]; | ||
return tokensArray.length > 0 ? new CancellationTokenSource(tokensArray).token : CancellationToken.none; | ||
} | ||
/** | ||
* Returns a CancellationToken that becomes canceled when **all** of the provided tokens are canceled. | ||
* @param tokens An iterable of CancellationToken objects. | ||
*/ | ||
static all(tokens) { | ||
if (!utils_1.isIterable(tokens)) | ||
throw new TypeError("Object not iterable: iterable."); | ||
const tokensArray = Array.isArray(tokens) ? tokens : [...tokens]; | ||
return tokensArray.length > 0 ? new CancellationTokenCountdown(tokensArray).token : CancellationToken.none; | ||
} | ||
/** | ||
* Throws a CancelError if cancellation has been requested. | ||
@@ -285,3 +305,77 @@ */ | ||
} | ||
/** | ||
* An object that provides a CancellationToken that becomes cancelled when **all** of its | ||
* containing tokens are canceled. This is similar to `CancellationToken.all`, except that you are | ||
* able to add additional tokens. | ||
*/ | ||
class CancellationTokenCountdown { | ||
constructor(iterable) { | ||
this._addedCount = 0; | ||
this._signaledCount = 0; | ||
this._canBeSignaled = false; | ||
this._source = new CancellationTokenSource(); | ||
this._registrations = []; | ||
if (!utils_1.isIterable(iterable, /*optional*/ true)) | ||
throw new TypeError("Object not iterable: iterable."); | ||
if (iterable) { | ||
for (const token of iterable) { | ||
this.add(token); | ||
} | ||
} | ||
this._canBeSignaled = true; | ||
this._checkSignalState(); | ||
} | ||
/** | ||
* Gets the number of tokens added to the countdown. | ||
*/ | ||
get addedCount() { return this._addedCount; } | ||
/** | ||
* Gets the number of tokens that have not yet been canceled. | ||
*/ | ||
get remainingCount() { return this._addedCount - this._signaledCount; } | ||
/** | ||
* Gets the CancellationToken for the countdown. | ||
*/ | ||
get token() { return this._source.token; } | ||
/** | ||
* Adds a CancellationToken to the countdown. | ||
*/ | ||
add(token) { | ||
if (!utils_1.isInstance(token, CancellationToken)) | ||
throw new TypeError("CancellationToken expected."); | ||
if (this._source._currentState !== "open") | ||
return this; | ||
if (token.cancellationRequested) { | ||
this._addedCount++; | ||
this._signaledCount++; | ||
this._checkSignalState(); | ||
} | ||
else if (token.canBeCanceled) { | ||
this._addedCount++; | ||
this._registrations.push(token.register(() => { | ||
this._signaledCount++; | ||
this._checkSignalState(); | ||
})); | ||
} | ||
return this; | ||
} | ||
_checkSignalState() { | ||
if (!this._canBeSignaled || this._signaledCount < this._addedCount) | ||
return; | ||
this._canBeSignaled = false; | ||
if (this._addedCount > 0) { | ||
try { | ||
for (const registration of this._registrations) { | ||
registration.unregister(); | ||
} | ||
} | ||
finally { | ||
this._registrations.length = 0; | ||
this._source.cancel(); | ||
} | ||
} | ||
} | ||
} | ||
exports.CancellationTokenCountdown = CancellationTokenCountdown; | ||
//# sourceMappingURL=cancellation.js.map |
{ | ||
"name": "prex", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Async coordination primitives and extensions on top of ES6 Promises", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
182115
5.83%2683
5.26%