Skip to content

Commit 8dcedba

Browse files
authored
Add fallback shim for AbortController (#24285)
* Add fallback shim for AbortController * Replace shim with a minimal stub * replace-fork * Better minification * Fix flow * Even smaller * replace-fork * Revert back to object constructor * replace-fork
1 parent b86baa1 commit 8dcedba

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// Polyfills for test environment
1313
global.ReadableStream = require('web-streams-polyfill/ponyfill/es6').ReadableStream;
1414
global.TextEncoder = require('util').TextEncoder;
15-
global.AbortController = require('abort-controller');
1615

1716
let React;
1817
let ReactDOMFizzServer;

packages/react-reconciler/src/ReactFiberCacheComponent.new.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,29 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
1515
import {pushProvider, popProvider} from './ReactFiberNewContext.new';
1616
import * as Scheduler from 'scheduler';
1717

18+
// In environments without AbortController (e.g. tests)
19+
// replace it with a lightweight shim that only has the features we use.
20+
const AbortControllerLocal = enableCache
21+
? typeof AbortController !== 'undefined'
22+
? AbortController
23+
: (function AbortControllerShim() {
24+
const listeners = [];
25+
const signal = (this.signal = {
26+
aborted: false,
27+
addEventListener: (type, listener) => {
28+
listeners.push(listener);
29+
},
30+
});
31+
32+
this.abort = () => {
33+
signal.aborted = true;
34+
listeners.forEach(listener => listener());
35+
};
36+
}: AbortController)
37+
: (null: any);
38+
1839
export type Cache = {|
19-
controller: AbortController,
40+
controller: AbortControllerLocal,
2041
data: Map<() => mixed, mixed>,
2142
refCount: number,
2243
|};
@@ -66,7 +87,7 @@ export function createCache(): Cache {
6687
return (null: any);
6788
}
6889
const cache: Cache = {
69-
controller: new AbortController(),
90+
controller: new AbortControllerLocal(),
7091
data: new Map(),
7192
refCount: 0,
7293
};

packages/react-reconciler/src/ReactFiberCacheComponent.old.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,29 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
1515
import {pushProvider, popProvider} from './ReactFiberNewContext.old';
1616
import * as Scheduler from 'scheduler';
1717

18+
// In environments without AbortController (e.g. tests)
19+
// replace it with a lightweight shim that only has the features we use.
20+
const AbortControllerLocal = enableCache
21+
? typeof AbortController !== 'undefined'
22+
? AbortController
23+
: (function AbortControllerShim() {
24+
const listeners = [];
25+
const signal = (this.signal = {
26+
aborted: false,
27+
addEventListener: (type, listener) => {
28+
listeners.push(listener);
29+
},
30+
});
31+
32+
this.abort = () => {
33+
signal.aborted = true;
34+
listeners.forEach(listener => listener());
35+
};
36+
}: AbortController)
37+
: (null: any);
38+
1839
export type Cache = {|
19-
controller: AbortController,
40+
controller: AbortControllerLocal,
2041
data: Map<() => mixed, mixed>,
2142
refCount: number,
2243
|};
@@ -66,7 +87,7 @@ export function createCache(): Cache {
6687
return (null: any);
6788
}
6889
const cache: Cache = {
69-
controller: new AbortController(),
90+
controller: new AbortControllerLocal(),
7091
data: new Map(),
7192
refCount: 0,
7293
};

scripts/jest/setupEnvironment.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable */
22

3-
const AbortController = require('abort-controller');
4-
53
const NODE_ENV = process.env.NODE_ENV;
64
if (NODE_ENV !== 'development' && NODE_ENV !== 'production') {
75
throw new Error('NODE_ENV must either be set to development or production.');
@@ -23,8 +21,6 @@ global.__EXPERIMENTAL__ =
2321

2422
global.__VARIANT__ = !!process.env.VARIANT;
2523

26-
global.AbortController = AbortController;
27-
2824
if (typeof window !== 'undefined') {
2925
global.requestIdleCallback = function(callback) {
3026
return setTimeout(() => {

0 commit comments

Comments
 (0)