UNPKG

3.51 kBJavaScriptView Raw
1/**
2 * @author Toru Nagashima <https://github.com/mysticatea>
3 * @copyright 2017 Toru Nagashima. All rights reserved.
4 * See LICENSE file in root directory for full license.
5 */
6import { EventTarget, defineEventAttribute } from 'event-target-shim';
7
8/**
9 * Aborted flag for each instances.
10 * @type {WeakMap<AbortSignal, boolean>}
11 */
12const abortedFlags = new WeakMap();
13
14/**
15 * The signal class.
16 * @constructor
17 * @see https://dom.spec.whatwg.org/#abortsignal
18 */
19function AbortSignal() {
20 EventTarget.call(this);
21 abortedFlags.set(this, false);
22}
23
24// Properties should be enumerable.
25AbortSignal.prototype = Object.create(EventTarget.prototype, {
26 constructor: {
27 value: AbortSignal,
28 configurable: true,
29 writable: true,
30 },
31
32 /**
33 * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
34 * @property
35 * @memberof AbortSignal
36 * @name aborted
37 * @type {boolean}
38 */
39 aborted: {
40 get: function get_aborted() { //eslint-disable-line camelcase
41 const aborted = abortedFlags.get(this);
42 console.assert(typeof aborted === "boolean", "Expected 'this' to be an 'AbortSignal' object, but got", this);
43 return Boolean(aborted)
44 },
45 configurable: true,
46 enumerable: true,
47 },
48
49 /**
50 * The event attribute for `abort` event.
51 * @property
52 * @memberof AbortSignal
53 * @name onabort
54 * @type {Function}
55 */
56});
57
58defineEventAttribute(AbortSignal.prototype, "abort");
59
60/**
61 * Abort a given signal.
62 * @param {AbortSignal} signal The signal to abort.
63 * @returns {void}
64 */
65function abortSignal(signal) {
66 if (abortedFlags.get(signal) !== false) {
67 return
68 }
69
70 abortedFlags.set(signal, true);
71 signal.dispatchEvent({ type: "abort" });
72}
73
74/**
75 * Associated signals.
76 * @type {WeakMap<AbortController, AbortSignal>}
77 */
78const signals = new WeakMap();
79
80/**
81 * Get the associated signal of a given controller.
82 * @param {AbortController} controller The controller to get its associated signal.
83 * @returns {AbortSignal} The associated signal.
84 */
85function getSignal(controller) {
86 const signal = signals.get(controller);
87 console.assert(signal != null, "Expected 'this' to be an 'AbortController' object, but got", controller);
88 return signal
89}
90
91/**
92 * The AbortController.
93 * @constructor
94 * @see https://dom.spec.whatwg.org/#abortcontroller
95 */
96function AbortController() {
97 signals.set(this, new AbortSignal());
98}
99
100// Properties should be enumerable.
101Object.defineProperties(AbortController.prototype, {
102 /**
103 * Returns the `AbortSignal` object associated with this object.
104 * @type {AbortSignal}
105 */
106 signal: {
107 get: function get_signal() { //eslint-disable-line camelcase
108 return getSignal(this)
109 },
110 configurable: true,
111 enumerable: true,
112 },
113
114 /**
115 * Abort and signal to any observers that the associated activity is to be aborted.
116 * @returns {void}
117 */
118 abort: {
119 value: function abort() {
120 // Not depend on this.signal which is overridable.
121 const signal = getSignal(this);
122 if (signal != null) {
123 abortSignal(signal);
124 }
125 },
126 configurable: true,
127 enumerable: true,
128 writable: true,
129 },
130});
131
132export { AbortController, AbortSignal };
133export default AbortController;
134//# sourceMappingURL=abort-controller.mjs.map