1 | import AbortSignal, { abortSignal, createAbortSignal } from './abortsignal';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export default class AbortController {
|
7 | |
8 |
|
9 |
|
10 | constructor() {
|
11 | signals.set(this, createAbortSignal());
|
12 | }
|
13 | |
14 |
|
15 |
|
16 | get signal() {
|
17 | return getSignal(this);
|
18 | }
|
19 | |
20 |
|
21 |
|
22 | abort() {
|
23 | abortSignal(getSignal(this));
|
24 | }
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 | const signals = new WeakMap();
|
30 |
|
31 |
|
32 |
|
33 | function 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 |
|
41 | Object.defineProperties(AbortController.prototype, {
|
42 | signal: { enumerable: true },
|
43 | abort: { enumerable: true },
|
44 | });
|
45 | if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
|
46 | Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
|
47 | configurable: true,
|
48 | value: 'AbortController',
|
49 | });
|
50 | }
|
51 | export { AbortController, AbortSignal };
|
52 |
|
\ | No newline at end of file |