Skip to content

Commit 8c8e3e5

Browse files
committed
feat: Add AbortSignal.any(signals), signal.throwIfAborted() and AbortSignal.timeout(time)
1 parent 0bbb4f1 commit 8c8e3e5

7 files changed

Lines changed: 650 additions & 295 deletions

File tree

packages/react-native/Libraries/Core/setUpXHR.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ polyfillGlobal('URL', () => require('../Blob/URL').URL);
3636
polyfillGlobal('URLSearchParams', () => require('../Blob/URL').URLSearchParams);
3737
polyfillGlobal(
3838
'AbortController',
39-
() => require('../../src/private/webapis/dom/abort-api/AbortController').AbortController, // flowlint-line untyped-import:off
39+
() =>
40+
require('../../src/private/webapis/dom/abort-api/AbortController')
41+
.AbortController, // flowlint-line untyped-import:off
4042
);
4143
polyfillGlobal(
4244
'AbortSignal',
4345
() =>
44-
require('../../src/private/webapis/dom/abort-api/AbortSignal').AbortSignal, // flowlint-line untyped-import:off
46+
require('../../src/private/webapis/dom/abort-api/AbortSignal')
47+
.AbortSignal_public, // flowlint-line untyped-import:off
4548
);
Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
/**
2+
* Based on abort-controller by Toru Nagashima
3+
* https://github.com/mysticatea/abort-controller
4+
*
5+
* Original work Copyright (c) 2017 Toru Nagashima
6+
* Modified work Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*
226
* @flow strict
327
* @format
428
*/
29+
30+
// flowlint unsafe-getters-setters:off
31+
532
import {AbortSignal, abortSignal, createAbortSignal} from './AbortSignal';
633

34+
const SIGNAL_KEY: symbol = Symbol('aborted');
35+
736
/**
837
* The AbortController.
938
* @see https://dom.spec.whatwg.org/#abortcontroller
@@ -13,35 +42,29 @@ export class AbortController {
1342
* Initialize this controller.
1443
*/
1544
constructor() {
16-
signals.set(this, createAbortSignal());
45+
this[SIGNAL_KEY] = createAbortSignal();
1746
}
1847

1948
/**
2049
* Returns the `AbortSignal` object associated with this object.
2150
*/
22-
// $FlowExpectedError[unsafe-getters-setters]
2351
get signal(): AbortSignal {
2452
return getSignal(this);
2553
}
2654

2755
/**
2856
* Abort and signal to any observers that the associated activity is to be aborted.
2957
*/
30-
abort(): void {
31-
abortSignal(getSignal(this));
58+
abort(reason: unknown): void {
59+
abortSignal(reason, getSignal(this));
3260
}
3361
}
3462

35-
/**
36-
* Associated signals.
37-
*/
38-
const signals = new WeakMap<AbortController, AbortSignal>()
39-
4063
/**
4164
* Get the associated signal of a given controller.
4265
*/
4366
function getSignal(controller: AbortController): AbortSignal {
44-
const signal = signals.get(controller)
67+
const signal = controller[SIGNAL_KEY];
4568
if (signal == null) {
4669
throw new TypeError(
4770
`Expected 'this' to be an 'AbortController' object, but got ${
@@ -50,20 +73,18 @@ function getSignal(controller: AbortController): AbortSignal {
5073
}`,
5174
);
5275
}
53-
return signal
76+
return signal;
5477
}
5578

5679
// Properties should be enumerable.
5780
//$FlowExpectedError[cannot-write]
5881
Object.defineProperties(AbortController.prototype, {
59-
signal: { enumerable: true },
60-
abort: { enumerable: true },
61-
})
82+
signal: {enumerable: true},
83+
abort: {enumerable: true},
84+
});
6285

63-
if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
64-
//$FlowExpectedError[cannot-write]
65-
Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
66-
configurable: true,
67-
value: 'AbortController',
68-
});
69-
}
86+
//$FlowExpectedError[cannot-write]
87+
Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
88+
configurable: true,
89+
value: 'AbortController',
90+
});

0 commit comments

Comments
 (0)