2020-07-19 13:49:44 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
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();
|
|
|
|
|
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2020-09-19 17:30:59 -04:00
|
|
|
constructor(key) {
|
|
|
|
if (key != illegalConstructorKey) {
|
|
|
|
throw new TypeError("Illegal constructor.");
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
get aborted() {
|
|
|
|
return Boolean(this.#aborted);
|
|
|
|
}
|
|
|
|
|
|
|
|
get [Symbol.toStringTag]() {
|
|
|
|
return "AbortSignal";
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 20:40:36 -05:00
|
|
|
defineEventHandler(AbortSignal.prototype, "abort");
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:40:36 -05:00
|
|
|
const handlerSymbol = Symbol("eventHandlers");
|
2020-11-09 11:49:19 -05:00
|
|
|
|
|
|
|
function makeWrappedHandler(handler) {
|
|
|
|
function wrappedHandler(...args) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wrappedHandler.handler.call(this, ...args);
|
|
|
|
}
|
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
2020-11-09 22:34:42 -05:00
|
|
|
// TODO(benjamingr) reuse this here and websocket where possible
|
2020-11-05 20:40:36 -05:00
|
|
|
function defineEventHandler(emitter, name) {
|
|
|
|
// HTML specification section 8.1.5.1
|
|
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
2020-11-09 11:49:19 -05:00
|
|
|
return this[handlerSymbol]?.get(name)?.handler;
|
2020-11-05 20:40:36 -05:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
this[handlerSymbol] = new Map();
|
|
|
|
}
|
2020-11-09 11:49:19 -05:00
|
|
|
let handlerWrapper = this[handlerSymbol]?.get(name);
|
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else {
|
|
|
|
handlerWrapper = makeWrappedHandler(value);
|
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
}
|
|
|
|
this[handlerSymbol].set(name, handlerWrapper);
|
2020-11-05 20:40:36 -05:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:43:20 -04:00
|
|
|
window.AbortSignal = AbortSignal;
|
|
|
|
window.AbortController = AbortController;
|
|
|
|
window.__bootstrap = window.__bootstrap || {};
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.abortSignal = {
|
|
|
|
add,
|
|
|
|
signalAbort,
|
|
|
|
remove,
|
|
|
|
};
|
|
|
|
})(this);
|