Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/publish/netlify/api/core/CancelablePromise.ts
6464 views
1
// deno-lint-ignore-file
2
/* istanbul ignore file */
3
/* tslint:disable */
4
/* eslint-disable */
5
export class CancelError extends Error {
6
constructor(message: string) {
7
super(message);
8
this.name = "CancelError";
9
}
10
11
public get isCancelled(): boolean {
12
return true;
13
}
14
}
15
16
export interface OnCancel {
17
readonly isResolved: boolean;
18
readonly isRejected: boolean;
19
readonly isCancelled: boolean;
20
21
(cancelHandler: () => void): void;
22
}
23
24
export class CancelablePromise<T> implements Promise<T> {
25
[Symbol.toStringTag] = "";
26
27
private _isResolved: boolean;
28
private _isRejected: boolean;
29
private _isCancelled: boolean;
30
private readonly _cancelHandlers: (() => void)[];
31
private readonly _promise: Promise<T>;
32
private _resolve?: (value: T | PromiseLike<T>) => void;
33
private _reject?: (reason?: any) => void;
34
35
constructor(
36
executor: (
37
resolve: (value: T | PromiseLike<T>) => void,
38
reject: (reason?: any) => void,
39
onCancel: OnCancel,
40
) => void,
41
) {
42
this._isResolved = false;
43
this._isRejected = false;
44
this._isCancelled = false;
45
this._cancelHandlers = [];
46
this._promise = new Promise<T>((resolve, reject) => {
47
this._resolve = resolve;
48
this._reject = reject;
49
50
const onResolve = (value: T | PromiseLike<T>): void => {
51
if (this._isResolved || this._isRejected || this._isCancelled) {
52
return;
53
}
54
this._isResolved = true;
55
this._resolve?.(value);
56
};
57
58
const onReject = (reason?: any): void => {
59
if (this._isResolved || this._isRejected || this._isCancelled) {
60
return;
61
}
62
this._isRejected = true;
63
this._reject?.(reason);
64
};
65
66
const onCancel = (cancelHandler: () => void): void => {
67
if (this._isResolved || this._isRejected || this._isCancelled) {
68
return;
69
}
70
this._cancelHandlers.push(cancelHandler);
71
};
72
73
Object.defineProperty(onCancel, "isResolved", {
74
get: (): boolean => this._isResolved,
75
});
76
77
Object.defineProperty(onCancel, "isRejected", {
78
get: (): boolean => this._isRejected,
79
});
80
81
Object.defineProperty(onCancel, "isCancelled", {
82
get: (): boolean => this._isCancelled,
83
});
84
85
return executor(onResolve, onReject, onCancel as OnCancel);
86
});
87
}
88
89
public then<TResult1 = T, TResult2 = never>(
90
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
91
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
92
): Promise<TResult1 | TResult2> {
93
return this._promise.then(onFulfilled, onRejected);
94
}
95
96
public catch<TResult = never>(
97
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
98
): Promise<T | TResult> {
99
return this._promise.catch(onRejected);
100
}
101
102
public finally(onFinally?: (() => void) | null): Promise<T> {
103
return this._promise.finally(onFinally);
104
}
105
106
public cancel(): void {
107
if (this._isResolved || this._isRejected || this._isCancelled) {
108
return;
109
}
110
this._isCancelled = true;
111
if (this._cancelHandlers.length) {
112
try {
113
for (const cancelHandler of this._cancelHandlers) {
114
cancelHandler();
115
}
116
} catch (error) {
117
console.warn("Cancellation threw an error", error);
118
return;
119
}
120
}
121
this._cancelHandlers.length = 0;
122
this._reject?.(new CancelError("Request aborted"));
123
}
124
125
public get isCancelled(): boolean {
126
return this._isCancelled;
127
}
128
}
129
130