|
2 | 2 |
|
3 | 3 | export class CancelError extends Error {
|
4 | 4 |
|
5 |
| - constructor(message: string) { |
6 |
| - super(message); |
7 |
| - this.name = 'CancelError'; |
8 |
| - } |
9 |
| - |
10 |
| - public get isCancelled(): boolean { |
11 |
| - return true; |
12 |
| - } |
| 5 | + constructor(message: string) { |
| 6 | + super(message); |
| 7 | + this.name = 'CancelError'; |
| 8 | + } |
| 9 | + |
| 10 | + public get isCancelled(): boolean { |
| 11 | + return true; |
| 12 | + } |
13 | 13 | }
|
14 | 14 |
|
15 | 15 | export interface OnCancel {
|
16 |
| - readonly isResolved: boolean; |
17 |
| - readonly isRejected: boolean; |
18 |
| - readonly isCancelled: boolean; |
| 16 | + readonly isResolved: boolean; |
| 17 | + readonly isRejected: boolean; |
| 18 | + readonly isCancelled: boolean; |
19 | 19 |
|
20 |
| - (cancelHandler: () => void): void; |
| 20 | + (cancelHandler: () => void): void; |
21 | 21 | }
|
22 | 22 |
|
23 | 23 | export class CancelablePromise<T> implements Promise<T> {
|
24 |
| - readonly [Symbol.toStringTag]: string; |
25 |
| - |
26 |
| - #isResolved: boolean; |
27 |
| - #isRejected: boolean; |
28 |
| - #isCancelled: boolean; |
29 |
| - readonly #cancelHandlers: (() => void)[]; |
30 |
| - readonly #promise: Promise<T>; |
31 |
| - #resolve?: (value: T | PromiseLike<T>) => void; |
32 |
| - #reject?: (reason?: any) => void; |
33 |
| - |
34 |
| - constructor( |
35 |
| - executor: ( |
36 |
| - resolve: (value: T | PromiseLike<T>) => void, |
37 |
| - reject: (reason?: any) => void, |
38 |
| - onCancel: OnCancel |
39 |
| - ) => void |
40 |
| - ) { |
41 |
| - this.#isResolved = false; |
42 |
| - this.#isRejected = false; |
43 |
| - this.#isCancelled = false; |
44 |
| - this.#cancelHandlers = []; |
45 |
| - this.#promise = new Promise<T>((resolve, reject) => { |
46 |
| - this.#resolve = resolve; |
47 |
| - this.#reject = reject; |
48 |
| - |
49 |
| - const onResolve = (value: T | PromiseLike<T>): void => { |
50 |
| - if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
51 |
| - return; |
52 |
| - } |
53 |
| - this.#isResolved = true; |
54 |
| - this.#resolve?.(value); |
55 |
| - }; |
56 |
| - |
57 |
| - const onReject = (reason?: any): void => { |
58 |
| - if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
59 |
| - return; |
60 |
| - } |
61 |
| - this.#isRejected = true; |
62 |
| - this.#reject?.(reason); |
63 |
| - }; |
64 |
| - |
65 |
| - const onCancel = (cancelHandler: () => void): void => { |
66 |
| - if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
67 |
| - return; |
68 |
| - } |
69 |
| - this.#cancelHandlers.push(cancelHandler); |
70 |
| - }; |
71 |
| - |
72 |
| - Object.defineProperty(onCancel, 'isResolved', { |
73 |
| - get: (): boolean => this.#isResolved, |
74 |
| - }); |
75 |
| - |
76 |
| - Object.defineProperty(onCancel, 'isRejected', { |
77 |
| - get: (): boolean => this.#isRejected, |
78 |
| - }); |
79 |
| - |
80 |
| - Object.defineProperty(onCancel, 'isCancelled', { |
81 |
| - get: (): boolean => this.#isCancelled, |
82 |
| - }); |
83 |
| - |
84 |
| - return executor(onResolve, onReject, onCancel as OnCancel); |
85 |
| - }); |
86 |
| - } |
87 |
| - |
88 |
| - public then<TResult1 = T, TResult2 = never>( |
89 |
| - onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, |
90 |
| - onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null |
91 |
| - ): Promise<TResult1 | TResult2> { |
92 |
| - return this.#promise.then(onFulfilled, onRejected); |
93 |
| - } |
94 |
| - |
95 |
| - public catch<TResult = never>( |
96 |
| - onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null |
97 |
| - ): Promise<T | TResult> { |
98 |
| - return this.#promise.catch(onRejected); |
99 |
| - } |
100 |
| - |
101 |
| - public finally(onFinally?: (() => void) | null): Promise<T> { |
102 |
| - return this.#promise.finally(onFinally); |
103 |
| - } |
104 |
| - |
105 |
| - public cancel(): void { |
106 |
| - if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
107 |
| - return; |
108 |
| - } |
109 |
| - this.#isCancelled = true; |
110 |
| - if (this.#cancelHandlers.length) { |
111 |
| - try { |
112 |
| - for (const cancelHandler of this.#cancelHandlers) { |
113 |
| - cancelHandler(); |
114 |
| - } |
115 |
| - } catch (error) { |
116 |
| - console.warn('Cancellation threw an error', error); |
117 |
| - return; |
118 |
| - } |
119 |
| - } |
120 |
| - this.#cancelHandlers.length = 0; |
121 |
| - this.#reject?.(new CancelError('Request aborted')); |
122 |
| - } |
123 |
| - |
124 |
| - public get isCancelled(): boolean { |
125 |
| - return this.#isCancelled; |
126 |
| - } |
| 24 | + readonly [Symbol.toStringTag]: string; |
| 25 | + |
| 26 | + #isResolved: boolean; |
| 27 | + #isRejected: boolean; |
| 28 | + #isCancelled: boolean; |
| 29 | + readonly #cancelHandlers: (() => void)[]; |
| 30 | + readonly #promise: Promise<T>; |
| 31 | + #resolve?: (value: T | PromiseLike<T>) => void; |
| 32 | + #reject?: (reason?: any) => void; |
| 33 | + |
| 34 | + constructor( |
| 35 | + executor: ( |
| 36 | + resolve: (value: T | PromiseLike<T>) => void, |
| 37 | + reject: (reason?: any) => void, |
| 38 | + onCancel: OnCancel |
| 39 | + ) => void |
| 40 | + ) { |
| 41 | + this.#isResolved = false; |
| 42 | + this.#isRejected = false; |
| 43 | + this.#isCancelled = false; |
| 44 | + this.#cancelHandlers = []; |
| 45 | + this.#promise = new Promise<T>((resolve, reject) => { |
| 46 | + this.#resolve = resolve; |
| 47 | + this.#reject = reject; |
| 48 | + |
| 49 | + const onResolve = (value: T | PromiseLike<T>): void => { |
| 50 | + if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
| 51 | + return; |
| 52 | + } |
| 53 | + this.#isResolved = true; |
| 54 | + this.#resolve?.(value); |
| 55 | + }; |
| 56 | + |
| 57 | + const onReject = (reason?: any): void => { |
| 58 | + if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
| 59 | + return; |
| 60 | + } |
| 61 | + this.#isRejected = true; |
| 62 | + this.#reject?.(reason); |
| 63 | + }; |
| 64 | + |
| 65 | + const onCancel = (cancelHandler: () => void): void => { |
| 66 | + if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
| 67 | + return; |
| 68 | + } |
| 69 | + this.#cancelHandlers.push(cancelHandler); |
| 70 | + }; |
| 71 | + |
| 72 | + Object.defineProperty(onCancel, 'isResolved', { |
| 73 | + get: (): boolean => this.#isResolved, |
| 74 | + }); |
| 75 | + |
| 76 | + Object.defineProperty(onCancel, 'isRejected', { |
| 77 | + get: (): boolean => this.#isRejected, |
| 78 | + }); |
| 79 | + |
| 80 | + Object.defineProperty(onCancel, 'isCancelled', { |
| 81 | + get: (): boolean => this.#isCancelled, |
| 82 | + }); |
| 83 | + |
| 84 | + return executor(onResolve, onReject, onCancel as OnCancel); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + public then<TResult1 = T, TResult2 = never>( |
| 89 | + onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, |
| 90 | + onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null |
| 91 | + ): Promise<TResult1 | TResult2> { |
| 92 | + return this.#promise.then(onFulfilled, onRejected); |
| 93 | + } |
| 94 | + |
| 95 | + public catch<TResult = never>( |
| 96 | + onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null |
| 97 | + ): Promise<T | TResult> { |
| 98 | + return this.#promise.catch(onRejected); |
| 99 | + } |
| 100 | + |
| 101 | + public finally(onFinally?: (() => void) | null): Promise<T> { |
| 102 | + return this.#promise.finally(onFinally); |
| 103 | + } |
| 104 | + |
| 105 | + public cancel(): void { |
| 106 | + if (this.#isResolved || this.#isRejected || this.#isCancelled) { |
| 107 | + return; |
| 108 | + } |
| 109 | + this.#isCancelled = true; |
| 110 | + if (this.#cancelHandlers.length) { |
| 111 | + try { |
| 112 | + for (const cancelHandler of this.#cancelHandlers) { |
| 113 | + cancelHandler(); |
| 114 | + } |
| 115 | + } catch (error) { |
| 116 | + console.warn('Cancellation threw an error', error); |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + this.#cancelHandlers.length = 0; |
| 121 | + this.#reject?.(new CancelError('Request aborted')); |
| 122 | + } |
| 123 | + |
| 124 | + public get isCancelled(): boolean { |
| 125 | + return this.#isCancelled; |
| 126 | + } |
127 | 127 | }
|
0 commit comments