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
Ricky committed
Apr 8, 2022 at 15:53 UTC
8dcedba15a4a0f5545e3b386355000208884b24f
4 files changed
+46
-9
packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js
-1
@@ -12,7 +12,6 @@
12
// Polyfills for test environment
13
global.ReadableStream = require('web-streams-polyfill/ponyfill/es6').ReadableStream;
14
global.TextEncoder = require('util').TextEncoder;
15
-global.AbortController = require('abort-controller');
15
16
let React;
17
let ReactDOMFizzServer;
packages/react-reconciler/src/ReactFiberCacheComponent.new.js
+23
-2
@@ -15,8 +15,29 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
15
import {pushProvider, popProvider} from './ReactFiberNewContext.new';
16
import * as Scheduler from 'scheduler';
17
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
+
39
export type Cache = {|
19
- controller: AbortController,
40
+ controller: AbortControllerLocal,
41
data: Map<() => mixed, mixed>,
42
refCount: number,
43
|};
@@ -66,7 +87,7 @@ export function createCache(): Cache {
87
return (null: any);
88
}
89
const cache: Cache = {
69
- controller: new AbortController(),
90
+ controller: new AbortControllerLocal(),
91
data: new Map(),
92
refCount: 0,
93
};
packages/react-reconciler/src/ReactFiberCacheComponent.old.js
+23
-2
@@ -15,8 +15,29 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
15
import {pushProvider, popProvider} from './ReactFiberNewContext.old';
16
import * as Scheduler from 'scheduler';
17
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
+
39
export type Cache = {|
19
- controller: AbortController,
40
+ controller: AbortControllerLocal,
41
data: Map<() => mixed, mixed>,
42
refCount: number,
43
|};
@@ -66,7 +87,7 @@ export function createCache(): Cache {
87
return (null: any);
88
}
89
const cache: Cache = {
69
- controller: new AbortController(),
90
+ controller: new AbortControllerLocal(),
91
data: new Map(),
92
refCount: 0,
93
};
scripts/jest/setupEnvironment.js
-4
@@ -1,7 +1,5 @@
1
/* eslint-disable */
2
3
-const AbortController = require('abort-controller');
4
-
3
const NODE_ENV = process.env.NODE_ENV;
4
if (NODE_ENV !== 'development' && NODE_ENV !== 'production') {
5
throw new Error('NODE_ENV must either be set to development or production.');
@@ -23,8 +21,6 @@ global.__EXPERIMENTAL__ =
21
22
global.__VARIANT__ = !!process.env.VARIANT;
23
26
-global.AbortController = AbortController;
27
-
24
if (typeof window !== 'undefined') {
25
global.requestIdleCallback = function(callback) {
26
return setTimeout(() => {