UNPKG

1.49 kBJavaScriptView Raw
1import AbortSignal, { abortSignal, createAbortSignal } from './abortsignal';
2/**
3 * The AbortController.
4 * @see https://dom.spec.whatwg.org/#abortcontroller
5 */
6export default class AbortController {
7 /**
8 * Initialize this controller.
9 */
10 constructor() {
11 signals.set(this, createAbortSignal());
12 }
13 /**
14 * Returns the `AbortSignal` object associated with this object.
15 */
16 get signal() {
17 return getSignal(this);
18 }
19 /**
20 * Abort and signal to any observers that the associated activity is to be aborted.
21 */
22 abort() {
23 abortSignal(getSignal(this));
24 }
25}
26/**
27 * Associated signals.
28 */
29const signals = new WeakMap();
30/**
31 * Get the associated signal of a given controller.
32 */
33function getSignal(controller) {
34 const signal = signals.get(controller);
35 if (signal == null) {
36 throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? 'null' : typeof controller}`);
37 }
38 return signal;
39}
40// Properties should be enumerable.
41Object.defineProperties(AbortController.prototype, {
42 signal: { enumerable: true },
43 abort: { enumerable: true },
44});
45if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
46 Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
47 configurable: true,
48 value: 'AbortController',
49 });
50}
51export { AbortController, AbortSignal };
52//# sourceMappingURL=index.js.map
\No newline at end of file