Path: blob/main/src/publish/netlify/api/core/CancelablePromise.ts
6464 views
// deno-lint-ignore-file1/* istanbul ignore file */2/* tslint:disable */3/* eslint-disable */4export class CancelError extends Error {5constructor(message: string) {6super(message);7this.name = "CancelError";8}910public get isCancelled(): boolean {11return true;12}13}1415export interface OnCancel {16readonly isResolved: boolean;17readonly isRejected: boolean;18readonly isCancelled: boolean;1920(cancelHandler: () => void): void;21}2223export class CancelablePromise<T> implements Promise<T> {24[Symbol.toStringTag] = "";2526private _isResolved: boolean;27private _isRejected: boolean;28private _isCancelled: boolean;29private readonly _cancelHandlers: (() => void)[];30private readonly _promise: Promise<T>;31private _resolve?: (value: T | PromiseLike<T>) => void;32private _reject?: (reason?: any) => void;3334constructor(35executor: (36resolve: (value: T | PromiseLike<T>) => void,37reject: (reason?: any) => void,38onCancel: OnCancel,39) => void,40) {41this._isResolved = false;42this._isRejected = false;43this._isCancelled = false;44this._cancelHandlers = [];45this._promise = new Promise<T>((resolve, reject) => {46this._resolve = resolve;47this._reject = reject;4849const onResolve = (value: T | PromiseLike<T>): void => {50if (this._isResolved || this._isRejected || this._isCancelled) {51return;52}53this._isResolved = true;54this._resolve?.(value);55};5657const onReject = (reason?: any): void => {58if (this._isResolved || this._isRejected || this._isCancelled) {59return;60}61this._isRejected = true;62this._reject?.(reason);63};6465const onCancel = (cancelHandler: () => void): void => {66if (this._isResolved || this._isRejected || this._isCancelled) {67return;68}69this._cancelHandlers.push(cancelHandler);70};7172Object.defineProperty(onCancel, "isResolved", {73get: (): boolean => this._isResolved,74});7576Object.defineProperty(onCancel, "isRejected", {77get: (): boolean => this._isRejected,78});7980Object.defineProperty(onCancel, "isCancelled", {81get: (): boolean => this._isCancelled,82});8384return executor(onResolve, onReject, onCancel as OnCancel);85});86}8788public then<TResult1 = T, TResult2 = never>(89onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,90onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,91): Promise<TResult1 | TResult2> {92return this._promise.then(onFulfilled, onRejected);93}9495public catch<TResult = never>(96onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,97): Promise<T | TResult> {98return this._promise.catch(onRejected);99}100101public finally(onFinally?: (() => void) | null): Promise<T> {102return this._promise.finally(onFinally);103}104105public cancel(): void {106if (this._isResolved || this._isRejected || this._isCancelled) {107return;108}109this._isCancelled = true;110if (this._cancelHandlers.length) {111try {112for (const cancelHandler of this._cancelHandlers) {113cancelHandler();114}115} catch (error) {116console.warn("Cancellation threw an error", error);117return;118}119}120this._cancelHandlers.length = 0;121this._reject?.(new CancelError("Request aborted"));122}123124public get isCancelled(): boolean {125return this._isCancelled;126}127}128129130