|
1 | | -export class CancelablePromise<T> extends Promise<T> { |
| 1 | +export class CancelablePromise<T> implements Promise<T> { |
| 2 | + readonly [Symbol.toStringTag]: string; |
2 | 3 |
|
3 | | - private readonly _cancel: (reason?: any) => void; |
| 4 | + private _isPending: boolean = true; |
| 5 | + private _isCanceled: boolean = false; |
| 6 | + private _promise: Promise<T>; |
| 7 | + private _resolve?: (value: T | PromiseLike<T>) => void; |
| 8 | + private _reject?: (reason?: unknown) => void; |
| 9 | + private _cancelHandler?: () => void; |
4 | 10 |
|
5 | | - constructor(executor: ( |
6 | | - resolve: (value: T | PromiseLike<T>) => void, |
7 | | - reject: () => void |
8 | | - cancel: () => void |
9 | | - ) => void) { |
10 | | - super(executor); |
| 11 | + constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) { |
| 12 | + this._promise = new Promise<T>((resolve, reject) => { |
| 13 | + this._resolve = resolve; |
| 14 | + this._reject = reject; |
| 15 | + |
| 16 | + const onResolve = (value: T | PromiseLike<T>): void => { |
| 17 | + if (!this._isCanceled && this._resolve) { |
| 18 | + this._isPending = false; |
| 19 | + this._resolve(value); |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + const onReject = (reason?: unknown): void => { |
| 24 | + if (this._reject) { |
| 25 | + this._isPending = false; |
| 26 | + this._reject(reason); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + const onCancel = (cancelHandler: () => void): void => { |
| 31 | + if (this._isPending) { |
| 32 | + this._cancelHandler = cancelHandler; |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + return executor(onResolve, onReject, onCancel); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + public then<TResult1 = T, TResult2 = never>( |
| 41 | + onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, |
| 42 | + onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null |
| 43 | + ): Promise<TResult1 | TResult2> { |
| 44 | + return this._promise.then(onFulfilled, onRejected); |
| 45 | + } |
| 46 | + |
| 47 | + public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> { |
| 48 | + return this._promise.catch(onRejected); |
| 49 | + } |
| 50 | + |
| 51 | + public finally(onFinally?: (() => void) | undefined | null): Promise<T> { |
| 52 | + return this._promise.finally(onFinally); |
11 | 53 | } |
12 | 54 |
|
13 | 55 | public cancel() { |
14 | | - // |
| 56 | + if (!this._isPending || this._isCanceled) { |
| 57 | + return; |
| 58 | + } |
| 59 | + this._isCanceled = true; |
| 60 | + if (this._cancelHandler && this._reject) { |
| 61 | + try { |
| 62 | + this._cancelHandler(); |
| 63 | + } catch (error) { |
| 64 | + this._reject(error); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public get isCanceled(): boolean { |
| 71 | + return this._isCanceled; |
15 | 72 | } |
16 | 73 | } |
0 commit comments