mirror of
https://github.com/denoland/deno.git
synced 2024-11-29 16:30:56 -05:00
97 lines
2.2 KiB
JavaScript
97 lines
2.2 KiB
JavaScript
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
((window) => {
|
|
const add = Symbol("add");
|
|
const signalAbort = Symbol("signalAbort");
|
|
const remove = Symbol("remove");
|
|
|
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
|
|
|
class AbortSignal extends EventTarget {
|
|
#aborted = false;
|
|
#abortAlgorithms = new Set();
|
|
|
|
[add](algorithm) {
|
|
this.#abortAlgorithms.add(algorithm);
|
|
}
|
|
|
|
[signalAbort]() {
|
|
if (this.#aborted) {
|
|
return;
|
|
}
|
|
this.#aborted = true;
|
|
for (const algorithm of this.#abortAlgorithms) {
|
|
algorithm();
|
|
}
|
|
this.#abortAlgorithms.clear();
|
|
this.dispatchEvent(new Event("abort"));
|
|
}
|
|
|
|
[remove](algorithm) {
|
|
this.#abortAlgorithms.delete(algorithm);
|
|
}
|
|
|
|
constructor(key) {
|
|
if (key != illegalConstructorKey) {
|
|
throw new TypeError("Illegal constructor.");
|
|
}
|
|
super();
|
|
}
|
|
|
|
get aborted() {
|
|
return Boolean(this.#aborted);
|
|
}
|
|
|
|
get [Symbol.toStringTag]() {
|
|
return "AbortSignal";
|
|
}
|
|
}
|
|
defineEventHandler(AbortSignal.prototype, "abort");
|
|
class AbortController {
|
|
#signal = new AbortSignal(illegalConstructorKey);
|
|
|
|
get signal() {
|
|
return this.#signal;
|
|
}
|
|
|
|
abort() {
|
|
this.#signal[signalAbort]();
|
|
}
|
|
|
|
get [Symbol.toStringTag]() {
|
|
return "AbortController";
|
|
}
|
|
}
|
|
|
|
const handlerSymbol = Symbol("eventHandlers");
|
|
function defineEventHandler(emitter, name) {
|
|
// HTML specification section 8.1.5.1
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
get() {
|
|
return this[handlerSymbol]?.get(name);
|
|
},
|
|
set(value) {
|
|
const oldListener = this[handlerSymbol]?.get(name);
|
|
if (oldListener) {
|
|
this.removeEventListener(name, oldListener);
|
|
}
|
|
if (!this[handlerSymbol]) {
|
|
this[handlerSymbol] = new Map();
|
|
}
|
|
this.addEventListener(name, value);
|
|
this[handlerSymbol].set(value);
|
|
},
|
|
configurable: true,
|
|
enumerable: true,
|
|
});
|
|
}
|
|
|
|
window.AbortSignal = AbortSignal;
|
|
window.AbortController = AbortController;
|
|
window.__bootstrap = window.__bootstrap || {};
|
|
window.__bootstrap.abortSignal = {
|
|
add,
|
|
signalAbort,
|
|
remove,
|
|
};
|
|
})(this);
|