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C1C,EAAA,WAAA,GAAA;AACE,UAAM,WAAN;AACD;;AAHyC,C","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 }\n else {\n this.once(\"cancel\", handler)\n }\n }\n\n createPromise(callback: (resolve: (thenableOrResult?: R) => void, reject: (error: Error) => void, onCancel: (callback: () => void) => void) => void): Promise {\n if (this.cancelled) {\n return Promise.reject(new CancellationError())\n }\n\n const finallyHandler = () => {\n if (cancelHandler != null) {\n try {\n this.removeListener(\"cancel\", cancelHandler)\n cancelHandler = null\n }\n catch (ignore) {\n // ignore\n }\n }\n }\n\n let cancelHandler: (() => void) | null = null\n return new Promise((resolve, reject) => {\n let addedCancelHandler: (() => void) | null = null\n\n cancelHandler = () => {\n try {\n if (addedCancelHandler != null) {\n addedCancelHandler()\n addedCancelHandler = null\n }\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 => {\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 }\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}"],"sourceRoot":""}