2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-07-06 08:38:12 -04:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
((window) => {
|
2021-04-20 08:47:22 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-06-21 13:53:52 -04:00
|
|
|
const { setIsTrusted, defineEventHandler } = window.__bootstrap.event;
|
2022-03-14 15:19:22 -04:00
|
|
|
const { listenerCount } = window.__bootstrap.eventTarget;
|
2021-07-06 08:38:12 -04:00
|
|
|
const {
|
|
|
|
Set,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
|
|
|
Symbol,
|
|
|
|
TypeError,
|
|
|
|
} = window.__bootstrap.primordials;
|
2022-03-14 15:19:22 -04:00
|
|
|
const { setTimeout, refTimer, unrefTimer } = window.__bootstrap.timers;
|
2021-01-05 16:43:25 -05:00
|
|
|
|
2021-11-08 17:37:06 -05:00
|
|
|
const add = Symbol("[[add]]");
|
|
|
|
const signalAbort = Symbol("[[signalAbort]]");
|
|
|
|
const remove = Symbol("[[remove]]");
|
|
|
|
const abortReason = Symbol("[[abortReason]]");
|
|
|
|
const abortAlgos = Symbol("[[abortAlgos]]");
|
|
|
|
const signal = Symbol("[[signal]]");
|
2022-03-14 15:19:22 -04:00
|
|
|
const timerId = Symbol("[[timerId]]");
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-09-19 17:30:59 -04:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class AbortSignal extends EventTarget {
|
2021-11-08 17:37:06 -05:00
|
|
|
static abort(reason = undefined) {
|
|
|
|
if (reason !== undefined) {
|
|
|
|
reason = webidl.converters.any(reason);
|
|
|
|
}
|
2021-03-27 10:49:57 -04:00
|
|
|
const signal = new AbortSignal(illegalConstructorKey);
|
2021-11-08 17:37:06 -05:00
|
|
|
signal[signalAbort](reason);
|
2021-03-27 10:49:57 -04:00
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:19:22 -04:00
|
|
|
static timeout(millis) {
|
|
|
|
const prefix = "Failed to call 'AbortSignal.timeout'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
millis = webidl.converters["unsigned long long"](millis, {
|
|
|
|
enforceRange: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const signal = new AbortSignal(illegalConstructorKey);
|
|
|
|
signal[timerId] = setTimeout(
|
|
|
|
() => {
|
|
|
|
signal[timerId] = null;
|
|
|
|
signal[signalAbort](
|
|
|
|
new DOMException("Signal timed out.", "TimeoutError"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
millis,
|
|
|
|
);
|
|
|
|
unrefTimer(signal[timerId]);
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
[add](algorithm) {
|
2021-11-08 17:37:06 -05:00
|
|
|
if (this.aborted) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-21 11:38:57 -04:00
|
|
|
if (this[abortAlgos] === null) {
|
|
|
|
this[abortAlgos] = new Set();
|
|
|
|
}
|
|
|
|
SetPrototypeAdd(this[abortAlgos], algorithm);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 17:37:06 -05:00
|
|
|
[signalAbort](
|
|
|
|
reason = new DOMException("The signal has been aborted", "AbortError"),
|
|
|
|
) {
|
|
|
|
if (this.aborted) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-11-08 17:37:06 -05:00
|
|
|
this[abortReason] = reason;
|
2021-09-21 11:38:57 -04:00
|
|
|
if (this[abortAlgos] !== null) {
|
|
|
|
for (const algorithm of this[abortAlgos]) {
|
|
|
|
algorithm();
|
|
|
|
}
|
|
|
|
this[abortAlgos] = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-05 16:43:25 -05:00
|
|
|
const event = new Event("abort");
|
|
|
|
setIsTrusted(event, true);
|
|
|
|
this.dispatchEvent(event);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
[remove](algorithm) {
|
2021-09-21 11:38:57 -04:00
|
|
|
this[abortAlgos] && SetPrototypeDelete(this[abortAlgos], algorithm);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-11-14 07:10:23 -05:00
|
|
|
constructor(key = null) {
|
2020-09-19 17:30:59 -04:00
|
|
|
if (key != illegalConstructorKey) {
|
|
|
|
throw new TypeError("Illegal constructor.");
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
super();
|
2021-11-08 17:37:06 -05:00
|
|
|
this[abortReason] = undefined;
|
2021-09-21 11:38:57 -04:00
|
|
|
this[abortAlgos] = null;
|
2022-03-14 15:19:22 -04:00
|
|
|
this[timerId] = null;
|
2021-04-20 08:47:22 -04:00
|
|
|
this[webidl.brand] = webidl.brand;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get aborted() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
2021-11-08 17:37:06 -05:00
|
|
|
return this[abortReason] !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
get reason() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
2021-11-08 17:37:06 -05:00
|
|
|
return this[abortReason];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-10 09:12:38 -05:00
|
|
|
|
|
|
|
throwIfAborted() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
2021-12-10 09:12:38 -05:00
|
|
|
if (this[abortReason] !== undefined) {
|
|
|
|
throw this[abortReason];
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 15:19:22 -04:00
|
|
|
|
|
|
|
// `addEventListener` and `removeEventListener` have to be overriden in
|
|
|
|
// order to have the timer block the event loop while there are listeners.
|
|
|
|
// `[add]` and `[remove]` don't ref and unref the timer because they can
|
|
|
|
// only be used by Deno internals, which use it to essentially cancel async
|
|
|
|
// ops which would block the event loop.
|
|
|
|
addEventListener(...args) {
|
|
|
|
super.addEventListener(...args);
|
|
|
|
if (this[timerId] !== null && listenerCount(this, "abort") > 0) {
|
|
|
|
refTimer(this[timerId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListener(...args) {
|
|
|
|
super.removeEventListener(...args);
|
|
|
|
if (this[timerId] !== null && listenerCount(this, "abort") === 0) {
|
|
|
|
unrefTimer(this[timerId]);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2020-11-05 20:40:36 -05:00
|
|
|
defineEventHandler(AbortSignal.prototype, "abort");
|
2021-06-07 04:04:10 -04:00
|
|
|
|
|
|
|
webidl.configurePrototype(AbortSignal);
|
2022-02-01 12:06:11 -05:00
|
|
|
const AbortSignalPrototype = AbortSignal.prototype;
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class AbortController {
|
2021-11-08 17:37:06 -05:00
|
|
|
[signal] = new AbortSignal(illegalConstructorKey);
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
get signal() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, AbortControllerPrototype);
|
2021-11-08 17:37:06 -05:00
|
|
|
return this[signal];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 17:37:06 -05:00
|
|
|
abort(reason) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, AbortControllerPrototype);
|
2021-11-08 17:37:06 -05:00
|
|
|
this[signal][signalAbort](reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:04:10 -04:00
|
|
|
webidl.configurePrototype(AbortController);
|
2022-02-01 12:06:11 -05:00
|
|
|
const AbortControllerPrototype = AbortController.prototype;
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
webidl.converters["AbortSignal"] = webidl.createInterfaceConverter(
|
|
|
|
"AbortSignal",
|
2022-02-01 12:06:11 -05:00
|
|
|
AbortSignal.prototype,
|
2021-04-20 08:47:22 -04:00
|
|
|
);
|
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
function newSignal() {
|
|
|
|
return new AbortSignal(illegalConstructorKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
function follow(followingSignal, parentSignal) {
|
2021-11-08 17:37:06 -05:00
|
|
|
if (followingSignal.aborted) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
if (parentSignal.aborted) {
|
2021-11-08 17:37:06 -05:00
|
|
|
followingSignal[signalAbort](parentSignal.reason);
|
2021-06-06 09:37:17 -04:00
|
|
|
} else {
|
2021-11-08 17:37:06 -05:00
|
|
|
parentSignal[add](() =>
|
|
|
|
followingSignal[signalAbort](parentSignal.reason)
|
|
|
|
);
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:43:20 -04:00
|
|
|
window.AbortSignal = AbortSignal;
|
|
|
|
window.AbortController = AbortController;
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.abortSignal = {
|
2022-02-01 12:06:11 -05:00
|
|
|
AbortSignalPrototype,
|
2020-07-19 13:49:44 -04:00
|
|
|
add,
|
|
|
|
signalAbort,
|
|
|
|
remove,
|
2021-06-06 09:37:17 -04:00
|
|
|
follow,
|
|
|
|
newSignal,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|