@@ -36,47 +36,47 @@ export class CancelablePromise<T> implements Promise<T> {
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
- this._resolve ?.(value);
51
+ this.#isResolved = true;
52
+ this.#resolve ?.(value);
53
53
};
54
54
55
55
const onReject = (reason?: any): void => {
56
- if (this._isResolved || this._isRejected || this._isCancelled ) {
56
+ if (this.#isResolved || this.#isRejected || this.#isCancelled ) {
57
57
return;
58
58
}
59
- this._isRejected = true;
60
- this._reject ?.(reason);
59
+ this.#isRejected = true;
60
+ 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);
@@ -91,39 +91,39 @@ export class CancelablePromise<T> implements Promise<T> {
91
91
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1 >) | null,
92
92
onRejected?: ((reason: any) => 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
98
onRejected?: ((reason: any) => 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
- this._reject ?.(new CancelError('Request aborted'));
122
+ this.#cancelHandlers .length = 0;
123
+ 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