2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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
|
|
|
|
|
|
|
((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;
|
2021-01-05 16:43:25 -05:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const add = Symbol("add");
|
|
|
|
const signalAbort = Symbol("signalAbort");
|
|
|
|
const remove = Symbol("remove");
|
|
|
|
|
2020-09-19 17:30:59 -04:00
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class AbortSignal extends EventTarget {
|
|
|
|
#aborted = false;
|
|
|
|
#abortAlgorithms = new Set();
|
|
|
|
|
2021-03-27 10:49:57 -04:00
|
|
|
static abort() {
|
|
|
|
const signal = new AbortSignal(illegalConstructorKey);
|
|
|
|
signal[signalAbort]();
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
[add](algorithm) {
|
|
|
|
this.#abortAlgorithms.add(algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
[signalAbort]() {
|
|
|
|
if (this.#aborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.#aborted = true;
|
|
|
|
for (const algorithm of this.#abortAlgorithms) {
|
|
|
|
algorithm();
|
|
|
|
}
|
|
|
|
this.#abortAlgorithms.clear();
|
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) {
|
|
|
|
this.#abortAlgorithms.delete(algorithm);
|
|
|
|
}
|
|
|
|
|
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-04-20 08:47:22 -04:00
|
|
|
this[webidl.brand] = webidl.brand;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get aborted() {
|
|
|
|
return Boolean(this.#aborted);
|
|
|
|
}
|
|
|
|
|
|
|
|
get [Symbol.toStringTag]() {
|
|
|
|
return "AbortSignal";
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 20:40:36 -05:00
|
|
|
defineEventHandler(AbortSignal.prototype, "abort");
|
2021-06-07 04:04:10 -04:00
|
|
|
|
|
|
|
webidl.configurePrototype(AbortSignal);
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class AbortController {
|
2020-09-19 17:30:59 -04:00
|
|
|
#signal = new AbortSignal(illegalConstructorKey);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
get signal() {
|
|
|
|
return this.#signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this.#signal[signalAbort]();
|
|
|
|
}
|
|
|
|
|
|
|
|
get [Symbol.toStringTag]() {
|
|
|
|
return "AbortController";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:04:10 -04:00
|
|
|
webidl.configurePrototype(AbortController);
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
webidl.converters["AbortSignal"] = webidl.createInterfaceConverter(
|
|
|
|
"AbortSignal",
|
|
|
|
AbortSignal,
|
|
|
|
);
|
|
|
|
|
2021-06-06 09:37:17 -04:00
|
|
|
function newSignal() {
|
|
|
|
return new AbortSignal(illegalConstructorKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
function follow(followingSignal, parentSignal) {
|
|
|
|
if (parentSignal.aborted) {
|
|
|
|
followingSignal[signalAbort]();
|
|
|
|
} else {
|
|
|
|
parentSignal[add](() => followingSignal[signalAbort]());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = {
|
|
|
|
add,
|
|
|
|
signalAbort,
|
|
|
|
remove,
|
2021-06-06 09:37:17 -04:00
|
|
|
follow,
|
|
|
|
newSignal,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|