Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
253 lines (211 loc) · 8.24 KB
/
Copy pathindex.ts
File metadata and controls
253 lines (211 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*!
Copyright 2019 Ron Buckton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Disposable } from "@esfx/disposable";
import /*#__INLINE__*/ { isFunction, isMissing, isObject } from "@esfx/internal-guards";
// Dynamically infer `ErrorOptions` based on whether the es2022 lib is loaded.
type ErrorOptions =
2 extends ConstructorParameters<typeof Error>["length"] ?
ConstructorParameters<typeof Error> extends [message?: string, options?: infer O] ?
NonNullable<O> :
never :
never;
type CancelErrorExtraArgs = [ErrorOptions] extends [never] ? [] : [options?: ErrorOptions];
export class CancelError extends Error {
static {
Object.defineProperty(this.prototype, "name", { configurable: true, writable: true, value: "CancelError" });
}
constructor(message?: string, options?: ErrorOptions);
constructor(message = "Operation was canceled", ...args: CancelErrorExtraArgs) {
super(message, ...args);
}
}
/**
* An object used to unsubscribe from a cancellation signal
*/
export interface CancelSubscription extends Disposable {
/**
* Unsubscribes from a cancellation signal.
*/
unsubscribe(): void;
}
export namespace CancelSubscription {
const CancelSubscriptionPrototype: Disposable = {
[Disposable.dispose](this: CancelSubscription) {
this.unsubscribe();
}
};
Object.defineProperty(CancelSubscriptionPrototype, Symbol.toStringTag, { configurable: true, value: "CancelSubscription" });
/**
* Creates a `CancelSubscription` object for an `unsubscribe` callback.
* @param unsubscribe The callback to execute when the `unsubscribe()` method is called.
*/
export function create(unsubscribe: () => void): CancelSubscription {
if (!isFunction(unsubscribe)) throw new TypeError("Function expected: unsubscribe");
return Object.setPrototypeOf({
unsubscribe() {
unsubscribe();
},
}, CancelSubscriptionPrototype);
}
}
/**
* An object that can be canceled from an external source.
*/
export interface Cancelable {
/**
* Gets the CancelSignal for this Cancelable.
*/
[Cancelable.cancelSignal](): CancelSignal;
}
export namespace Cancelable {
// #region Cancelable
/**
* A well-known symbol used to define a method to retrieve the `CancelSignal` for an object.
*/
export const cancelSignal = Symbol.for("@esfx/cancelable:Cancelable.cancelSignal");
// #endregion Cancelable
const CancelSignalPrototype: Cancelable = {
[Cancelable.cancelSignal](this: CancelableCancelSignal) {
return this;
}
};
Object.defineProperty(CancelSignalPrototype, Symbol.toStringTag, { configurable: true, value: "CancelSignal" });
const emptySubscription: CancelSubscription = CancelSubscription.create(() => { });
Object.freeze(emptySubscription);
const canceledReason = new CancelError();
Object.freeze(canceledReason);
/**
* A `Cancelable` that is already signaled.
*/
export const canceled: CancelableCancelSignal = Object.setPrototypeOf({
get signaled() {
return true;
},
get reason() {
return canceledReason;
},
subscribe(onSignaled: () => void) {
if (!isFunction(onSignaled)) throw new TypeError("Function expected: onSignaled");
onSignaled();
return emptySubscription;
}
}, CancelSignalPrototype);
Object.freeze(canceled);
/**
* A `Cancelable` that can never be signaled.
*/
export const none: CancelableCancelSignal = Object.setPrototypeOf({
get signaled() {
return false;
},
get reason() {
return undefined;
},
subscribe(onSignaled: () => void) {
if (!isFunction(onSignaled)) throw new TypeError("Function expected: onSignaled");
return emptySubscription;
},
}, CancelSignalPrototype);
Object.freeze(none);
/**
* Determines whether `cancelable` is in the signaled state.
*/
export function isSignaled(cancelable: Cancelable | null | undefined) {
if (isMissing(cancelable)) return false;
if (!hasInstance(cancelable)) throw new TypeError("Cancelable expected: cancelable");
if (cancelable === Cancelable.canceled) return true;
if (cancelable === Cancelable.none) return false;
return cancelable[Cancelable.cancelSignal]().signaled;
}
/**
* Gets the reason for cancelation.
*/
export function getReason(cancelable: Cancelable | null | undefined) {
if (isMissing(cancelable)) return undefined;
if (!hasInstance(cancelable)) throw new TypeError("Cancelable expected: cancelable");
if (cancelable === Cancelable.canceled) return canceledReason;
if (cancelable === Cancelable.none) return undefined;
return cancelable[Cancelable.cancelSignal]().reason;
}
/**
* Throws a `CancelError` exception if the provided `cancelable` is in the signaled state.
*/
export function throwIfSignaled(cancelable: Cancelable | null | undefined) {
if (isSignaled(cancelable)) {
throw getReason(cancelable) ?? new CancelError();
}
}
/**
* Subscribes to be notified when a `cancelable` becomes signaled.
*/
export function subscribe(cancelable: Cancelable | null | undefined, onSignaled: () => void) {
if (isMissing(cancelable)) return emptySubscription;
if (!hasInstance(cancelable)) throw new TypeError("Cancelable expected: cancelable");
if (cancelable === Cancelable.canceled) return onSignaled(), emptySubscription;
if (cancelable === Cancelable.none) return emptySubscription;
return cancelable[Cancelable.cancelSignal]().subscribe(onSignaled);
}
export const name = "Cancelable";
/**
* Determines whether a value is a `Cancelable` object.
*/
export function hasInstance(value: unknown): value is Cancelable {
return isObject(value)
&& Cancelable.cancelSignal in value;
}
}
/**
* An object that represents a cancellation signal.
*/
export interface CancelSignal {
/**
* Gets a value indicating whether cancellation was signaled.
*/
readonly signaled: boolean;
/**
* Gets the reason cancellation was signaled.
*/
readonly reason: unknown;
/**
* Subscribes to notifications for when the object becomes signaled.
*/
subscribe(onSignaled: () => void): CancelSubscription;
}
export interface CancelableCancelSignal extends CancelSignal {
[Cancelable.cancelSignal](): CancelableCancelSignal;
}
/**
* Represents an object that is a source for cancelation.
*/
export interface CancelableSource extends Cancelable {
/**
* Cancels the source, notifying the associated CancelSignal.
*/
[CancelableSource.cancel](reason?: unknown): void;
}
export namespace CancelableSource {
// #region Cancelable
export import cancelSignal = Cancelable.cancelSignal;
// #endregion Cancelable
// #region CancelableSource
export const cancel = Symbol.for("@esfx/cancelable:CancelableSource.cancel");
// #endregion CancelableSource
export const name = "CancelableSource";
/**
* Determines whether a value is a `CancelableSource` object.
*/
export function hasInstance(value: unknown): value is CancelableSource {
return Cancelable.hasInstance(value)
&& CancelableSource.cancel in value;
}
}