|
| 1 | +/** |
| 2 | + * Based on abort-controller by Toru Nagashima |
| 3 | + * https://github.com/mysticatea/abort-controller |
| 4 | + * |
| 5 | + * Original work Copyright (c) 2017 Toru Nagashima |
| 6 | + * Modified work Copyright (c) Meta Platforms, Inc. and affiliates. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + * |
| 26 | + * @flow strict |
| 27 | + * @format |
| 28 | + */ |
| 29 | + |
| 30 | +// flowlint unsafe-getters-setters:off |
| 31 | + |
| 32 | +import type {EventCallback} from '../events/EventTarget'; |
| 33 | + |
| 34 | +import DOMException from '../../errors/DOMException'; |
| 35 | +import Event from '../events/Event'; |
| 36 | +import { |
| 37 | + getEventHandlerAttribute, |
| 38 | + setEventHandlerAttribute, |
| 39 | +} from '../events/EventHandlerAttributes'; |
| 40 | +import EventTarget from '../events/EventTarget'; |
| 41 | +import {dispatchTrustedEvent} from '../events/internals/EventTargetInternals'; |
| 42 | +import {AbortController} from './AbortController'; |
| 43 | + |
| 44 | +const ABORTED_KEY: symbol = Symbol('aborted'); |
| 45 | +const REASON_KEY: symbol = Symbol('reason'); |
| 46 | + |
| 47 | +/** |
| 48 | + * The signal class. |
| 49 | + * @see https://dom.spec.whatwg.org/#abortsignal |
| 50 | + */ |
| 51 | +export class AbortSignal extends EventTarget { |
| 52 | + /** |
| 53 | + * |
| 54 | + * Returns an AbortSignal instance whose abort reason is set to reason if not undefined; otherwise to an "AbortError" DOMException. |
| 55 | + * Docs: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/abort_static |
| 56 | + * Spec: https://dom.spec.whatwg.org/#dom-abortsignal-abort |
| 57 | + */ |
| 58 | + static abort(reason: unknown): AbortSignal { |
| 59 | + const signal = createAbortSignal(); |
| 60 | + abortSignal(reason, signal); |
| 61 | + return signal; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * AbortSignal.timeout static method |
| 66 | + * Docs: https:developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static |
| 67 | + * Spec: https://dom.spec.whatwg.org/#dom-abortsignal-timeout |
| 68 | + */ |
| 69 | + static timeout(timeInMs: number): AbortSignal { |
| 70 | + if (!(timeInMs >= 0)) { |
| 71 | + throw new TypeError( |
| 72 | + "Failed to execute 'timeout' on 'AbortSignal': The provided value has to be a non-negative number.", |
| 73 | + ); |
| 74 | + } |
| 75 | + const controller = new AbortController(); |
| 76 | + setTimeout( |
| 77 | + () => |
| 78 | + controller.abort(new DOMException('signal timed out', 'TimeoutError')), |
| 79 | + timeInMs, |
| 80 | + ); |
| 81 | + return controller.signal; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * 3. AbortSignal.any static method |
| 86 | + * Docs: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static |
| 87 | + * Spec: https://dom.spec.whatwg.org/#dom-abortsignal-any |
| 88 | + */ |
| 89 | + static any(signals: AbortSignal[]): AbortSignal { |
| 90 | + if (!Array.isArray(signals)) { |
| 91 | + throw new TypeError('The signals value must be an instance of Array'); |
| 92 | + } |
| 93 | + |
| 94 | + const controller = new AbortController(); |
| 95 | + const listeners = []; |
| 96 | + const cleanup = () => listeners.forEach(unsubscribe => unsubscribe()); |
| 97 | + |
| 98 | + for (let i = 0; i < signals.length; i++) { |
| 99 | + const signal = signals[i]; |
| 100 | + |
| 101 | + // Validate that each item is an AbortSignal |
| 102 | + if (!(signal instanceof AbortSignal)) { |
| 103 | + cleanup(); // Remove all listeners added so far |
| 104 | + throw new TypeError( |
| 105 | + 'The "signals[' + |
| 106 | + i + |
| 107 | + ']" argument must be an instance of AbortSignal', |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + // Abort immediately if one of the signals is already aborted |
| 112 | + if (signal.aborted) { |
| 113 | + cleanup(); // Remove all listeners added so far |
| 114 | + controller.abort(signal.reason); |
| 115 | + break; |
| 116 | + } |
| 117 | + |
| 118 | + const onAbort = () => { |
| 119 | + controller.abort(signal.reason); |
| 120 | + cleanup(); |
| 121 | + }; |
| 122 | + signal.addEventListener('abort', onAbort); |
| 123 | + listeners.push(() => signal.removeEventListener('abort', onAbort)); |
| 124 | + } |
| 125 | + return controller.signal; |
| 126 | + } |
| 127 | + |
| 128 | + // $FlowExpectedError[unsupported-syntax] |
| 129 | + [ABORTED_KEY]: boolean = false; |
| 130 | + // $FlowExpectedError[unsupported-syntax] |
| 131 | + [REASON_KEY]: unknown; |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise. |
| 135 | + */ |
| 136 | + get aborted(): boolean { |
| 137 | + // $FlowExpectedError[prop-missing] |
| 138 | + const aborted = this[ABORTED_KEY]; |
| 139 | + if (typeof aborted !== 'boolean') { |
| 140 | + throw new TypeError( |
| 141 | + `Expected 'this' to be an 'AbortSignal' object, but got ${ |
| 142 | + // $FlowExpectedError[invalid-compare] |
| 143 | + this === null ? 'null' : typeof this |
| 144 | + }`, |
| 145 | + ); |
| 146 | + } |
| 147 | + return aborted; |
| 148 | + } |
| 149 | + |
| 150 | + get reason(): unknown { |
| 151 | + // $FlowExpectedError[prop-missing] |
| 152 | + return this[REASON_KEY]; |
| 153 | + } |
| 154 | + |
| 155 | + get onabort(): EventCallback | null { |
| 156 | + return getEventHandlerAttribute(this, 'abort'); |
| 157 | + } |
| 158 | + |
| 159 | + set onabort(listener: ?EventCallback): void { |
| 160 | + setEventHandlerAttribute(this, 'abort', listener); |
| 161 | + } |
| 162 | + |
| 163 | + throwIfAborted(): void { |
| 164 | + if (this.aborted) { |
| 165 | + throw this.reason; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * Create an AbortSignal object. |
| 172 | + */ |
| 173 | +export function createAbortSignal(): AbortSignal { |
| 174 | + return new AbortSignal(); |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * Abort a given signal. |
| 179 | + */ |
| 180 | +export function abortSignal( |
| 181 | + reason: unknown | void = new DOMException( |
| 182 | + 'signal is aborted without reason', |
| 183 | + 'AbortError', |
| 184 | + ), |
| 185 | + signal: AbortSignal, |
| 186 | +): void { |
| 187 | + // $FlowExpectedError[invalid-compare] |
| 188 | + // $FlowExpectedError[prop-missing] |
| 189 | + if (signal[ABORTED_KEY] !== false) { |
| 190 | + return; |
| 191 | + } |
| 192 | + // $FlowExpectedError[prop-missing] |
| 193 | + signal[ABORTED_KEY] = true; |
| 194 | + // $FlowExpectedError[prop-missing] |
| 195 | + signal[REASON_KEY] = reason; |
| 196 | + dispatchTrustedEvent(signal, new Event('abort')); |
| 197 | +} |
| 198 | + |
| 199 | +// Properties should be enumerable. |
| 200 | +//$FlowExpectedError[cannot-write] |
| 201 | +Object.defineProperties(AbortSignal.prototype, { |
| 202 | + aborted: {enumerable: true}, |
| 203 | + reason: {enumerable: true}, |
| 204 | + onabort: {enumerable: true}, |
| 205 | + throwIfAborted: {enumerable: true}, |
| 206 | +}); |
| 207 | + |
| 208 | +// `toString()` should return `"[object AbortSignal]"` |
| 209 | +Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, { |
| 210 | + configurable: true, |
| 211 | + value: 'AbortSignal', |
| 212 | +}); |
| 213 | + |
| 214 | +/** |
| 215 | + * AbortSignal cannot be constructed directly. |
| 216 | + * So this wrapper is used to achieve such behavior |
| 217 | + */ |
| 218 | +export const AbortSignal_public: typeof AbortSignal = |
| 219 | + /* eslint-disable no-shadow */ |
| 220 | + // $FlowExpectedError[incompatible-type] |
| 221 | + function AbortSignal() { |
| 222 | + throw new TypeError( |
| 223 | + "Failed to construct 'AbortSignal': Illegal constructor", |
| 224 | + ); |
| 225 | + }; |
| 226 | + |
| 227 | +// Copy static properties ('length', 'name', 'prototype', 'abort', 'any', 'timeout') so that callers accessing them via the public constructor (e.g. `AbortSignal.timeout(0)`) still work. |
| 228 | +// $FlowFixMe[unsafe-object-assign] |
| 229 | +// $FlowFixMe[not-an-object] |
| 230 | +Object.getOwnPropertyNames(AbortSignal).forEach(methodName => { |
| 231 | + Object.defineProperty( |
| 232 | + AbortSignal_public, |
| 233 | + methodName, |
| 234 | + // $FlowExpectedError[incompatible-type] |
| 235 | + Object.getOwnPropertyDescriptor(AbortSignal, methodName), |
| 236 | + ); |
| 237 | +}); |
0 commit comments