@wailsio/runtime 3.0.0-alpha.7 → 3.0.0-alpha.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{application.js → dist/application.js} +32 -46
- package/dist/browser.js +20 -0
- package/dist/callable.js +144 -0
- package/dist/calls.js +194 -0
- package/dist/cancellable.js +792 -0
- package/dist/clipboard.js +30 -0
- package/dist/contextmenu.js +80 -0
- package/dist/create.js +94 -0
- package/dist/dialogs.js +134 -0
- package/dist/drag.js +222 -0
- package/dist/event_types.js +232 -0
- package/dist/events.js +114 -0
- package/dist/flags.js +23 -0
- package/dist/index.js +42 -0
- package/dist/listener.js +44 -0
- package/dist/nanoid.js +38 -0
- package/dist/runtime.js +63 -0
- package/{screens.js → dist/screens.js} +38 -45
- package/dist/system.js +146 -0
- package/dist/utils.js +95 -0
- package/dist/window.js +570 -0
- package/dist/wml.js +179 -0
- package/package.json +62 -18
- package/types/application.d.ts +12 -0
- package/types/browser.d.ts +6 -0
- package/types/callable.d.ts +3 -0
- package/types/calls.d.ts +63 -0
- package/types/cancellable.d.ts +434 -0
- package/types/clipboard.d.ts +13 -0
- package/types/contextmenu.d.ts +1 -0
- package/types/create.d.ts +31 -0
- package/types/dialogs.d.ts +140 -0
- package/types/drag.d.ts +1 -0
- package/types/event_types.d.ts +221 -0
- package/types/events.d.ts +67 -0
- package/types/flags.d.ts +7 -0
- package/types/index.d.ts +16 -0
- package/types/listener.d.ts +9 -0
- package/types/nanoid.d.ts +1 -0
- package/types/runtime.d.ts +22 -0
- package/types/screens.d.ts +60 -0
- package/types/system.d.ts +92 -0
- package/types/utils.d.ts +21 -0
- package/types/window.d.ts +306 -0
- package/types/wml.d.ts +8 -0
- package/browser.js +0 -24
- package/calls.js +0 -123
- package/clipboard.js +0 -35
- package/contextmenu.js +0 -98
- package/dialogs.js +0 -138
- package/drag.js +0 -125
- package/events.js +0 -134
- package/flags.js +0 -63
- package/index.js +0 -56
- package/runtime.js +0 -122
- package/system.js +0 -122
- package/types.d.ts +0 -135
- package/window.js +0 -122
- package/wml.js +0 -151
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
/*
|
|
2
|
+
_ __ _ __
|
|
3
|
+
| | / /___ _(_) /____
|
|
4
|
+
| | /| / / __ `/ / / ___/
|
|
5
|
+
| |/ |/ / /_/ / / (__ )
|
|
6
|
+
|__/|__/\__,_/_/_/____/
|
|
7
|
+
The electron alternative for Go
|
|
8
|
+
(c) Lea Anthony 2019-present
|
|
9
|
+
*/
|
|
10
|
+
var _a;
|
|
11
|
+
import isCallable from "./callable.js";
|
|
12
|
+
/**
|
|
13
|
+
* Exception class that will be used as rejection reason
|
|
14
|
+
* in case a {@link CancellablePromise} is cancelled successfully.
|
|
15
|
+
*
|
|
16
|
+
* The value of the {@link name} property is the string `"CancelError"`.
|
|
17
|
+
* The value of the {@link cause} property is the cause passed to the cancel method, if any.
|
|
18
|
+
*/
|
|
19
|
+
export class CancelError extends Error {
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new `CancelError` instance.
|
|
22
|
+
* @param message - The error message.
|
|
23
|
+
* @param options - Options to be forwarded to the Error constructor.
|
|
24
|
+
*/
|
|
25
|
+
constructor(message, options) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.name = "CancelError";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Exception class that will be reported as an unhandled rejection
|
|
32
|
+
* in case a {@link CancellablePromise} rejects after being cancelled,
|
|
33
|
+
* or when the `oncancelled` callback throws or rejects.
|
|
34
|
+
*
|
|
35
|
+
* The value of the {@link name} property is the string `"CancelledRejectionError"`.
|
|
36
|
+
* The value of the {@link cause} property is the reason the promise rejected with.
|
|
37
|
+
*
|
|
38
|
+
* Because the original promise was cancelled,
|
|
39
|
+
* a wrapper promise will be passed to the unhandled rejection listener instead.
|
|
40
|
+
* The {@link promise} property holds a reference to the original promise.
|
|
41
|
+
*/
|
|
42
|
+
export class CancelledRejectionError extends Error {
|
|
43
|
+
/**
|
|
44
|
+
* Constructs a new `CancelledRejectionError` instance.
|
|
45
|
+
* @param promise - The promise that caused the error originally.
|
|
46
|
+
* @param reason - The rejection reason.
|
|
47
|
+
* @param info - An optional informative message specifying the circumstances in which the error was thrown.
|
|
48
|
+
* Defaults to the string `"Unhandled rejection in cancelled promise."`.
|
|
49
|
+
*/
|
|
50
|
+
constructor(promise, reason, info) {
|
|
51
|
+
super((info !== null && info !== void 0 ? info : "Unhandled rejection in cancelled promise.") + " Reason: " + errorMessage(reason), { cause: reason });
|
|
52
|
+
this.promise = promise;
|
|
53
|
+
this.name = "CancelledRejectionError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Private field names.
|
|
57
|
+
const barrierSym = Symbol("barrier");
|
|
58
|
+
const cancelImplSym = Symbol("cancelImpl");
|
|
59
|
+
const species = (_a = Symbol.species) !== null && _a !== void 0 ? _a : Symbol("speciesPolyfill");
|
|
60
|
+
/**
|
|
61
|
+
* A promise with an attached method for cancelling long-running operations (see {@link CancellablePromise#cancel}).
|
|
62
|
+
* Cancellation can optionally be bound to an {@link AbortSignal}
|
|
63
|
+
* for better composability (see {@link CancellablePromise#cancelOn}).
|
|
64
|
+
*
|
|
65
|
+
* Cancelling a pending promise will result in an immediate rejection
|
|
66
|
+
* with an instance of {@link CancelError} as reason,
|
|
67
|
+
* but whoever started the promise will be responsible
|
|
68
|
+
* for actually aborting the underlying operation.
|
|
69
|
+
* To this purpose, the constructor and all chaining methods
|
|
70
|
+
* accept optional cancellation callbacks.
|
|
71
|
+
*
|
|
72
|
+
* If a `CancellablePromise` still resolves after having been cancelled,
|
|
73
|
+
* the result will be discarded. If it rejects, the reason
|
|
74
|
+
* will be reported as an unhandled rejection,
|
|
75
|
+
* wrapped in a {@link CancelledRejectionError} instance.
|
|
76
|
+
* To facilitate the handling of cancellation requests,
|
|
77
|
+
* cancelled `CancellablePromise`s will _not_ report unhandled `CancelError`s
|
|
78
|
+
* whose `cause` field is the same as the one with which the current promise was cancelled.
|
|
79
|
+
*
|
|
80
|
+
* All usual promise methods are defined and return a `CancellablePromise`
|
|
81
|
+
* whose cancel method will cancel the parent operation as well, propagating the cancellation reason
|
|
82
|
+
* upwards through promise chains.
|
|
83
|
+
* Conversely, cancelling a promise will not automatically cancel dependent promises downstream:
|
|
84
|
+
* ```ts
|
|
85
|
+
* let root = new CancellablePromise((resolve, reject) => { ... });
|
|
86
|
+
* let child1 = root.then(() => { ... });
|
|
87
|
+
* let child2 = child1.then(() => { ... });
|
|
88
|
+
* let child3 = root.catch(() => { ... });
|
|
89
|
+
* child1.cancel(); // Cancels child1 and root, but not child2 or child3
|
|
90
|
+
* ```
|
|
91
|
+
* Cancelling a promise that has already settled is safe and has no consequence.
|
|
92
|
+
*
|
|
93
|
+
* The `cancel` method returns a promise that _always fulfills_
|
|
94
|
+
* after the whole chain has processed the cancel request
|
|
95
|
+
* and all attached callbacks up to that moment have run.
|
|
96
|
+
*
|
|
97
|
+
* All ES2024 promise methods (static and instance) are defined on CancellablePromise,
|
|
98
|
+
* but actual availability may vary with OS/webview version.
|
|
99
|
+
*
|
|
100
|
+
* In line with the proposal at https://github.com/tc39/proposal-rm-builtin-subclassing,
|
|
101
|
+
* `CancellablePromise` does not support transparent subclassing.
|
|
102
|
+
* Extenders should take care to provide their own method implementations.
|
|
103
|
+
* This might be reconsidered in case the proposal is retired.
|
|
104
|
+
*
|
|
105
|
+
* CancellablePromise is a wrapper around the DOM Promise object
|
|
106
|
+
* and is compliant with the [Promises/A+ specification](https://promisesaplus.com/)
|
|
107
|
+
* (it passes the [compliance suite](https://github.com/promises-aplus/promises-tests))
|
|
108
|
+
* if so is the underlying implementation.
|
|
109
|
+
*/
|
|
110
|
+
export class CancellablePromise extends Promise {
|
|
111
|
+
/**
|
|
112
|
+
* Creates a new `CancellablePromise`.
|
|
113
|
+
*
|
|
114
|
+
* @param executor - A callback used to initialize the promise. This callback is passed two arguments:
|
|
115
|
+
* a `resolve` callback used to resolve the promise with a value
|
|
116
|
+
* or the result of another promise (possibly cancellable),
|
|
117
|
+
* and a `reject` callback used to reject the promise with a provided reason or error.
|
|
118
|
+
* If the value provided to the `resolve` callback is a thenable _and_ cancellable object
|
|
119
|
+
* (it has a `then` _and_ a `cancel` method),
|
|
120
|
+
* cancellation requests will be forwarded to that object and the oncancelled will not be invoked anymore.
|
|
121
|
+
* If any one of the two callbacks is called _after_ the promise has been cancelled,
|
|
122
|
+
* the provided values will be cancelled and resolved as usual,
|
|
123
|
+
* but their results will be discarded.
|
|
124
|
+
* However, if the resolution process ultimately ends up in a rejection
|
|
125
|
+
* that is not due to cancellation, the rejection reason
|
|
126
|
+
* will be wrapped in a {@link CancelledRejectionError}
|
|
127
|
+
* and bubbled up as an unhandled rejection.
|
|
128
|
+
* @param oncancelled - It is the caller's responsibility to ensure that any operation
|
|
129
|
+
* started by the executor is properly halted upon cancellation.
|
|
130
|
+
* This optional callback can be used to that purpose.
|
|
131
|
+
* It will be called _synchronously_ with a cancellation cause
|
|
132
|
+
* when cancellation is requested, _after_ the promise has already rejected
|
|
133
|
+
* with a {@link CancelError}, but _before_
|
|
134
|
+
* any {@link then}/{@link catch}/{@link finally} callback runs.
|
|
135
|
+
* If the callback returns a thenable, the promise returned from {@link cancel}
|
|
136
|
+
* will only fulfill after the former has settled.
|
|
137
|
+
* Unhandled exceptions or rejections from the callback will be wrapped
|
|
138
|
+
* in a {@link CancelledRejectionError} and bubbled up as unhandled rejections.
|
|
139
|
+
* If the `resolve` callback is called before cancellation with a cancellable promise,
|
|
140
|
+
* cancellation requests on this promise will be diverted to that promise,
|
|
141
|
+
* and the original `oncancelled` callback will be discarded.
|
|
142
|
+
*/
|
|
143
|
+
constructor(executor, oncancelled) {
|
|
144
|
+
let resolve;
|
|
145
|
+
let reject;
|
|
146
|
+
super((res, rej) => { resolve = res; reject = rej; });
|
|
147
|
+
if (this.constructor[species] !== Promise) {
|
|
148
|
+
throw new TypeError("CancellablePromise does not support transparent subclassing. Please refrain from overriding the [Symbol.species] static property.");
|
|
149
|
+
}
|
|
150
|
+
let promise = {
|
|
151
|
+
promise: this,
|
|
152
|
+
resolve,
|
|
153
|
+
reject,
|
|
154
|
+
get oncancelled() { return oncancelled !== null && oncancelled !== void 0 ? oncancelled : null; },
|
|
155
|
+
set oncancelled(cb) { oncancelled = cb !== null && cb !== void 0 ? cb : undefined; }
|
|
156
|
+
};
|
|
157
|
+
const state = {
|
|
158
|
+
get root() { return state; },
|
|
159
|
+
resolving: false,
|
|
160
|
+
settled: false
|
|
161
|
+
};
|
|
162
|
+
// Setup cancellation system.
|
|
163
|
+
void Object.defineProperties(this, {
|
|
164
|
+
[barrierSym]: {
|
|
165
|
+
configurable: false,
|
|
166
|
+
enumerable: false,
|
|
167
|
+
writable: true,
|
|
168
|
+
value: null
|
|
169
|
+
},
|
|
170
|
+
[cancelImplSym]: {
|
|
171
|
+
configurable: false,
|
|
172
|
+
enumerable: false,
|
|
173
|
+
writable: false,
|
|
174
|
+
value: cancellerFor(promise, state)
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// Run the actual executor.
|
|
178
|
+
const rejector = rejectorFor(promise, state);
|
|
179
|
+
try {
|
|
180
|
+
executor(resolverFor(promise, state), rejector);
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
if (state.resolving) {
|
|
184
|
+
console.log("Unhandled exception in CancellablePromise executor.", err);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
rejector(err);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Cancels immediately the execution of the operation associated with this promise.
|
|
193
|
+
* The promise rejects with a {@link CancelError} instance as reason,
|
|
194
|
+
* with the {@link CancelError#cause} property set to the given argument, if any.
|
|
195
|
+
*
|
|
196
|
+
* Has no effect if called after the promise has already settled;
|
|
197
|
+
* repeated calls in particular are safe, but only the first one
|
|
198
|
+
* will set the cancellation cause.
|
|
199
|
+
*
|
|
200
|
+
* The `CancelError` exception _need not_ be handled explicitly _on the promises that are being cancelled:_
|
|
201
|
+
* cancelling a promise with no attached rejection handler does not trigger an unhandled rejection event.
|
|
202
|
+
* Therefore, the following idioms are all equally correct:
|
|
203
|
+
* ```ts
|
|
204
|
+
* new CancellablePromise((resolve, reject) => { ... }).cancel();
|
|
205
|
+
* new CancellablePromise((resolve, reject) => { ... }).then(...).cancel();
|
|
206
|
+
* new CancellablePromise((resolve, reject) => { ... }).then(...).catch(...).cancel();
|
|
207
|
+
* ```
|
|
208
|
+
* Whenever some cancelled promise in a chain rejects with a `CancelError`
|
|
209
|
+
* with the same cancellation cause as itself, the error will be discarded silently.
|
|
210
|
+
* However, the `CancelError` _will still be delivered_ to all attached rejection handlers
|
|
211
|
+
* added by {@link then} and related methods:
|
|
212
|
+
* ```ts
|
|
213
|
+
* let cancellable = new CancellablePromise((resolve, reject) => { ... });
|
|
214
|
+
* cancellable.then(() => { ... }).catch(console.log);
|
|
215
|
+
* cancellable.cancel(); // A CancelError is printed to the console.
|
|
216
|
+
* ```
|
|
217
|
+
* If the `CancelError` is not handled downstream by the time it reaches
|
|
218
|
+
* a _non-cancelled_ promise, it _will_ trigger an unhandled rejection event,
|
|
219
|
+
* just like normal rejections would:
|
|
220
|
+
* ```ts
|
|
221
|
+
* let cancellable = new CancellablePromise((resolve, reject) => { ... });
|
|
222
|
+
* let chained = cancellable.then(() => { ... }).then(() => { ... }); // No catch...
|
|
223
|
+
* cancellable.cancel(); // Unhandled rejection event on chained!
|
|
224
|
+
* ```
|
|
225
|
+
* Therefore, it is important to either cancel whole promise chains from their tail,
|
|
226
|
+
* as shown in the correct idioms above, or take care of handling errors everywhere.
|
|
227
|
+
*
|
|
228
|
+
* @returns A cancellable promise that _fulfills_ after the cancel callback (if any)
|
|
229
|
+
* and all handlers attached up to the call to cancel have run.
|
|
230
|
+
* If the cancel callback returns a thenable, the promise returned by `cancel`
|
|
231
|
+
* will also wait for that thenable to settle.
|
|
232
|
+
* This enables callers to wait for the cancelled operation to terminate
|
|
233
|
+
* without being forced to handle potential errors at the call site.
|
|
234
|
+
* ```ts
|
|
235
|
+
* cancellable.cancel().then(() => {
|
|
236
|
+
* // Cleanup finished, it's safe to do something else.
|
|
237
|
+
* }, (err) => {
|
|
238
|
+
* // Unreachable: the promise returned from cancel will never reject.
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
* Note that the returned promise will _not_ handle implicitly any rejection
|
|
242
|
+
* that might have occurred already in the cancelled chain.
|
|
243
|
+
* It will just track whether registered handlers have been executed or not.
|
|
244
|
+
* Therefore, unhandled rejections will never be silently handled by calling cancel.
|
|
245
|
+
*/
|
|
246
|
+
cancel(cause) {
|
|
247
|
+
return new CancellablePromise((resolve) => {
|
|
248
|
+
// INVARIANT: the result of this[cancelImplSym] and the barrier do not ever reject.
|
|
249
|
+
// Unfortunately macOS High Sierra does not support Promise.allSettled.
|
|
250
|
+
Promise.all([
|
|
251
|
+
this[cancelImplSym](new CancelError("Promise cancelled.", { cause })),
|
|
252
|
+
currentBarrier(this)
|
|
253
|
+
]).then(() => resolve(), () => resolve());
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Binds promise cancellation to the abort event of the given {@link AbortSignal}.
|
|
258
|
+
* If the signal has already aborted, the promise will be cancelled immediately.
|
|
259
|
+
* When either condition is verified, the cancellation cause will be set
|
|
260
|
+
* to the signal's abort reason (see {@link AbortSignal#reason}).
|
|
261
|
+
*
|
|
262
|
+
* Has no effect if called (or if the signal aborts) _after_ the promise has already settled.
|
|
263
|
+
* Only the first signal to abort will set the cancellation cause.
|
|
264
|
+
*
|
|
265
|
+
* For more details about the cancellation process,
|
|
266
|
+
* see {@link cancel} and the `CancellablePromise` constructor.
|
|
267
|
+
*
|
|
268
|
+
* This method enables `await`ing cancellable promises without having
|
|
269
|
+
* to store them for future cancellation, e.g.:
|
|
270
|
+
* ```ts
|
|
271
|
+
* await longRunningOperation().cancelOn(signal);
|
|
272
|
+
* ```
|
|
273
|
+
* instead of:
|
|
274
|
+
* ```ts
|
|
275
|
+
* let promiseToBeCancelled = longRunningOperation();
|
|
276
|
+
* await promiseToBeCancelled;
|
|
277
|
+
* ```
|
|
278
|
+
*
|
|
279
|
+
* @returns This promise, for method chaining.
|
|
280
|
+
*/
|
|
281
|
+
cancelOn(signal) {
|
|
282
|
+
if (signal.aborted) {
|
|
283
|
+
void this.cancel(signal.reason);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
signal.addEventListener('abort', () => void this.cancel(signal.reason), { capture: true });
|
|
287
|
+
}
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Attaches callbacks for the resolution and/or rejection of the `CancellablePromise`.
|
|
292
|
+
*
|
|
293
|
+
* The optional `oncancelled` argument will be invoked when the returned promise is cancelled,
|
|
294
|
+
* with the same semantics as the `oncancelled` argument of the constructor.
|
|
295
|
+
* When the parent promise rejects or is cancelled, the `onrejected` callback will run,
|
|
296
|
+
* _even after the returned promise has been cancelled:_
|
|
297
|
+
* in that case, should it reject or throw, the reason will be wrapped
|
|
298
|
+
* in a {@link CancelledRejectionError} and bubbled up as an unhandled rejection.
|
|
299
|
+
*
|
|
300
|
+
* @param onfulfilled The callback to execute when the Promise is resolved.
|
|
301
|
+
* @param onrejected The callback to execute when the Promise is rejected.
|
|
302
|
+
* @returns A `CancellablePromise` for the completion of whichever callback is executed.
|
|
303
|
+
* The returned promise is hooked up to propagate cancellation requests up the chain, but not down:
|
|
304
|
+
*
|
|
305
|
+
* - if the parent promise is cancelled, the `onrejected` handler will be invoked with a `CancelError`
|
|
306
|
+
* and the returned promise _will resolve regularly_ with its result;
|
|
307
|
+
* - conversely, if the returned promise is cancelled, _the parent promise is cancelled too;_
|
|
308
|
+
* the `onrejected` handler will still be invoked with the parent's `CancelError`,
|
|
309
|
+
* but its result will be discarded
|
|
310
|
+
* and the returned promise will reject with a `CancelError` as well.
|
|
311
|
+
*
|
|
312
|
+
* The promise returned from {@link cancel} will fulfill only after all attached handlers
|
|
313
|
+
* up the entire promise chain have been run.
|
|
314
|
+
*
|
|
315
|
+
* If either callback returns a cancellable promise,
|
|
316
|
+
* cancellation requests will be diverted to it,
|
|
317
|
+
* and the specified `oncancelled` callback will be discarded.
|
|
318
|
+
*/
|
|
319
|
+
then(onfulfilled, onrejected, oncancelled) {
|
|
320
|
+
if (!(this instanceof CancellablePromise)) {
|
|
321
|
+
throw new TypeError("CancellablePromise.prototype.then called on an invalid object.");
|
|
322
|
+
}
|
|
323
|
+
// NOTE: TypeScript's built-in type for then is broken,
|
|
324
|
+
// as it allows specifying an arbitrary TResult1 != T even when onfulfilled is not a function.
|
|
325
|
+
// We cannot fix it if we want to CancellablePromise to implement PromiseLike<T>.
|
|
326
|
+
if (!isCallable(onfulfilled)) {
|
|
327
|
+
onfulfilled = identity;
|
|
328
|
+
}
|
|
329
|
+
if (!isCallable(onrejected)) {
|
|
330
|
+
onrejected = thrower;
|
|
331
|
+
}
|
|
332
|
+
if (onfulfilled === identity && onrejected == thrower) {
|
|
333
|
+
// Shortcut for trivial arguments.
|
|
334
|
+
return new CancellablePromise((resolve) => resolve(this));
|
|
335
|
+
}
|
|
336
|
+
const barrier = {};
|
|
337
|
+
this[barrierSym] = barrier;
|
|
338
|
+
return new CancellablePromise((resolve, reject) => {
|
|
339
|
+
void super.then((value) => {
|
|
340
|
+
var _a;
|
|
341
|
+
if (this[barrierSym] === barrier) {
|
|
342
|
+
this[barrierSym] = null;
|
|
343
|
+
}
|
|
344
|
+
(_a = barrier.resolve) === null || _a === void 0 ? void 0 : _a.call(barrier);
|
|
345
|
+
try {
|
|
346
|
+
resolve(onfulfilled(value));
|
|
347
|
+
}
|
|
348
|
+
catch (err) {
|
|
349
|
+
reject(err);
|
|
350
|
+
}
|
|
351
|
+
}, (reason) => {
|
|
352
|
+
var _a;
|
|
353
|
+
if (this[barrierSym] === barrier) {
|
|
354
|
+
this[barrierSym] = null;
|
|
355
|
+
}
|
|
356
|
+
(_a = barrier.resolve) === null || _a === void 0 ? void 0 : _a.call(barrier);
|
|
357
|
+
try {
|
|
358
|
+
resolve(onrejected(reason));
|
|
359
|
+
}
|
|
360
|
+
catch (err) {
|
|
361
|
+
reject(err);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
}, async (cause) => {
|
|
365
|
+
//cancelled = true;
|
|
366
|
+
try {
|
|
367
|
+
return oncancelled === null || oncancelled === void 0 ? void 0 : oncancelled(cause);
|
|
368
|
+
}
|
|
369
|
+
finally {
|
|
370
|
+
await this.cancel(cause);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Attaches a callback for only the rejection of the Promise.
|
|
376
|
+
*
|
|
377
|
+
* The optional `oncancelled` argument will be invoked when the returned promise is cancelled,
|
|
378
|
+
* with the same semantics as the `oncancelled` argument of the constructor.
|
|
379
|
+
* When the parent promise rejects or is cancelled, the `onrejected` callback will run,
|
|
380
|
+
* _even after the returned promise has been cancelled:_
|
|
381
|
+
* in that case, should it reject or throw, the reason will be wrapped
|
|
382
|
+
* in a {@link CancelledRejectionError} and bubbled up as an unhandled rejection.
|
|
383
|
+
*
|
|
384
|
+
* It is equivalent to
|
|
385
|
+
* ```ts
|
|
386
|
+
* cancellablePromise.then(undefined, onrejected, oncancelled);
|
|
387
|
+
* ```
|
|
388
|
+
* and the same caveats apply.
|
|
389
|
+
*
|
|
390
|
+
* @returns A Promise for the completion of the callback.
|
|
391
|
+
* Cancellation requests on the returned promise
|
|
392
|
+
* will propagate up the chain to the parent promise,
|
|
393
|
+
* but not in the other direction.
|
|
394
|
+
*
|
|
395
|
+
* The promise returned from {@link cancel} will fulfill only after all attached handlers
|
|
396
|
+
* up the entire promise chain have been run.
|
|
397
|
+
*
|
|
398
|
+
* If `onrejected` returns a cancellable promise,
|
|
399
|
+
* cancellation requests will be diverted to it,
|
|
400
|
+
* and the specified `oncancelled` callback will be discarded.
|
|
401
|
+
* See {@link then} for more details.
|
|
402
|
+
*/
|
|
403
|
+
catch(onrejected, oncancelled) {
|
|
404
|
+
return this.then(undefined, onrejected, oncancelled);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Attaches a callback that is invoked when the CancellablePromise is settled (fulfilled or rejected). The
|
|
408
|
+
* resolved value cannot be accessed or modified from the callback.
|
|
409
|
+
* The returned promise will settle in the same state as the original one
|
|
410
|
+
* after the provided callback has completed execution,
|
|
411
|
+
* unless the callback throws or returns a rejecting promise,
|
|
412
|
+
* in which case the returned promise will reject as well.
|
|
413
|
+
*
|
|
414
|
+
* The optional `oncancelled` argument will be invoked when the returned promise is cancelled,
|
|
415
|
+
* with the same semantics as the `oncancelled` argument of the constructor.
|
|
416
|
+
* Once the parent promise settles, the `onfinally` callback will run,
|
|
417
|
+
* _even after the returned promise has been cancelled:_
|
|
418
|
+
* in that case, should it reject or throw, the reason will be wrapped
|
|
419
|
+
* in a {@link CancelledRejectionError} and bubbled up as an unhandled rejection.
|
|
420
|
+
*
|
|
421
|
+
* This method is implemented in terms of {@link then} and the same caveats apply.
|
|
422
|
+
* It is polyfilled, hence available in every OS/webview version.
|
|
423
|
+
*
|
|
424
|
+
* @returns A Promise for the completion of the callback.
|
|
425
|
+
* Cancellation requests on the returned promise
|
|
426
|
+
* will propagate up the chain to the parent promise,
|
|
427
|
+
* but not in the other direction.
|
|
428
|
+
*
|
|
429
|
+
* The promise returned from {@link cancel} will fulfill only after all attached handlers
|
|
430
|
+
* up the entire promise chain have been run.
|
|
431
|
+
*
|
|
432
|
+
* If `onfinally` returns a cancellable promise,
|
|
433
|
+
* cancellation requests will be diverted to it,
|
|
434
|
+
* and the specified `oncancelled` callback will be discarded.
|
|
435
|
+
* See {@link then} for more details.
|
|
436
|
+
*/
|
|
437
|
+
finally(onfinally, oncancelled) {
|
|
438
|
+
if (!(this instanceof CancellablePromise)) {
|
|
439
|
+
throw new TypeError("CancellablePromise.prototype.finally called on an invalid object.");
|
|
440
|
+
}
|
|
441
|
+
if (!isCallable(onfinally)) {
|
|
442
|
+
return this.then(onfinally, onfinally, oncancelled);
|
|
443
|
+
}
|
|
444
|
+
return this.then((value) => CancellablePromise.resolve(onfinally()).then(() => value), (reason) => CancellablePromise.resolve(onfinally()).then(() => { throw reason; }), oncancelled);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* We use the `[Symbol.species]` static property, if available,
|
|
448
|
+
* to disable the built-in automatic subclassing features from {@link Promise}.
|
|
449
|
+
* It is critical for performance reasons that extenders do not override this.
|
|
450
|
+
* Once the proposal at https://github.com/tc39/proposal-rm-builtin-subclassing
|
|
451
|
+
* is either accepted or retired, this implementation will have to be revised accordingly.
|
|
452
|
+
*
|
|
453
|
+
* @ignore
|
|
454
|
+
* @internal
|
|
455
|
+
*/
|
|
456
|
+
static get [species]() {
|
|
457
|
+
return Promise;
|
|
458
|
+
}
|
|
459
|
+
static all(values) {
|
|
460
|
+
let collected = Array.from(values);
|
|
461
|
+
const promise = collected.length === 0
|
|
462
|
+
? CancellablePromise.resolve(collected)
|
|
463
|
+
: new CancellablePromise((resolve, reject) => {
|
|
464
|
+
void Promise.all(collected).then(resolve, reject);
|
|
465
|
+
}, (cause) => cancelAll(promise, collected, cause));
|
|
466
|
+
return promise;
|
|
467
|
+
}
|
|
468
|
+
static allSettled(values) {
|
|
469
|
+
let collected = Array.from(values);
|
|
470
|
+
const promise = collected.length === 0
|
|
471
|
+
? CancellablePromise.resolve(collected)
|
|
472
|
+
: new CancellablePromise((resolve, reject) => {
|
|
473
|
+
void Promise.allSettled(collected).then(resolve, reject);
|
|
474
|
+
}, (cause) => cancelAll(promise, collected, cause));
|
|
475
|
+
return promise;
|
|
476
|
+
}
|
|
477
|
+
static any(values) {
|
|
478
|
+
let collected = Array.from(values);
|
|
479
|
+
const promise = collected.length === 0
|
|
480
|
+
? CancellablePromise.resolve(collected)
|
|
481
|
+
: new CancellablePromise((resolve, reject) => {
|
|
482
|
+
void Promise.any(collected).then(resolve, reject);
|
|
483
|
+
}, (cause) => cancelAll(promise, collected, cause));
|
|
484
|
+
return promise;
|
|
485
|
+
}
|
|
486
|
+
static race(values) {
|
|
487
|
+
let collected = Array.from(values);
|
|
488
|
+
const promise = new CancellablePromise((resolve, reject) => {
|
|
489
|
+
void Promise.race(collected).then(resolve, reject);
|
|
490
|
+
}, (cause) => cancelAll(promise, collected, cause));
|
|
491
|
+
return promise;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Creates a new cancelled CancellablePromise for the provided cause.
|
|
495
|
+
*
|
|
496
|
+
* @group Static Methods
|
|
497
|
+
*/
|
|
498
|
+
static cancel(cause) {
|
|
499
|
+
const p = new CancellablePromise(() => { });
|
|
500
|
+
p.cancel(cause);
|
|
501
|
+
return p;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Creates a new CancellablePromise that cancels
|
|
505
|
+
* after the specified timeout, with the provided cause.
|
|
506
|
+
*
|
|
507
|
+
* If the {@link AbortSignal.timeout} factory method is available,
|
|
508
|
+
* it is used to base the timeout on _active_ time rather than _elapsed_ time.
|
|
509
|
+
* Otherwise, `timeout` falls back to {@link setTimeout}.
|
|
510
|
+
*
|
|
511
|
+
* @group Static Methods
|
|
512
|
+
*/
|
|
513
|
+
static timeout(milliseconds, cause) {
|
|
514
|
+
const promise = new CancellablePromise(() => { });
|
|
515
|
+
if (AbortSignal && typeof AbortSignal === 'function' && AbortSignal.timeout && typeof AbortSignal.timeout === 'function') {
|
|
516
|
+
AbortSignal.timeout(milliseconds).addEventListener('abort', () => void promise.cancel(cause));
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
setTimeout(() => void promise.cancel(cause), milliseconds);
|
|
520
|
+
}
|
|
521
|
+
return promise;
|
|
522
|
+
}
|
|
523
|
+
static sleep(milliseconds, value) {
|
|
524
|
+
return new CancellablePromise((resolve) => {
|
|
525
|
+
setTimeout(() => resolve(value), milliseconds);
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Creates a new rejected CancellablePromise for the provided reason.
|
|
530
|
+
*
|
|
531
|
+
* @group Static Methods
|
|
532
|
+
*/
|
|
533
|
+
static reject(reason) {
|
|
534
|
+
return new CancellablePromise((_, reject) => reject(reason));
|
|
535
|
+
}
|
|
536
|
+
static resolve(value) {
|
|
537
|
+
if (value instanceof CancellablePromise) {
|
|
538
|
+
// Optimise for cancellable promises.
|
|
539
|
+
return value;
|
|
540
|
+
}
|
|
541
|
+
return new CancellablePromise((resolve) => resolve(value));
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Creates a new CancellablePromise and returns it in an object, along with its resolve and reject functions
|
|
545
|
+
* and a getter/setter for the cancellation callback.
|
|
546
|
+
*
|
|
547
|
+
* This method is polyfilled, hence available in every OS/webview version.
|
|
548
|
+
*
|
|
549
|
+
* @group Static Methods
|
|
550
|
+
*/
|
|
551
|
+
static withResolvers() {
|
|
552
|
+
let result = { oncancelled: null };
|
|
553
|
+
result.promise = new CancellablePromise((resolve, reject) => {
|
|
554
|
+
result.resolve = resolve;
|
|
555
|
+
result.reject = reject;
|
|
556
|
+
}, (cause) => { var _a; (_a = result.oncancelled) === null || _a === void 0 ? void 0 : _a.call(result, cause); });
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Returns a callback that implements the cancellation algorithm for the given cancellable promise.
|
|
562
|
+
* The promise returned from the resulting function does not reject.
|
|
563
|
+
*/
|
|
564
|
+
function cancellerFor(promise, state) {
|
|
565
|
+
let cancellationPromise = undefined;
|
|
566
|
+
return (reason) => {
|
|
567
|
+
if (!state.settled) {
|
|
568
|
+
state.settled = true;
|
|
569
|
+
state.reason = reason;
|
|
570
|
+
promise.reject(reason);
|
|
571
|
+
// Attach an error handler that ignores this specific rejection reason and nothing else.
|
|
572
|
+
// In theory, a sane underlying implementation at this point
|
|
573
|
+
// should always reject with our cancellation reason,
|
|
574
|
+
// hence the handler will never throw.
|
|
575
|
+
void Promise.prototype.then.call(promise.promise, undefined, (err) => {
|
|
576
|
+
if (err !== reason) {
|
|
577
|
+
throw err;
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
// If reason is not set, the promise resolved regularly, hence we must not call oncancelled.
|
|
582
|
+
// If oncancelled is unset, no need to go any further.
|
|
583
|
+
if (!state.reason || !promise.oncancelled) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
cancellationPromise = new Promise((resolve) => {
|
|
587
|
+
try {
|
|
588
|
+
resolve(promise.oncancelled(state.reason.cause));
|
|
589
|
+
}
|
|
590
|
+
catch (err) {
|
|
591
|
+
Promise.reject(new CancelledRejectionError(promise.promise, err, "Unhandled exception in oncancelled callback."));
|
|
592
|
+
}
|
|
593
|
+
}).catch((reason) => {
|
|
594
|
+
Promise.reject(new CancelledRejectionError(promise.promise, reason, "Unhandled rejection in oncancelled callback."));
|
|
595
|
+
});
|
|
596
|
+
// Unset oncancelled to prevent repeated calls.
|
|
597
|
+
promise.oncancelled = null;
|
|
598
|
+
return cancellationPromise;
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Returns a callback that implements the resolution algorithm for the given cancellable promise.
|
|
603
|
+
*/
|
|
604
|
+
function resolverFor(promise, state) {
|
|
605
|
+
return (value) => {
|
|
606
|
+
if (state.resolving) {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
state.resolving = true;
|
|
610
|
+
if (value === promise.promise) {
|
|
611
|
+
if (state.settled) {
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
state.settled = true;
|
|
615
|
+
promise.reject(new TypeError("A promise cannot be resolved with itself."));
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (value != null && (typeof value === 'object' || typeof value === 'function')) {
|
|
619
|
+
let then;
|
|
620
|
+
try {
|
|
621
|
+
then = value.then;
|
|
622
|
+
}
|
|
623
|
+
catch (err) {
|
|
624
|
+
state.settled = true;
|
|
625
|
+
promise.reject(err);
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
if (isCallable(then)) {
|
|
629
|
+
try {
|
|
630
|
+
let cancel = value.cancel;
|
|
631
|
+
if (isCallable(cancel)) {
|
|
632
|
+
const oncancelled = (cause) => {
|
|
633
|
+
Reflect.apply(cancel, value, [cause]);
|
|
634
|
+
};
|
|
635
|
+
if (state.reason) {
|
|
636
|
+
// If already cancelled, propagate cancellation.
|
|
637
|
+
// The promise returned from the canceller algorithm does not reject
|
|
638
|
+
// so it can be discarded safely.
|
|
639
|
+
void cancellerFor(Object.assign(Object.assign({}, promise), { oncancelled }), state)(state.reason);
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
promise.oncancelled = oncancelled;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
catch (_a) { }
|
|
647
|
+
const newState = {
|
|
648
|
+
root: state.root,
|
|
649
|
+
resolving: false,
|
|
650
|
+
get settled() { return this.root.settled; },
|
|
651
|
+
set settled(value) { this.root.settled = value; },
|
|
652
|
+
get reason() { return this.root.reason; }
|
|
653
|
+
};
|
|
654
|
+
const rejector = rejectorFor(promise, newState);
|
|
655
|
+
try {
|
|
656
|
+
Reflect.apply(then, value, [resolverFor(promise, newState), rejector]);
|
|
657
|
+
}
|
|
658
|
+
catch (err) {
|
|
659
|
+
rejector(err);
|
|
660
|
+
}
|
|
661
|
+
return; // IMPORTANT!
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
if (state.settled) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
state.settled = true;
|
|
668
|
+
promise.resolve(value);
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Returns a callback that implements the rejection algorithm for the given cancellable promise.
|
|
673
|
+
*/
|
|
674
|
+
function rejectorFor(promise, state) {
|
|
675
|
+
return (reason) => {
|
|
676
|
+
if (state.resolving) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
state.resolving = true;
|
|
680
|
+
if (state.settled) {
|
|
681
|
+
try {
|
|
682
|
+
if (reason instanceof CancelError && state.reason instanceof CancelError && Object.is(reason.cause, state.reason.cause)) {
|
|
683
|
+
// Swallow late rejections that are CancelErrors whose cancellation cause is the same as ours.
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
catch (_a) { }
|
|
688
|
+
void Promise.reject(new CancelledRejectionError(promise.promise, reason));
|
|
689
|
+
}
|
|
690
|
+
else {
|
|
691
|
+
state.settled = true;
|
|
692
|
+
promise.reject(reason);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Cancels all values in an array that look like cancellable thenables.
|
|
698
|
+
* Returns a promise that fulfills once all cancellation procedures for the given values have settled.
|
|
699
|
+
*/
|
|
700
|
+
function cancelAll(parent, values, cause) {
|
|
701
|
+
const results = [];
|
|
702
|
+
for (const value of values) {
|
|
703
|
+
let cancel;
|
|
704
|
+
try {
|
|
705
|
+
if (!isCallable(value.then)) {
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
cancel = value.cancel;
|
|
709
|
+
if (!isCallable(cancel)) {
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
catch (_a) {
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
let result;
|
|
717
|
+
try {
|
|
718
|
+
result = Reflect.apply(cancel, value, [cause]);
|
|
719
|
+
}
|
|
720
|
+
catch (err) {
|
|
721
|
+
Promise.reject(new CancelledRejectionError(parent, err, "Unhandled exception in cancel method."));
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
if (!result) {
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
results.push((result instanceof Promise ? result : Promise.resolve(result)).catch((reason) => {
|
|
728
|
+
Promise.reject(new CancelledRejectionError(parent, reason, "Unhandled rejection in cancel method."));
|
|
729
|
+
}));
|
|
730
|
+
}
|
|
731
|
+
return Promise.all(results);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Returns its argument.
|
|
735
|
+
*/
|
|
736
|
+
function identity(x) {
|
|
737
|
+
return x;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Throws its argument.
|
|
741
|
+
*/
|
|
742
|
+
function thrower(reason) {
|
|
743
|
+
throw reason;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Attempts various strategies to convert an error to a string.
|
|
747
|
+
*/
|
|
748
|
+
function errorMessage(err) {
|
|
749
|
+
try {
|
|
750
|
+
if (err instanceof Error || typeof err !== 'object' || err.toString !== Object.prototype.toString) {
|
|
751
|
+
return "" + err;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
catch (_a) { }
|
|
755
|
+
try {
|
|
756
|
+
return JSON.stringify(err);
|
|
757
|
+
}
|
|
758
|
+
catch (_b) { }
|
|
759
|
+
try {
|
|
760
|
+
return Object.prototype.toString.call(err);
|
|
761
|
+
}
|
|
762
|
+
catch (_c) { }
|
|
763
|
+
return "<could not convert error to string>";
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Gets the current barrier promise for the given cancellable promise. If necessary, initialises the barrier.
|
|
767
|
+
*/
|
|
768
|
+
function currentBarrier(promise) {
|
|
769
|
+
var _a;
|
|
770
|
+
let pwr = (_a = promise[barrierSym]) !== null && _a !== void 0 ? _a : {};
|
|
771
|
+
if (!('promise' in pwr)) {
|
|
772
|
+
Object.assign(pwr, promiseWithResolvers());
|
|
773
|
+
}
|
|
774
|
+
if (promise[barrierSym] == null) {
|
|
775
|
+
pwr.resolve();
|
|
776
|
+
promise[barrierSym] = pwr;
|
|
777
|
+
}
|
|
778
|
+
return pwr.promise;
|
|
779
|
+
}
|
|
780
|
+
// Polyfill Promise.withResolvers.
|
|
781
|
+
let promiseWithResolvers = Promise.withResolvers;
|
|
782
|
+
if (promiseWithResolvers && typeof promiseWithResolvers === 'function') {
|
|
783
|
+
promiseWithResolvers = promiseWithResolvers.bind(Promise);
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
promiseWithResolvers = function () {
|
|
787
|
+
let resolve;
|
|
788
|
+
let reject;
|
|
789
|
+
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
|
|
790
|
+
return { promise, resolve, reject };
|
|
791
|
+
};
|
|
792
|
+
}
|