CancellationAggregate.ts
41 lines
| 1.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r170 | import { ICancellation, IDestroyable } from "./interfaces"; | ||
| export class CancellationAggregate implements ICancellation { | ||||
| private readonly _tokens: ICancellation[]; | ||||
| constructor(tokens: ICancellation[]) { | ||||
| this._tokens = tokens || []; | ||||
| } | ||||
| throwIfRequested() { | ||||
| this._tokens.forEach(ct => ct.throwIfRequested()); | ||||
| } | ||||
| isRequested() { | ||||
| return this._tokens.some(ct => ct.isRequested()); | ||||
| } | ||||
| isSupported() { | ||||
| return !!this._tokens.length; | ||||
| } | ||||
| register(cb: (e: any) => void): IDestroyable { | ||||
| let fired = false; | ||||
| const once = (e: any) => { | ||||
| if (!fired) { | ||||
| fired = true; | ||||
| destroy(); | ||||
| cb(e); | ||||
| } | ||||
|
|
r172 | }; | ||
|
|
r170 | |||
| const destroy = () => subscriptions | ||||
|
|
r172 | .splice(0, subscriptions.length) // empty array | ||
|
|
r170 | .forEach(subscription => subscription.destroy()); // cleanup | ||
|
|
r172 | const subscriptions = this._tokens.map(ct => ct.register(once)); | ||
|
|
r170 | return { | ||
| destroy | ||||
| }; | ||||
| } | ||||
| } | ||||
