##// END OF EJS Templates
Added CancelledError, fixed lint warnings
Added CancelledError, fixed lint warnings

File last commit:

r172:3969a8fb8049 release v1.4.6 default
r172:3969a8fb8049 release v1.4.6 default
Show More
Cancellation.ts
107 lines | 2.9 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / Cancellation.ts
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 import { CancellationAggregate } from "./CancellationAggregate";
cin
Added CancelledError, fixed lint warnings
r172 import { CancelledError } from "./CancelledError";
cin
changed the project structure
r49 import { ICancellation, IDestroyable } from "./interfaces";
cin
Added safe.delay...
r76 import { argumentNotNull, destroyed } from "./safe";
cin
changed the project structure
r49
export class Cancellation implements ICancellation {
private _reason: any;
cin
corrected code to support ts strict mode...
r115 private _cbs: Array<(e: any) => void> | undefined;
cin
changed the project structure
r49
cin
Added safe.delay...
r76 constructor(action: (cancel: (e?: any) => void) => void) {
cin
changed the project structure
r49 argumentNotNull(action, "action");
action(this._cancel.bind(this));
}
isSupported(): boolean {
return true;
}
throwIfRequested(): void {
if (this._reason)
throw this._reason;
}
isRequested(): boolean {
return !!this._reason;
}
register(cb: (e: any) => void): IDestroyable {
argumentNotNull(cb, "cb");
if (this._reason) {
cb(this._reason);
return destroyed;
} else {
if (!this._cbs)
this._cbs = [cb];
else
this._cbs.push(cb);
const me = this;
return {
destroy() {
me._unregister(cb);
}
};
}
}
cin
corrected code to support ts strict mode...
r115 private _unregister(cb: any) {
cin
changed the project structure
r49 if (this._cbs) {
const i = this._cbs.indexOf(cb);
if (i >= 0)
this._cbs.splice(i, 1);
}
}
cin
corrected code to support ts strict mode...
r115 private _cancel(reason: any) {
cin
changed the project structure
r49 if (this._reason)
return;
cin
Added CancelledError, fixed lint warnings
r172 this._reason = (reason = reason || new CancelledError(undefined, this));
cin
changed the project structure
r49
if (this._cbs) {
this._cbs.forEach(cb => cb(reason));
cin
corrected code to support ts strict mode...
r115 this._cbs = undefined;
cin
changed the project structure
r49 }
}
static readonly none: ICancellation = {
isSupported(): boolean {
return false;
},
throwIfRequested(): void {
},
isRequested(): boolean {
return false;
},
register(_cb: (e: any) => void): IDestroyable {
return destroyed;
}
};
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170
/**
* Combines multiple cancellation tokens to the single aggregated token.
cin
Added CancelledError, fixed lint warnings
r172 *
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 * Aggregated token will be considered as signalled when some tokens are
* signalled. The cancellation callback can be registered with the `register`
* method, it will be fired once with the first signalled token, all other
* tokens will be ignored.
cin
Added CancelledError, fixed lint warnings
r172 *
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 * The tokens which don't support cancellation are filtered out, if there are
* no tokens left in the list the method returns `Cancellation.none`.
cin
Added CancelledError, fixed lint warnings
r172 *
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 * @param args The list of cancellation tokens to combine
cin
Added CancelledError, fixed lint warnings
r172 * @returns Aggregated cancellation token
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 */
static combine(...args: ICancellation[]) {
const tokens = args.filter(ct => ct.isSupported());
return tokens.length > 1 ?
new CancellationAggregate(tokens) :
cin
Added CancelledError, fixed lint warnings
r172 tokens.length === 1 ? tokens[0] :
cin
Added Cancellation.combine(...tokens) method to combine multiple cancellation...
r170 this.none;
}
cin
changed the project structure
r49 }