diff options
Diffstat (limited to 'ext/web/03_abort_signal.js')
-rw-r--r-- | ext/web/03_abort_signal.js | 335 |
1 files changed, 170 insertions, 165 deletions
diff --git a/ext/web/03_abort_signal.js b/ext/web/03_abort_signal.js index cce1bac7e..96757f41f 100644 --- a/ext/web/03_abort_signal.js +++ b/ext/web/03_abort_signal.js @@ -1,200 +1,205 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -"use strict"; // @ts-check /// <reference path="../../core/internal.d.ts" /> -((window) => { - const webidl = window.__bootstrap.webidl; - const { Event, setIsTrusted, defineEventHandler } = window.__bootstrap.event; - const { EventTarget, listenerCount } = window.__bootstrap.eventTarget; - const { - SafeArrayIterator, - SafeSetIterator, - Set, - SetPrototypeAdd, - SetPrototypeDelete, - Symbol, - TypeError, - } = window.__bootstrap.primordials; - const { setTimeout, refTimer, unrefTimer } = window.__bootstrap.timers; - - 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; - } +import * as webidl from "internal:ext/webidl/00_webidl.js"; +import { + defineEventHandler, + Event, + EventTarget, + listenerCount, + setIsTrusted, +} from "internal:ext/web/02_event.js"; +const primordials = globalThis.__bootstrap.primordials; +const { + SafeArrayIterator, + SafeSetIterator, + Set, + SetPrototypeAdd, + SetPrototypeDelete, + Symbol, + TypeError, +} = primordials; +import { + refTimer, + setTimeout, + unrefTimer, +} from "internal:ext/web/02_timers.js"; + +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; + } - static timeout(millis) { - const prefix = "Failed to call 'AbortSignal.timeout'"; - webidl.requiredArguments(arguments.length, 1, { prefix }); - millis = webidl.converters["unsigned long long"](millis, { - enforceRange: true, - }); - - 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; - } + static timeout(millis) { + const prefix = "Failed to call 'AbortSignal.timeout'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + millis = webidl.converters["unsigned long long"](millis, { + enforceRange: true, + }); + + 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; + } - [add](algorithm) { - if (this.aborted) { - return; - } - if (this[abortAlgos] === null) { - this[abortAlgos] = new Set(); - } - SetPrototypeAdd(this[abortAlgos], algorithm); + [add](algorithm) { + if (this.aborted) { + return; } - - [signalAbort]( - reason = new DOMException("The signal has been aborted", "AbortError"), - ) { - if (this.aborted) { - return; - } - this[abortReason] = reason; - if (this[abortAlgos] !== null) { - for (const algorithm of new SafeSetIterator(this[abortAlgos])) { - algorithm(); - } - this[abortAlgos] = null; - } - const event = new Event("abort"); - setIsTrusted(event, true); - this.dispatchEvent(event); + if (this[abortAlgos] === null) { + this[abortAlgos] = new Set(); } + SetPrototypeAdd(this[abortAlgos], algorithm); + } - [remove](algorithm) { - this[abortAlgos] && SetPrototypeDelete(this[abortAlgos], algorithm); + [signalAbort]( + reason = new DOMException("The signal has been aborted", "AbortError"), + ) { + if (this.aborted) { + return; } - - constructor(key = null) { - if (key != illegalConstructorKey) { - throw new TypeError("Illegal constructor."); + this[abortReason] = reason; + if (this[abortAlgos] !== null) { + for (const algorithm of new SafeSetIterator(this[abortAlgos])) { + algorithm(); } - super(); - this[abortReason] = undefined; this[abortAlgos] = null; - this[timerId] = null; - this[webidl.brand] = webidl.brand; } + const event = new Event("abort"); + setIsTrusted(event, true); + this.dispatchEvent(event); + } - get aborted() { - webidl.assertBranded(this, AbortSignalPrototype); - return this[abortReason] !== undefined; - } + [remove](algorithm) { + this[abortAlgos] && SetPrototypeDelete(this[abortAlgos], algorithm); + } - get reason() { - webidl.assertBranded(this, AbortSignalPrototype); - return this[abortReason]; + constructor(key = null) { + if (key != illegalConstructorKey) { + throw new TypeError("Illegal constructor."); } + super(); + this[abortReason] = undefined; + this[abortAlgos] = null; + this[timerId] = null; + this[webidl.brand] = webidl.brand; + } - throwIfAborted() { - webidl.assertBranded(this, AbortSignalPrototype); - if (this[abortReason] !== undefined) { - throw this[abortReason]; - } + 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]; } + } - // `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]); - } + // `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]); } + } - removeEventListener(...args) { - super.removeEventListener(...new SafeArrayIterator(args)); - if (this[timerId] !== null && listenerCount(this, "abort") === 0) { - unrefTimer(this[timerId]); - } + removeEventListener(...args) { + super.removeEventListener(...new SafeArrayIterator(args)); + if (this[timerId] !== null && listenerCount(this, "abort") === 0) { + unrefTimer(this[timerId]); } } - defineEventHandler(AbortSignal.prototype, "abort"); +} +defineEventHandler(AbortSignal.prototype, "abort"); - webidl.configurePrototype(AbortSignal); - const AbortSignalPrototype = AbortSignal.prototype; +webidl.configurePrototype(AbortSignal); +const AbortSignalPrototype = AbortSignal.prototype; - class AbortController { - [signal] = new AbortSignal(illegalConstructorKey); +class AbortController { + [signal] = new AbortSignal(illegalConstructorKey); - constructor() { - this[webidl.brand] = webidl.brand; - } + constructor() { + this[webidl.brand] = webidl.brand; + } - get signal() { - webidl.assertBranded(this, AbortControllerPrototype); - return this[signal]; - } + get signal() { + webidl.assertBranded(this, AbortControllerPrototype); + return this[signal]; + } - abort(reason) { - webidl.assertBranded(this, AbortControllerPrototype); - this[signal][signalAbort](reason); - } + abort(reason) { + webidl.assertBranded(this, AbortControllerPrototype); + this[signal][signalAbort](reason); } +} - webidl.configurePrototype(AbortController); - const AbortControllerPrototype = AbortController.prototype; +webidl.configurePrototype(AbortController); +const AbortControllerPrototype = AbortController.prototype; - webidl.converters["AbortSignal"] = webidl.createInterfaceConverter( - "AbortSignal", - AbortSignal.prototype, - ); +webidl.converters["AbortSignal"] = webidl.createInterfaceConverter( + "AbortSignal", + AbortSignal.prototype, +); - function newSignal() { - return new AbortSignal(illegalConstructorKey); - } +function newSignal() { + return new AbortSignal(illegalConstructorKey); +} - function follow(followingSignal, parentSignal) { - if (followingSignal.aborted) { - return; - } - if (parentSignal.aborted) { - followingSignal[signalAbort](parentSignal.reason); - } else { - parentSignal[add](() => - followingSignal[signalAbort](parentSignal.reason) - ); - } +function follow(followingSignal, parentSignal) { + if (followingSignal.aborted) { + return; } - - window.__bootstrap.abortSignal = { - AbortSignal, - AbortController, - AbortSignalPrototype, - add, - signalAbort, - remove, - follow, - newSignal, - }; -})(this); + if (parentSignal.aborted) { + followingSignal[signalAbort](parentSignal.reason); + } else { + parentSignal[add](() => followingSignal[signalAbort](parentSignal.reason)); + } +} + +export { + AbortController, + AbortSignal, + AbortSignalPrototype, + add, + follow, + newSignal, + remove, + signalAbort, +}; |