Skip to content

Commit ad11e63

Browse files
committed
- Using private props in cancelable promise
- Updated snapshots - Removed unused imports
1 parent d7c153f commit ad11e63

File tree

5 files changed

+126
-96
lines changed

5 files changed

+126
-96
lines changed
Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1+
{{>header}}
2+
13
export class CancelablePromise<T> implements Promise<T> {
24
readonly [Symbol.toStringTag]: string;
35

4-
private _isPending: boolean = true;
5-
private _isCanceled: boolean = false;
6-
private _promise: Promise<T>;
7-
private _resolve?: (value: T | PromiseLike<T>) => void;
8-
private _reject?: (reason?: unknown) => void;
9-
private _cancelHandler?: () => void;
6+
#isPending: boolean;
7+
#isCanceled: boolean;
8+
readonly #cancelHandlers: (() => void)[];
9+
readonly #promise: Promise<T>;
10+
#resolve?: (value: T | PromiseLike<T>) => void;
11+
#reject?: (reason?: any) => void;
1012

11-
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
12-
this._promise = new Promise<T>((resolve, reject) => {
13-
this._resolve = resolve;
14-
this._reject = reject;
13+
constructor(executor: (
14+
resolve: (value: T | PromiseLike<T>) => void,
15+
reject: (reason?: any) => void,
16+
onCancel: (cancelHandler: () => void) => void) => void,
17+
) {
18+
this.#isPending = true;
19+
this.#isCanceled = false;
20+
this.#cancelHandlers = [];
21+
this.#promise = new Promise<T>((resolve, reject) => {
22+
this.#resolve = resolve;
23+
this.#reject = reject;
1524

1625
const onResolve = (value: T | PromiseLike<T>): void => {
17-
if (!this._isCanceled && this._resolve) {
18-
this._isPending = false;
19-
this._resolve(value);
26+
if (!this.#isCanceled) {
27+
this.#isPending = false;
28+
this.#resolve?.(value);
2029
}
2130
};
2231

23-
const onReject = (reason?: unknown): void => {
24-
if (this._reject) {
25-
this._isPending = false;
26-
this._reject(reason);
27-
}
32+
const onReject = (reason?: any): void => {
33+
this.#isPending = false;
34+
this.#reject?.(reason);
2835
};
2936

3037
const onCancel = (cancelHandler: () => void): void => {
31-
if (this._isPending) {
32-
this._cancelHandler = cancelHandler;
38+
if (this.#isPending) {
39+
this.#cancelHandlers.push(cancelHandler);
3340
}
3441
};
3542

@@ -41,33 +48,35 @@ export class CancelablePromise<T> implements Promise<T> {
4148
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
4249
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
4350
): Promise<TResult1 | TResult2> {
44-
return this._promise.then(onFulfilled, onRejected);
51+
return this.#promise.then(onFulfilled, onRejected);
4552
}
4653

4754
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
48-
return this._promise.catch(onRejected);
55+
return this.#promise.catch(onRejected);
4956
}
5057

5158
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
52-
return this._promise.finally(onFinally);
59+
return this.#promise.finally(onFinally);
5360
}
5461

55-
public cancel() {
56-
if (!this._isPending || this._isCanceled) {
62+
public cancel(): void {
63+
if (!this.#isPending || this.#isCanceled) {
5764
return;
5865
}
59-
this._isCanceled = true;
60-
if (this._cancelHandler && this._reject) {
66+
this.#isCanceled = true;
67+
if (this.#cancelHandlers.length) {
6168
try {
62-
this._cancelHandler();
69+
for (const cancelHandler of this.#cancelHandlers) {
70+
cancelHandler();
71+
}
6372
} catch (error) {
64-
this._reject(error);
73+
this.#reject?.(error);
6574
return;
6675
}
6776
}
6877
}
6978

7079
public get isCanceled(): boolean {
71-
return this._isCanceled;
80+
return this.#isCanceled;
7281
}
7382
}

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{>header}}
22

3-
import { AbortController, AbortSignal } from 'abort-controller';
3+
import { AbortController } from 'abort-controller';
44
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
55
import FormData from 'form-data';
66

src/templates/core/axios/sendRequest.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ async function sendRequest(options: ApiRequestOptions, url: string, onCancel: (c
22
const controller = new AbortController();
33
const formData = options.formData && getFormData(options.formData);
44
const data = formData || options.body;
5+
56
const config: AxiosRequestConfig = {
67
url,
78
data,

src/templates/core/node/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{>header}}
22

3-
import { AbortController, AbortSignal } from 'abort-controller';
3+
import { AbortController } from 'abort-controller';
44
import FormData from 'form-data';
55
import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch';
66
import { types } from 'util';

test/__snapshots__/index.spec.js.snap

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,46 @@ export type ApiResult = {
5555
`;
5656

5757
exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] = `
58-
"export class CancelablePromise<T> implements Promise<T> {
58+
"/* istanbul ignore file */
59+
/* tslint:disable */
60+
/* eslint-disable */
61+
export class CancelablePromise<T> implements Promise<T> {
5962
readonly [Symbol.toStringTag]: string;
6063

61-
private _isPending: boolean = true;
62-
private _isCanceled: boolean = false;
63-
private _promise: Promise<T>;
64-
private _resolve?: (value: T | PromiseLike<T>) => void;
65-
private _reject?: (reason?: unknown) => void;
66-
private _cancelHandler?: () => void;
67-
68-
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
69-
this._promise = new Promise<T>((resolve, reject) => {
70-
this._resolve = resolve;
71-
this._reject = reject;
64+
#isPending: boolean;
65+
#isCanceled: boolean;
66+
readonly #cancelHandlers: (() => void)[];
67+
readonly #promise: Promise<T>;
68+
#resolve?: (value: T | PromiseLike<T>) => void;
69+
#reject?: (reason?: any) => void;
70+
71+
constructor(executor: (
72+
resolve: (value: T | PromiseLike<T>) => void,
73+
reject: (reason?: any) => void,
74+
onCancel: (cancelHandler: () => void) => void) => void,
75+
) {
76+
this.#isPending = true;
77+
this.#isCanceled = false;
78+
this.#cancelHandlers = [];
79+
this.#promise = new Promise<T>((resolve, reject) => {
80+
this.#resolve = resolve;
81+
this.#reject = reject;
7282

7383
const onResolve = (value: T | PromiseLike<T>): void => {
74-
if (!this._isCanceled && this._resolve) {
75-
this._isPending = false;
76-
this._resolve(value);
84+
if (!this.#isCanceled) {
85+
this.#isPending = false;
86+
this.#resolve?.(value);
7787
}
7888
};
7989

80-
const onReject = (reason?: unknown): void => {
81-
if (this._reject) {
82-
this._isPending = false;
83-
this._reject(reason);
84-
}
90+
const onReject = (reason?: any): void => {
91+
this.#isPending = false;
92+
this.#reject?.(reason);
8593
};
8694

8795
const onCancel = (cancelHandler: () => void): void => {
88-
if (this._isPending) {
89-
this._cancelHandler = cancelHandler;
96+
if (this.#isPending) {
97+
this.#cancelHandlers.push(cancelHandler);
9098
}
9199
};
92100

@@ -98,34 +106,36 @@ exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] =
98106
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
99107
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
100108
): Promise<TResult1 | TResult2> {
101-
return this._promise.then(onFulfilled, onRejected);
109+
return this.#promise.then(onFulfilled, onRejected);
102110
}
103111

104112
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
105-
return this._promise.catch(onRejected);
113+
return this.#promise.catch(onRejected);
106114
}
107115

108116
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
109-
return this._promise.finally(onFinally);
117+
return this.#promise.finally(onFinally);
110118
}
111119

112-
public cancel() {
113-
if (!this._isPending || this._isCanceled) {
120+
public cancel(): void {
121+
if (!this.#isPending || this.#isCanceled) {
114122
return;
115123
}
116-
this._isCanceled = true;
117-
if (this._cancelHandler && this._reject) {
124+
this.#isCanceled = true;
125+
if (this.#cancelHandlers.length) {
118126
try {
119-
this._cancelHandler();
127+
for (const cancelHandler of this.#cancelHandlers) {
128+
cancelHandler();
129+
}
120130
} catch (error) {
121-
this._reject(error);
131+
this.#reject?.(error);
122132
return;
123133
}
124134
}
125135
}
126136

127137
public get isCanceled(): boolean {
128-
return this._isCanceled;
138+
return this.#isCanceled;
129139
}
130140
}"
131141
`;
@@ -2514,38 +2524,46 @@ export type ApiResult = {
25142524
`;
25152525

25162526
exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] = `
2517-
"export class CancelablePromise<T> implements Promise<T> {
2527+
"/* istanbul ignore file */
2528+
/* tslint:disable */
2529+
/* eslint-disable */
2530+
export class CancelablePromise<T> implements Promise<T> {
25182531
readonly [Symbol.toStringTag]: string;
25192532

2520-
private _isPending: boolean = true;
2521-
private _isCanceled: boolean = false;
2522-
private _promise: Promise<T>;
2523-
private _resolve?: (value: T | PromiseLike<T>) => void;
2524-
private _reject?: (reason?: unknown) => void;
2525-
private _cancelHandler?: () => void;
2526-
2527-
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
2528-
this._promise = new Promise<T>((resolve, reject) => {
2529-
this._resolve = resolve;
2530-
this._reject = reject;
2533+
#isPending: boolean;
2534+
#isCanceled: boolean;
2535+
readonly #cancelHandlers: (() => void)[];
2536+
readonly #promise: Promise<T>;
2537+
#resolve?: (value: T | PromiseLike<T>) => void;
2538+
#reject?: (reason?: any) => void;
2539+
2540+
constructor(executor: (
2541+
resolve: (value: T | PromiseLike<T>) => void,
2542+
reject: (reason?: any) => void,
2543+
onCancel: (cancelHandler: () => void) => void) => void,
2544+
) {
2545+
this.#isPending = true;
2546+
this.#isCanceled = false;
2547+
this.#cancelHandlers = [];
2548+
this.#promise = new Promise<T>((resolve, reject) => {
2549+
this.#resolve = resolve;
2550+
this.#reject = reject;
25312551

25322552
const onResolve = (value: T | PromiseLike<T>): void => {
2533-
if (!this._isCanceled && this._resolve) {
2534-
this._isPending = false;
2535-
this._resolve(value);
2553+
if (!this.#isCanceled) {
2554+
this.#isPending = false;
2555+
this.#resolve?.(value);
25362556
}
25372557
};
25382558

2539-
const onReject = (reason?: unknown): void => {
2540-
if (this._reject) {
2541-
this._isPending = false;
2542-
this._reject(reason);
2543-
}
2559+
const onReject = (reason?: any): void => {
2560+
this.#isPending = false;
2561+
this.#reject?.(reason);
25442562
};
25452563

25462564
const onCancel = (cancelHandler: () => void): void => {
2547-
if (this._isPending) {
2548-
this._cancelHandler = cancelHandler;
2565+
if (this.#isPending) {
2566+
this.#cancelHandlers.push(cancelHandler);
25492567
}
25502568
};
25512569

@@ -2557,34 +2575,36 @@ exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] =
25572575
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
25582576
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
25592577
): Promise<TResult1 | TResult2> {
2560-
return this._promise.then(onFulfilled, onRejected);
2578+
return this.#promise.then(onFulfilled, onRejected);
25612579
}
25622580

25632581
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
2564-
return this._promise.catch(onRejected);
2582+
return this.#promise.catch(onRejected);
25652583
}
25662584

25672585
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
2568-
return this._promise.finally(onFinally);
2586+
return this.#promise.finally(onFinally);
25692587
}
25702588

2571-
public cancel() {
2572-
if (!this._isPending || this._isCanceled) {
2589+
public cancel(): void {
2590+
if (!this.#isPending || this.#isCanceled) {
25732591
return;
25742592
}
2575-
this._isCanceled = true;
2576-
if (this._cancelHandler && this._reject) {
2593+
this.#isCanceled = true;
2594+
if (this.#cancelHandlers.length) {
25772595
try {
2578-
this._cancelHandler();
2596+
for (const cancelHandler of this.#cancelHandlers) {
2597+
cancelHandler();
2598+
}
25792599
} catch (error) {
2580-
this._reject(error);
2600+
this.#reject?.(error);
25812601
return;
25822602
}
25832603
}
25842604
}
25852605

25862606
public get isCanceled(): boolean {
2587-
return this._isCanceled;
2607+
return this.#isCanceled;
25882608
}
25892609
}"
25902610
`;

0 commit comments

Comments
 (0)