1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/extensions/web/03_abort_signal.js
Luca Casonato f9ff981daf
feat: MessageChannel and MessagePort (#11051)
This commit introduces support for MessageChannel and MessagePort.
MessagePorts can be transfered across other MessagePorts.
2021-06-21 19:53:52 +02:00

110 lines
2.4 KiB
JavaScript

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const webidl = window.__bootstrap.webidl;
const { setIsTrusted, defineEventHandler } = window.__bootstrap.event;
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();
static abort() {
const signal = new AbortSignal(illegalConstructorKey);
signal[signalAbort]();
return signal;
}
[add](algorithm) {
this.#abortAlgorithms.add(algorithm);
}
[signalAbort]() {
if (this.#aborted) {
return;
}
this.#aborted = true;
for (const algorithm of this.#abortAlgorithms) {
algorithm();
}
this.#abortAlgorithms.clear();
const event = new Event("abort");
setIsTrusted(event, true);
this.dispatchEvent(event);
}
[remove](algorithm) {
this.#abortAlgorithms.delete(algorithm);
}
constructor(key = null) {
if (key != illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
}
super();
this[webidl.brand] = webidl.brand;
}
get aborted() {
return Boolean(this.#aborted);
}
get [Symbol.toStringTag]() {
return "AbortSignal";
}
}
defineEventHandler(AbortSignal.prototype, "abort");
webidl.configurePrototype(AbortSignal);
class AbortController {
#signal = new AbortSignal(illegalConstructorKey);
get signal() {
return this.#signal;
}
abort() {
this.#signal[signalAbort]();
}
get [Symbol.toStringTag]() {
return "AbortController";
}
}
webidl.configurePrototype(AbortController);
webidl.converters["AbortSignal"] = webidl.createInterfaceConverter(
"AbortSignal",
AbortSignal,
);
function newSignal() {
return new AbortSignal(illegalConstructorKey);
}
function follow(followingSignal, parentSignal) {
if (parentSignal.aborted) {
followingSignal[signalAbort]();
} else {
parentSignal[add](() => followingSignal[signalAbort]());
}
}
window.AbortSignal = AbortSignal;
window.AbortController = AbortController;
window.__bootstrap.abortSignal = {
add,
signalAbort,
remove,
follow,
newSignal,
};
})(this);