UNPKG

1.63 kBJavaScriptView Raw
1import { Observable } from '../data/observable';
2/**
3 * The signal class.
4 * @see https://dom.spec.whatwg.org/#abortsignal
5 */
6export default class AbortSignal extends Observable {
7 /**
8 * AbortSignal cannot be constructed directly.
9 */
10 constructor() {
11 super();
12 }
13 /**
14 * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
15 */
16 get aborted() {
17 const aborted = abortedFlags.get(this);
18 if (typeof aborted !== 'boolean') {
19 throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? 'null' : typeof this}`);
20 }
21 return aborted;
22 }
23}
24/**
25 * Create an AbortSignal object.
26 */
27export function createAbortSignal() {
28 const signal = new AbortSignal();
29 abortedFlags.set(signal, false);
30 return signal;
31}
32/**
33 * Abort a given signal.
34 */
35export function abortSignal(signal) {
36 if (abortedFlags.get(signal) !== false) {
37 return;
38 }
39 abortedFlags.set(signal, true);
40 signal.notify({ eventName: 'abort', type: 'abort' });
41}
42/**
43 * Aborted flag for each instances.
44 */
45const abortedFlags = new WeakMap();
46// Properties should be enumerable.
47Object.defineProperties(AbortSignal.prototype, {
48 aborted: { enumerable: true },
49});
50// `toString()` should return `"[object AbortSignal]"`
51if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
52 Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
53 configurable: true,
54 value: 'AbortSignal',
55 });
56}
57//# sourceMappingURL=abortsignal.js.map
\No newline at end of file