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