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AAE;YAChB,cAAc,EAAE,CAAA;YAChB,MAAM,CAAC,CAAA;QACT,CAAC,CAAC,CAAA;IACN,CAAC;IAEO,yBAAyB;QAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAA;QAC3B,IAAI,MAAM,IAAI,IAAI,IAAI,IAAI,CAAC,mBAAmB,IAAI,IAAI,EAAE,CAAC;YACvD,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC,mBAAmB,CAAC,CAAA;YACzD,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAA;QACjC,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI,CAAC;YACH,IAAI,CAAC,yBAAyB,EAAE,CAAA;QAClC,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,kBAAkB,EAAE,CAAA;YACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;QACrB,CAAC;IACH,CAAC;CACF;AA9GD,8CA8GC;AAED,MAAa,iBAAkB,SAAQ,KAAK;IAC1C;QACE,KAAK,CAAC,WAAW,CAAC,CAAA;IACpB,CAAC;CACF;AAJD,8CAIC","sourcesContent":["import { EventEmitter } from \"events\"\n\nexport class CancellationToken extends EventEmitter {\n private parentCancelHandler: (() => any) | null = null\n\n private _cancelled: boolean\n get cancelled(): boolean {\n return this._cancelled || (this._parent != null && this._parent.cancelled)\n }\n\n private _parent: CancellationToken | null = null\n set parent(value: CancellationToken) {\n this.removeParentCancelHandler()\n\n this._parent = value\n this.parentCancelHandler = () => this.cancel()\n this._parent.onCancel(this.parentCancelHandler)\n }\n\n // babel cannot compile ... correctly for super calls\n constructor(parent?: CancellationToken) {\n super()\n\n this._cancelled = false\n if (parent != null) {\n this.parent = parent\n }\n }\n\n cancel() {\n this._cancelled = true\n this.emit(\"cancel\")\n }\n\n private onCancel(handler: () => any) {\n if (this.cancelled) {\n handler()\n } else {\n this.once(\"cancel\", handler)\n }\n }\n\n createPromise<R>(\n callback: (resolve: (thenableOrResult: R | PromiseLike<R>) => void, reject: (error: Error) => void, onCancel: (callback: () => void) => void) => void\n ): Promise<R> {\n if (this.cancelled) {\n return Promise.reject<R>(new CancellationError())\n }\n\n const finallyHandler = () => {\n if (cancelHandler != null) {\n try {\n this.removeListener(\"cancel\", cancelHandler)\n cancelHandler = null\n } catch (ignore) {\n // ignore\n }\n }\n }\n\n let cancelHandler: (() => void) | null = null\n return new Promise<R>((resolve, reject) => {\n let addedCancelHandler: (() => void) | null = null\n\n cancelHandler = () => {\n try {\n if (addedCancelHandler != null) {\n addedCancelHandler()\n addedCancelHandler = null\n }\n } finally {\n reject(new CancellationError())\n }\n }\n\n if (this.cancelled) {\n cancelHandler()\n return\n }\n\n this.onCancel(cancelHandler)\n\n callback(resolve, reject, (callback: () => void) => {\n addedCancelHandler = callback\n })\n })\n .then(it => {\n finallyHandler()\n return it\n })\n .catch((e: any) => {\n finallyHandler()\n throw e\n })\n }\n\n private removeParentCancelHandler() {\n const parent = this._parent\n if (parent != null && this.parentCancelHandler != null) {\n parent.removeListener(\"cancel\", this.parentCancelHandler)\n this.parentCancelHandler = null\n }\n }\n\n dispose() {\n try {\n this.removeParentCancelHandler()\n } finally {\n this.removeAllListeners()\n this._parent = null\n }\n }\n}\n\nexport class CancellationError extends Error {\n constructor() {\n super(\"cancelled\")\n }\n}\n"]} |