- Using private props in cancelable promise

- Updated snapshots
- Removed unused imports
This commit is contained in:
Ferdi Koomen 2021-10-19 20:56:29 +02:00
parent d7c153ff55
commit ad11e63a57
5 changed files with 124 additions and 94 deletions

View File

@ -1,35 +1,42 @@
{{>header}}
export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]: string;
private _isPending: boolean = true;
private _isCanceled: boolean = false;
private _promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;
private _cancelHandler?: () => void;
#isPending: boolean;
#isCanceled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
this._promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
constructor(executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: (cancelHandler: () => void) => void) => void,
) {
this.#isPending = true;
this.#isCanceled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;
const onResolve = (value: T | PromiseLike<T>): void => {
if (!this._isCanceled && this._resolve) {
this._isPending = false;
this._resolve(value);
if (!this.#isCanceled) {
this.#isPending = false;
this.#resolve?.(value);
}
};
const onReject = (reason?: unknown): void => {
if (this._reject) {
this._isPending = false;
this._reject(reason);
}
const onReject = (reason?: any): void => {
this.#isPending = false;
this.#reject?.(reason);
};
const onCancel = (cancelHandler: () => void): void => {
if (this._isPending) {
this._cancelHandler = cancelHandler;
if (this.#isPending) {
this.#cancelHandlers.push(cancelHandler);
}
};
@ -41,33 +48,35 @@ export class CancelablePromise<T> implements Promise<T> {
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this._promise.then(onFulfilled, onRejected);
return this.#promise.then(onFulfilled, onRejected);
}
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
return this._promise.catch(onRejected);
return this.#promise.catch(onRejected);
}
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
return this._promise.finally(onFinally);
return this.#promise.finally(onFinally);
}
public cancel() {
if (!this._isPending || this._isCanceled) {
public cancel(): void {
if (!this.#isPending || this.#isCanceled) {
return;
}
this._isCanceled = true;
if (this._cancelHandler && this._reject) {
this.#isCanceled = true;
if (this.#cancelHandlers.length) {
try {
this._cancelHandler();
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler();
}
} catch (error) {
this._reject(error);
this.#reject?.(error);
return;
}
}
}
public get isCanceled(): boolean {
return this._isCanceled;
return this.#isCanceled;
}
}

View File

@ -1,6 +1,6 @@
{{>header}}
import { AbortController, AbortSignal } from 'abort-controller';
import { AbortController } from 'abort-controller';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import FormData from 'form-data';

View File

@ -2,6 +2,7 @@ async function sendRequest(options: ApiRequestOptions, url: string, onCancel: (c
const controller = new AbortController();
const formData = options.formData && getFormData(options.formData);
const data = formData || options.body;
const config: AxiosRequestConfig = {
url,
data,

View File

@ -1,6 +1,6 @@
{{>header}}
import { AbortController, AbortSignal } from 'abort-controller';
import { AbortController } from 'abort-controller';
import FormData from 'form-data';
import fetch, { BodyInit, Headers, RequestInit, Response } from 'node-fetch';
import { types } from 'util';

View File

@ -55,38 +55,46 @@ export type ApiResult = {
`;
exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] = `
"export class CancelablePromise<T> implements Promise<T> {
"/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]: string;
private _isPending: boolean = true;
private _isCanceled: boolean = false;
private _promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;
private _cancelHandler?: () => void;
#isPending: boolean;
#isCanceled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
this._promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
constructor(executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: (cancelHandler: () => void) => void) => void,
) {
this.#isPending = true;
this.#isCanceled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;
const onResolve = (value: T | PromiseLike<T>): void => {
if (!this._isCanceled && this._resolve) {
this._isPending = false;
this._resolve(value);
if (!this.#isCanceled) {
this.#isPending = false;
this.#resolve?.(value);
}
};
const onReject = (reason?: unknown): void => {
if (this._reject) {
this._isPending = false;
this._reject(reason);
}
const onReject = (reason?: any): void => {
this.#isPending = false;
this.#reject?.(reason);
};
const onCancel = (cancelHandler: () => void): void => {
if (this._isPending) {
this._cancelHandler = cancelHandler;
if (this.#isPending) {
this.#cancelHandlers.push(cancelHandler);
}
};
@ -98,34 +106,36 @@ exports[`v2 should generate: ./test/generated/v2/core/CancelablePromise.ts 1`] =
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this._promise.then(onFulfilled, onRejected);
return this.#promise.then(onFulfilled, onRejected);
}
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
return this._promise.catch(onRejected);
return this.#promise.catch(onRejected);
}
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
return this._promise.finally(onFinally);
return this.#promise.finally(onFinally);
}
public cancel() {
if (!this._isPending || this._isCanceled) {
public cancel(): void {
if (!this.#isPending || this.#isCanceled) {
return;
}
this._isCanceled = true;
if (this._cancelHandler && this._reject) {
this.#isCanceled = true;
if (this.#cancelHandlers.length) {
try {
this._cancelHandler();
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler();
}
} catch (error) {
this._reject(error);
this.#reject?.(error);
return;
}
}
}
public get isCanceled(): boolean {
return this._isCanceled;
return this.#isCanceled;
}
}"
`;
@ -2514,38 +2524,46 @@ export type ApiResult = {
`;
exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] = `
"export class CancelablePromise<T> implements Promise<T> {
"/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]: string;
private _isPending: boolean = true;
private _isCanceled: boolean = false;
private _promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;
private _cancelHandler?: () => void;
#isPending: boolean;
#isCanceled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;
constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void, onCancel: (cancelHandler: () => void) => void) => void) {
this._promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
constructor(executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: (cancelHandler: () => void) => void) => void,
) {
this.#isPending = true;
this.#isCanceled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;
const onResolve = (value: T | PromiseLike<T>): void => {
if (!this._isCanceled && this._resolve) {
this._isPending = false;
this._resolve(value);
if (!this.#isCanceled) {
this.#isPending = false;
this.#resolve?.(value);
}
};
const onReject = (reason?: unknown): void => {
if (this._reject) {
this._isPending = false;
this._reject(reason);
}
const onReject = (reason?: any): void => {
this.#isPending = false;
this.#reject?.(reason);
};
const onCancel = (cancelHandler: () => void): void => {
if (this._isPending) {
this._cancelHandler = cancelHandler;
if (this.#isPending) {
this.#cancelHandlers.push(cancelHandler);
}
};
@ -2557,34 +2575,36 @@ exports[`v3 should generate: ./test/generated/v3/core/CancelablePromise.ts 1`] =
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this._promise.then(onFulfilled, onRejected);
return this.#promise.then(onFulfilled, onRejected);
}
public catch<TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> {
return this._promise.catch(onRejected);
return this.#promise.catch(onRejected);
}
public finally(onFinally?: (() => void) | undefined | null): Promise<T> {
return this._promise.finally(onFinally);
return this.#promise.finally(onFinally);
}
public cancel() {
if (!this._isPending || this._isCanceled) {
public cancel(): void {
if (!this.#isPending || this.#isCanceled) {
return;
}
this._isCanceled = true;
if (this._cancelHandler && this._reject) {
this.#isCanceled = true;
if (this.#cancelHandlers.length) {
try {
this._cancelHandler();
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler();
}
} catch (error) {
this._reject(error);
this.#reject?.(error);
return;
}
}
}
public get isCanceled(): boolean {
return this._isCanceled;
return this.#isCanceled;
}
}"
`;