Cancellation.ts
66 lines
| 1.4 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r13 | import { ICancellation } from "./interfaces"; | |
|
|
r17 | import { argumentNotNull } from "./safe"; | |
|
|
r13 | ||
| export class Cancellation implements ICancellation { | |||
|
|
r17 | private _reason: any; | |
| private _cbs: Array<(e) => void>; | |||
| constructor(action: (cancel: (e) => void) => void) { | |||
| argumentNotNull(action, "action"); | |||
| action(this._cancel.bind(this)); | |||
| } | |||
|
|
r13 | isSupported(): boolean { | |
|
|
r17 | return true; | |
|
|
r13 | } | |
| throwIfRequested(): void { | |||
|
|
r17 | if (this._reason) | |
| throw this._reason; | |||
|
|
r13 | } | |
|
|
r17 | ||
|
|
r13 | isRequested(): boolean { | |
|
|
r17 | return !!this._reason; | |
|
|
r13 | } | |
|
|
r17 | register(cb: (e: any) => void): void { | |
| argumentNotNull(cb, "cb"); | |||
| if (this._reason) { | |||
| cb(this._reason); | |||
| } else { | |||
| if (!this._cbs) | |||
| this._cbs = [cb]; | |||
| else | |||
| this._cbs.push(cb); | |||
| } | |||
|
|
r13 | } | |
|
|
r17 | private _cancel(reason) { | |
| if (this._reason) | |||
| return; | |||
| this._reason = (reason = reason || new Error("Operation cancelled")); | |||
| if (this._cbs) { | |||
| this._cbs.forEach(cb => cb(reason)); | |||
| this._cbs = null; | |||
| } | |||
| } | |||
| static readonly none: ICancellation = { | |||
|
|
r13 | isSupported(): boolean { | |
| return false; | |||
| }, | |||
| throwIfRequested(): void { | |||
| }, | |||
|
|
r17 | ||
|
|
r13 | isRequested(): boolean { | |
| return false; | |||
| }, | |||
|
|
r17 | ||
| register(_cb: (e: any) => void): void { | |||
|
|
r13 | } | |
| }; | |||
| } |
