Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CancelablePromise #1465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 67 additions & 71 deletions frontend/src/client/core/CancelablePromise.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
export class CancelError extends Error {
constructor(message: string) {
super(message)
this.name = "CancelError"
super(message);
this.name = "CancelError";
}

public get isCancelled(): boolean {
return true
return true;
}
}

export interface OnCancel {
readonly isResolved: boolean
readonly isRejected: boolean
readonly isCancelled: boolean
readonly isResolved: boolean;
readonly isRejected: boolean;
readonly isCancelled: boolean;

(cancelHandler: () => void): void
(cancelHandler: () => void): void;
}

export class CancelablePromise<T> implements Promise<T> {
private _isResolved: boolean
private _isRejected: boolean
private _isCancelled: boolean
readonly cancelHandlers: (() => void)[]
readonly promise: Promise<T>
private _resolve?: (value: T | PromiseLike<T>) => void
private _reject?: (reason?: unknown) => void
private _isResolved: boolean = false;
private _isRejected: boolean = false;
private _isCancelled: boolean = false;
private readonly cancelHandlers: (() => void)[] = [];
private readonly promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;

constructor(
executor: (
Expand All @@ -33,94 +33,90 @@ export class CancelablePromise<T> implements Promise<T> {
onCancel: OnCancel,
) => void,
) {
this._isResolved = false
this._isRejected = false
this._isCancelled = 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._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isResolved = true
if (this._resolve) this._resolve(value)
}
this._resolve = resolve;
this._reject = reject;

const onReject = (reason?: unknown): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isRejected = true
if (this._reject) this._reject(reason)
}
const onCancel = this.createOnCancel();

const onCancel = (cancelHandler: () => void): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this.cancelHandlers.push(cancelHandler)
}
executor(this.createResolve(), this.createReject(), onCancel);
});
}

private createResolve(): (value: T | PromiseLike<T>) => void {
return (value: T | PromiseLike<T>): void => {
if (this.isFinalState()) return;
this._isResolved = true;
this._resolve?.(value);
};
}

Object.defineProperty(onCancel, "isResolved", {
get: (): boolean => this._isResolved,
})
private createReject(): (reason?: unknown) => void {
return (reason?: unknown): void => {
if (this.isFinalState()) return;
this._isRejected = true;
this._reject?.(reason);
};
}

private createOnCancel(): OnCancel {
const onCancel = ((cancelHandler: () => void): void => {
if (this.isFinalState()) return;
this.cancelHandlers.push(cancelHandler);
}) as OnCancel;

Object.defineProperty(onCancel, "isRejected", {
get: (): boolean => this._isRejected,
})
Object.defineProperties(onCancel, {
isResolved: { get: () => this._isResolved },
isRejected: { get: () => this._isRejected },
isCancelled: { get: () => this._isCancelled },
});

Object.defineProperty(onCancel, "isCancelled", {
get: (): boolean => this._isCancelled,
})
return onCancel;
}

return executor(onResolve, onReject, onCancel as OnCancel)
})
private isFinalState(): boolean {
return this._isResolved || this._isRejected || this._isCancelled;
}

get [Symbol.toStringTag]() {
return "Cancellable Promise"
return "Cancellable Promise";
}

public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: unknown) => 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: unknown) => TResult | PromiseLike<TResult>) | null,
): Promise<T | TResult> {
return this.promise.catch(onRejected)
return this.promise.catch(onRejected);
}

public finally(onFinally?: (() => void) | null): Promise<T> {
return this.promise.finally(onFinally)
return this.promise.finally(onFinally);
}

public cancel(): void {
if (this._isResolved || this._isRejected || this._isCancelled) {
return
}
this._isCancelled = true
if (this.cancelHandlers.length) {
try {
for (const cancelHandler of this.cancelHandlers) {
cancelHandler()
}
} catch (error) {
console.warn("Cancellation threw an error", error)
return
if (this.isFinalState()) return;

this._isCancelled = true;

try {
for (const cancelHandler of this.cancelHandlers) {
cancelHandler();
}
} catch (error) {
console.error("Error during cancellation:", error);
} finally {
this.cancelHandlers.length = 0;
this._reject?.(new CancelError("Request aborted"));
}
this.cancelHandlers.length = 0
if (this._reject) this._reject(new CancelError("Request aborted"))
}

public get isCancelled(): boolean {
return this._isCancelled
return this._isCancelled;
}
}
Loading