@@ -55,38 +55,46 @@ export type ApiResult = {
55
55
`;
56
56
57
57
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> {
59
62
readonly [Symbol.toStringTag]: string;
60
63
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;
72
82
73
83
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);
77
87
}
78
88
};
79
89
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);
85
93
};
86
94
87
95
const onCancel = (cancelHandler: () => void): void => {
88
- if (this._isPending ) {
89
- this._cancelHandler = cancelHandler;
96
+ if (this.#isPending ) {
97
+ this.#cancelHandlers.push( cancelHandler) ;
90
98
}
91
99
};
92
100
@@ -98,34 +106,36 @@ exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] =
98
106
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
99
107
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
100
108
): Promise<TResult1 | TResult2> {
101
- return this._promise .then(onFulfilled, onRejected);
109
+ return this.#promise .then(onFulfilled, onRejected);
102
110
}
103
111
104
112
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);
106
114
}
107
115
108
116
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
109
- return this._promise .finally(onFinally);
117
+ return this.#promise .finally(onFinally);
110
118
}
111
119
112
- public cancel() {
113
- if (!this._isPending || this._isCanceled ) {
120
+ public cancel(): void {
121
+ if (!this.#isPending || this.#isCanceled ) {
114
122
return;
115
123
}
116
- this._isCanceled = true;
117
- if (this._cancelHandler && this._reject ) {
124
+ this.#isCanceled = true;
125
+ if (this.#cancelHandlers.length ) {
118
126
try {
119
- this._cancelHandler();
127
+ for (const cancelHandler of this.#cancelHandlers) {
128
+ cancelHandler();
129
+ }
120
130
} catch (error) {
121
- this._reject (error);
131
+ this.#reject?. (error);
122
132
return;
123
133
}
124
134
}
125
135
}
126
136
127
137
public get isCanceled(): boolean {
128
- return this._isCanceled ;
138
+ return this.#isCanceled ;
129
139
}
130
140
}"
131
141
`;
@@ -2514,38 +2524,46 @@ export type ApiResult = {
2514
2524
`;
2515
2525
2516
2526
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> {
2518
2531
readonly [Symbol.toStringTag]: string;
2519
2532
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;
2531
2551
2532
2552
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);
2536
2556
}
2537
2557
};
2538
2558
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);
2544
2562
};
2545
2563
2546
2564
const onCancel = (cancelHandler: () => void): void => {
2547
- if (this._isPending ) {
2548
- this._cancelHandler = cancelHandler;
2565
+ if (this.#isPending ) {
2566
+ this.#cancelHandlers.push( cancelHandler) ;
2549
2567
}
2550
2568
};
2551
2569
@@ -2557,34 +2575,36 @@ exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] =
2557
2575
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
2558
2576
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
2559
2577
): Promise<TResult1 | TResult2> {
2560
- return this._promise .then(onFulfilled, onRejected);
2578
+ return this.#promise .then(onFulfilled, onRejected);
2561
2579
}
2562
2580
2563
2581
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);
2565
2583
}
2566
2584
2567
2585
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
2568
- return this._promise .finally(onFinally);
2586
+ return this.#promise .finally(onFinally);
2569
2587
}
2570
2588
2571
- public cancel() {
2572
- if (!this._isPending || this._isCanceled ) {
2589
+ public cancel(): void {
2590
+ if (!this.#isPending || this.#isCanceled ) {
2573
2591
return;
2574
2592
}
2575
- this._isCanceled = true;
2576
- if (this._cancelHandler && this._reject ) {
2593
+ this.#isCanceled = true;
2594
+ if (this.#cancelHandlers.length ) {
2577
2595
try {
2578
- this._cancelHandler();
2596
+ for (const cancelHandler of this.#cancelHandlers) {
2597
+ cancelHandler();
2598
+ }
2579
2599
} catch (error) {
2580
- this._reject (error);
2600
+ this.#reject?. (error);
2581
2601
return;
2582
2602
}
2583
2603
}
2584
2604
}
2585
2605
2586
2606
public get isCanceled(): boolean {
2587
- return this._isCanceled ;
2607
+ return this.#isCanceled ;
2588
2608
}
2589
2609
}"
2590
2610
`;
0 commit comments