-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abort.js
192 lines (166 loc) · 4.54 KB
/
abort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
if (! ('AbortSignal' in globalThis)) {
const symbols = {
signal: Symbol('signal'),
aborted: Symbol('aborted'),
reason: Symbol('reason'),
onabort: Symbol('onabort'),
};
globalThis.AbortError = class AbortError extends Error {};
globalThis.AbortSignal = class AbortSignal extends EventTarget {
constructor() {
super();
Object.defineProperties(this,{
[symbols.aborted]: {
enumerable: false,
writable: true,
configurable: false,
value: false,
},
[symbols.reason]: {
enumerable: false,
writable: true,
configurable: false,
value: undefined,
},
[symbols.onabort]: {
enumerable: false,
writable: true,
configurable: false,
value: null,
},
});
this.addEventListener('abort', event => {
if (this.onabort instanceof Function) {
this.onabort.call(this, event);
}
});
}
get aborted() {
return this[symbols.aborted];
}
get onabort() {
return this[symbols.onabort];
}
set onabort(value) {
if (value instanceof Function) {
this[symbols.onabort] = value;
} else {
this[symbols.onabort] = null;
}
}
get reason() {
return this[symbols.reason];
}
throwIfAborted() {
if (this.aborted) {
throw this.reason;
}
}
static abort(reason = new DOMException('Operation aborted')) {
const signal = new AbortSignal();
signal[symbols.aborted] = true;
signal[symbols.reason] = reason;
return signal;
}
};
globalThis.AbortController = class AbortController {
constructor() {
this[symbols.signal] = new AbortSignal();
}
get signal() {
return this[symbols.signal];
}
abort(reason = new DOMException('Operation aborted')) {
const signal = this.signal;
if (! signal.aborted) {
signal[symbols.aborted] = true;
signal[symbols.reason] = reason;
signal.dispatchEvent(new Event('abort'));
}
}
};
}
if (! ('reason' in AbortSignal.prototype)) {
const reasons = new WeakMap();
const abort = AbortController.prototype.abort;
if (AbortSignal.abort instanceof Function) {
const staticAbort = AbortSignal.abort;
AbortSignal.abort = function(reason = new DOMException('Operation aborted')) {
const signal = staticAbort();
reasons.set(signal, reason);
return signal;
};
} else {
AbortSignal.abort = function abort(reason = new DOMException('Operation aborted')) {
const controller = new AbortController();
controller.abort(reason);
return controller.reason;
};
}
Object.defineProperty(AbortSignal.prototype, 'reason', {
enumerable: true,
configurable: true,
get: function() {
if (reasons.has(this)) {
return reasons.get(this);
} else {
return undefined;
}
}
});
AbortController.prototype.abort = function(reason = new DOMException('Operation aborted')) {
reasons.set(this.signal, reason);
abort.call(this);
};
}
if (! (AbortSignal.prototype.throwIfAborted instanceof Function)) {
AbortSignal.prototype.throwIfAborted = function() {
if (this.aborted) {
throw this.reason;
}
};
}
if (! (AbortSignal.timeout instanceof Function)) {
AbortSignal.timeout = function(ms) {
if (typeof ms === 'undefined') {
throw new TypeError('At least one 1 argument required but only 0 passed');
} else if (! Number.isFinite(ms)) {
throw new TypeError('Argument 1 is not a finite value, so it is out of range for unsigned long long.');
} else if (ms < 0) {
throw new TypeError('Argument 1 is out of range for unsigned long long.');
} else {
const controller = new AbortController();
setTimeout(() => controller.abort(new DOMException('The operation timed out.')), ms);
return controller.signal;
}
};
}
/**
* @see https://chromestatus.com/feature/5202879349522432
* @TODO How do I handle if a signal is already aborted
* @TODO Should controller abort on a TypeError
*/
if (! (AbortSignal.any instanceof Function)) {
AbortSignal.any = function(signals) {
if (! Array.isArray(signals)) {
throw new TypeError('Expected an array of signals');
}
const controller = new AbortController();
for (const signal of signals) {
if (! (signal instanceof AbortSignal)) {
const err = new TypeError('`signal` is not an `AbortSignal`');
controller.abort(err);
throw err;
} else if (signal.aborted) {
controller.abort(signal.reason || new DOMException('Operation aborted.'));
break;
} else {
signal.addEventListener('abort', ({ target }) => {
controller.abort(target.reason || new DOMException('Operation aborted.'));
}, { signal: controller.signal });
}
}
return controller.signal;
};
}