@@ -23,13 +23,13 @@ export interface OnCancel {
2323export class CancelablePromise<T > implements Promise<T > {
2424readonly [Symbol.toStringTag]: string;
2525
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;
3333
3434constructor(
3535executor: (
@@ -38,47 +38,47 @@ export class CancelablePromise<T> implements Promise<T> {
3838onCancel: OnCancel
3939) => void
4040) {
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;
4848
4949const onResolve = (value: T | PromiseLike<T >): void => {
50- if (this.#isResolved  || this.#isRejected  || this.#isCancelled ) {
50+ if (this._isResolved  || this._isRejected  || this._isCancelled ) {
5151return;
5252}
53- this.#isResolved  = true;
54- this.#resolve ?.(value);
53+ this._isResolved  = true;
54+ this._resolve ?.(value);
5555};
5656
5757const onReject = (reason?: any): void => {
58- if (this.#isResolved  || this.#isRejected  || this.#isCancelled ) {
58+ if (this._isResolved  || this._isRejected  || this._isCancelled ) {
5959return;
6060}
61- this.#isRejected  = true;
62- this.#reject ?.(reason);
61+ this._isRejected  = true;
62+ this._reject ?.(reason);
6363};
6464
6565const onCancel = (cancelHandler: () => void): void => {
66- if (this.#isResolved  || this.#isRejected  || this.#isCancelled ) {
66+ if (this._isResolved  || this._isRejected  || this._isCancelled ) {
6767return;
6868}
69- this.#cancelHandlers .push(cancelHandler);
69+ this._cancelHandlers .push(cancelHandler);
7070};
7171
7272Object.defineProperty(onCancel, 'isResolved', {
73- get: (): boolean => this.#isResolved ,
73+ get: (): boolean => this._isResolved ,
7474});
7575
7676Object.defineProperty(onCancel, 'isRejected', {
77- get: (): boolean => this.#isRejected ,
77+ get: (): boolean => this._isRejected ,
7878});
7979
8080Object.defineProperty(onCancel, 'isCancelled', {
81- get: (): boolean => this.#isCancelled ,
81+ get: (): boolean => this._isCancelled ,
8282});
8383
8484return executor(onResolve, onReject, onCancel as OnCancel);
@@ -89,39 +89,39 @@ export class CancelablePromise<T> implements Promise<T> {
8989onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1 >) | null,
9090onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2 >) | null
9191): Promise<TResult1  | TResult2> {
92- return this.#promise .then(onFulfilled, onRejected);
92+ return this._promise .then(onFulfilled, onRejected);
9393}
9494
9595public catch<TResult  = never>(
9696onRejected?: ((reason: any) => TResult | PromiseLike<TResult >) | null
9797): Promise<T  | TResult> {
98- return this.#promise .catch(onRejected);
98+ return this._promise .catch(onRejected);
9999}
100100
101101public finally(onFinally?: (() => void) | null): Promise<T > {
102- return this.#promise .finally(onFinally);
102+ return this._promise .finally(onFinally);
103103}
104104
105105public cancel(): void {
106- if (this.#isResolved  || this.#isRejected  || this.#isCancelled ) {
106+ if (this._isResolved  || this._isRejected  || this._isCancelled ) {
107107return;
108108}
109- this.#isCancelled  = true;
110- if (this.#cancelHandlers .length) {
109+ this._isCancelled  = true;
110+ if (this._cancelHandlers .length) {
111111try {
112- for (const cancelHandler of this.#cancelHandlers ) {
112+ for (const cancelHandler of this._cancelHandlers ) {
113113cancelHandler();
114114}
115115} catch (error) {
116116console.warn('Cancellation threw an error', error);
117117return;
118118}
119119}
120- this.#cancelHandlers .length = 0;
121- this.#reject ?.(new CancelError('Request aborted'));
120+ this._cancelHandlers .length = 0;
121+ this._reject ?.(new CancelError('Request aborted'));
122122}
123123
124124public get isCancelled(): boolean {
125- return this.#isCancelled ;
125+ return this._isCancelled ;
126126}
127127}
0 commit comments