2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-07-06 08:38:12 -04:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
defineEventHandler,
|
|
|
|
Event,
|
|
|
|
EventTarget,
|
|
|
|
listenerCount,
|
|
|
|
setIsTrusted,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/02_event.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
SafeArrayIterator,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeSet,
|
2023-02-07 14:22:46 -05:00
|
|
|
SafeSetIterator,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
|
|
|
Symbol,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
2023-03-08 06:44:54 -05:00
|
|
|
import { refTimer, setTimeout, unrefTimer } from "ext:deno_web/02_timers.js";
|
2023-02-07 14:22:46 -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]]");
|
|
|
|
const timerId = Symbol("[[timerId]]");
|
|
|
|
|
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
|
|
|
class AbortSignal extends EventTarget {
|
|
|
|
static abort(reason = undefined) {
|
|
|
|
if (reason !== undefined) {
|
|
|
|
reason = webidl.converters.any(reason);
|
|
|
|
}
|
|
|
|
const signal = new AbortSignal(illegalConstructorKey);
|
|
|
|
signal[signalAbort](reason);
|
|
|
|
return signal;
|
|
|
|
}
|
2021-03-27 10:49:57 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static timeout(millis) {
|
|
|
|
const prefix = "Failed to call 'AbortSignal.timeout'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
millis = webidl.converters["unsigned long long"](
|
|
|
|
millis,
|
|
|
|
prefix,
|
|
|
|
"Argument 1",
|
|
|
|
{
|
|
|
|
enforceRange: true,
|
|
|
|
},
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-03-14 15:19:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[add](algorithm) {
|
|
|
|
if (this.aborted) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this[abortAlgos] === null) {
|
2023-04-14 16:23:28 -04:00
|
|
|
this[abortAlgos] = new SafeSet();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
SetPrototypeAdd(this[abortAlgos], algorithm);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[signalAbort](
|
|
|
|
reason = new DOMException("The signal has been aborted", "AbortError"),
|
|
|
|
) {
|
|
|
|
if (this.aborted) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
this[abortReason] = reason;
|
|
|
|
if (this[abortAlgos] !== null) {
|
|
|
|
for (const algorithm of new SafeSetIterator(this[abortAlgos])) {
|
|
|
|
algorithm();
|
2020-09-19 17:30:59 -04:00
|
|
|
}
|
2021-09-21 11:38:57 -04:00
|
|
|
this[abortAlgos] = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new Event("abort");
|
|
|
|
setIsTrusted(event, true);
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[remove](algorithm) {
|
|
|
|
this[abortAlgos] && SetPrototypeDelete(this[abortAlgos], algorithm);
|
|
|
|
}
|
2021-11-08 17:37:06 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(key = null) {
|
|
|
|
if (key != illegalConstructorKey) {
|
|
|
|
throw new TypeError("Illegal constructor.");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
super();
|
|
|
|
this[abortReason] = undefined;
|
|
|
|
this[abortAlgos] = null;
|
|
|
|
this[timerId] = null;
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2021-12-10 09:12:38 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get aborted() {
|
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
|
|
|
return this[abortReason] !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
get reason() {
|
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
|
|
|
return this[abortReason];
|
|
|
|
}
|
|
|
|
|
|
|
|
throwIfAborted() {
|
|
|
|
webidl.assertBranded(this, AbortSignalPrototype);
|
|
|
|
if (this[abortReason] !== undefined) {
|
|
|
|
throw this[abortReason];
|
2021-12-10 09:12:38 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-03-14 15:19:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05: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(...new SafeArrayIterator(args));
|
|
|
|
if (this[timerId] !== null && listenerCount(this, "abort") > 0) {
|
|
|
|
refTimer(this[timerId]);
|
2022-03-14 15:19:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-03-14 15:19:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
removeEventListener(...args) {
|
|
|
|
super.removeEventListener(...new SafeArrayIterator(args));
|
|
|
|
if (this[timerId] !== null && listenerCount(this, "abort") === 0) {
|
|
|
|
unrefTimer(this[timerId]);
|
2022-03-14 15:19:22 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
defineEventHandler(AbortSignal.prototype, "abort");
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.configurePrototype(AbortSignal);
|
|
|
|
const AbortSignalPrototype = AbortSignal.prototype;
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class AbortController {
|
|
|
|
[signal] = new AbortSignal(illegalConstructorKey);
|
2021-11-08 17:37:06 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get signal() {
|
|
|
|
webidl.assertBranded(this, AbortControllerPrototype);
|
|
|
|
return this[signal];
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
abort(reason) {
|
|
|
|
webidl.assertBranded(this, AbortControllerPrototype);
|
|
|
|
this[signal][signalAbort](reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.configurePrototype(AbortController);
|
|
|
|
const AbortControllerPrototype = AbortController.prototype;
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.converters["AbortSignal"] = webidl.createInterfaceConverter(
|
|
|
|
"AbortSignal",
|
|
|
|
AbortSignal.prototype,
|
|
|
|
);
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function newSignal() {
|
|
|
|
return new AbortSignal(illegalConstructorKey);
|
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function follow(followingSignal, parentSignal) {
|
|
|
|
if (followingSignal.aborted) {
|
|
|
|
return;
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (parentSignal.aborted) {
|
|
|
|
followingSignal[signalAbort](parentSignal.reason);
|
|
|
|
} else {
|
|
|
|
parentSignal[add](() => followingSignal[signalAbort](parentSignal.reason));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
AbortController,
|
|
|
|
AbortSignal,
|
|
|
|
AbortSignalPrototype,
|
|
|
|
add,
|
|
|
|
follow,
|
|
|
|
newSignal,
|
|
|
|
remove,
|
|
|
|
signalAbort,
|
2023-05-31 14:06:21 -04:00
|
|
|
timerId,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|