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