1 | class Emitter {
|
2 | constructor() {
|
3 | Object.defineProperty(this, 'listeners', { value: {}, writable: true, configurable: true });
|
4 | }
|
5 | addEventListener(type, callback, options) {
|
6 | if (!(type in this.listeners)) {
|
7 | this.listeners[type] = [];
|
8 | }
|
9 | this.listeners[type].push({ callback, options });
|
10 | }
|
11 | removeEventListener(type, callback) {
|
12 | if (!(type in this.listeners)) {
|
13 | return;
|
14 | }
|
15 | const stack = this.listeners[type];
|
16 | for (let i = 0, l = stack.length; i < l; i++) {
|
17 | if (stack[i].callback === callback) {
|
18 | stack.splice(i, 1);
|
19 | return;
|
20 | }
|
21 | }
|
22 | }
|
23 | dispatchEvent(event) {
|
24 | if (!(event.type in this.listeners)) {
|
25 | return;
|
26 | }
|
27 | const stack = this.listeners[event.type];
|
28 | const stackToCall = stack.slice();
|
29 | for (let i = 0, l = stackToCall.length; i < l; i++) {
|
30 | const listener = stackToCall[i];
|
31 | try {
|
32 | listener.callback.call(this, event);
|
33 | } catch (e) {
|
34 | Promise.resolve().then(() => {
|
35 | throw e;
|
36 | });
|
37 | }
|
38 | if (listener.options && listener.options.once) {
|
39 | this.removeEventListener(event.type, listener.callback);
|
40 | }
|
41 | }
|
42 | return !event.defaultPrevented;
|
43 | }
|
44 | }
|
45 |
|
46 | export class AbortSignal extends Emitter {
|
47 | constructor() {
|
48 | super();
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | if (!this.listeners) {
|
56 | Emitter.call(this);
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | Object.defineProperty(this, 'aborted', { value: false, writable: true, configurable: true });
|
62 | Object.defineProperty(this, 'onabort', { value: null, writable: true, configurable: true });
|
63 | }
|
64 | toString() {
|
65 | return '[object AbortSignal]';
|
66 | }
|
67 | dispatchEvent(event) {
|
68 | if (event.type === 'abort') {
|
69 | this.aborted = true;
|
70 | if (typeof this.onabort === 'function') {
|
71 | this.onabort.call(this, event);
|
72 | }
|
73 | }
|
74 |
|
75 | super.dispatchEvent(event);
|
76 | }
|
77 | }
|
78 |
|
79 | export class AbortController {
|
80 | constructor() {
|
81 |
|
82 |
|
83 | Object.defineProperty(this, 'signal', { value: new AbortSignal(), writable: true, configurable: true });
|
84 | }
|
85 | abort() {
|
86 | let event;
|
87 | try {
|
88 | event = new Event('abort');
|
89 | } catch (e) {
|
90 | if (typeof document !== 'undefined') {
|
91 | if (!document.createEvent) {
|
92 |
|
93 | event = document.createEventObject();
|
94 | event.type = 'abort';
|
95 | } else {
|
96 |
|
97 | event = document.createEvent('Event');
|
98 | event.initEvent('abort', false, false);
|
99 | }
|
100 | } else {
|
101 |
|
102 | event = {
|
103 | type: 'abort',
|
104 | bubbles: false,
|
105 | cancelable: false,
|
106 | };
|
107 | }
|
108 | }
|
109 | this.signal.dispatchEvent(event);
|
110 | }
|
111 | toString() {
|
112 | return '[object AbortController]';
|
113 | }
|
114 | }
|
115 |
|
116 | export default AbortController;
|
117 |
|
118 | if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
119 |
|
120 |
|
121 | AbortController.prototype[Symbol.toStringTag] = 'AbortController';
|
122 | AbortSignal.prototype[Symbol.toStringTag] = 'AbortSignal';
|
123 | }
|