Skip to content

Commit 1a74726

Browse files
authoredFeb 12, 2021
Add supportsMicrotasks to the host config (#20809)
* Add `supportsMicrotasks` to the host config Only certain renderers support scheduling a microtask, so we need a renderer specific flag that we can toggle. That way it's off for some renderers and on for others. I copied the approach we use for the other optional parts of the host config, like persistent mode and test selectors. Why isn't the feature flag sufficient? The feature flag modules, confusingly, are not renderer-specific, at least when running the our tests against the source files. They are meant to correspond to a release channel, not a renderer, but we got confused at some point and haven't cleaned it up. For example, when we run `yarn test`, Jest loads the flags from the default `ReactFeatureFlags.js` module, even when we import the React Native renderer — but in the actual builds, we load a different feature flag module, `ReactFeatureFlags.native-oss.js.` There's no way in our current Jest load a different host config for each renderer, because they all just import the same module. We should solve this by creating separate Jest project for each renderer, so that the flags loaded when running against source are the same ones that we use in the compiled bundles. The feature flag (`enableDiscreteMicrotasks`) still exists — it's used to set the React DOM host config's `supportsMicrotasks` flag to `true`. (Same for React Noop) The important part is that turning on the feature flag does *not* affect the other renderers, like React Native. The host config will likely outlive the feature flag, too, since the feature flag only exists so we can gradually roll it out and measure the impact in production; once we do, we'll remove it. Whereas the host config flag may continue to be used to disable the discrete microtask behavior for RN, because RN will likely use a native (non-JavaScript) API to schedule its tasks. * Add `supportsMicrotask` to react-reconciler README
1 parent 696e736 commit 1a74726

File tree

12 files changed

+62
-34
lines changed

12 files changed

+62
-34
lines changed
 

‎packages/react-art/src/ReactARTHostConfig.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoPersistence';
249249
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration';
250250
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes';
251251
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors';
252+
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
252253

253254
export function appendInitialChild(parentInstance, child) {
254255
if (typeof child === 'string') {
@@ -338,9 +339,6 @@ export function getChildHostContext() {
338339
export const scheduleTimeout = setTimeout;
339340
export const cancelTimeout = clearTimeout;
340341
export const noTimeout = -1;
341-
export function scheduleMicrotask(callback: Function) {
342-
invariant(false, 'Not implemented.');
343-
}
344342

345343
export function shouldSetTextContent(type, props) {
346344
return (

‎packages/react-dom/src/client/ReactDOMHostConfig.js

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
enableCreateEventHandleAPI,
6868
enableScopeAPI,
6969
enableNewReconciler,
70+
enableDiscreteEventMicroTasks,
7071
} from 'shared/ReactFeatureFlags';
7172
import {HostComponent, HostText} from 'react-reconciler/src/ReactWorkTags';
7273
import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem';
@@ -399,6 +400,11 @@ export const scheduleTimeout: any =
399400
export const cancelTimeout: any =
400401
typeof clearTimeout === 'function' ? clearTimeout : (undefined: any);
401402
export const noTimeout = -1;
403+
404+
// -------------------
405+
// Microtasks
406+
// -------------------
407+
export const supportsMicrotasks = enableDiscreteEventMicroTasks;
402408
export const scheduleMicrotask: any =
403409
typeof queueMicrotask === 'function'
404410
? queueMicrotask

‎packages/react-native-renderer/src/ReactFabricHostConfig.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMutation';
193193
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration';
194194
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes';
195195
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors';
196+
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
196197

197198
export function appendInitialChild(
198199
parentInstance: Instance,
@@ -360,9 +361,6 @@ export const warnsIfNotActing = false;
360361
export const scheduleTimeout = setTimeout;
361362
export const cancelTimeout = clearTimeout;
362363
export const noTimeout = -1;
363-
export function scheduleMicrotask(callback: Function) {
364-
invariant(false, 'Not implemented.');
365-
}
366364

367365
// -------------------
368366
// Persistence

‎packages/react-native-renderer/src/ReactNativeHostConfig.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoPersistence';
9797
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration';
9898
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes';
9999
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors';
100+
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
100101

101102
export function appendInitialChild(
102103
parentInstance: Instance,
@@ -255,9 +256,6 @@ export const warnsIfNotActing = true;
255256
export const scheduleTimeout = setTimeout;
256257
export const cancelTimeout = clearTimeout;
257258
export const noTimeout = -1;
258-
export function scheduleMicrotask(callback: Function) {
259-
invariant(false, 'Not implemented.');
260-
}
261259

262260
export function shouldSetTextContent(type: string, props: Props): boolean {
263261
// TODO (bvaughn) Revisit this decision.

‎packages/react-noop-renderer/src/createReactNoop.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import {
2727
LegacyRoot,
2828
} from 'react-reconciler/src/ReactRootTags';
2929

30-
import {enableNativeEventPriorityInference} from 'shared/ReactFeatureFlags';
30+
import {
31+
enableNativeEventPriorityInference,
32+
enableDiscreteEventMicroTasks,
33+
} from 'shared/ReactFeatureFlags';
3134
import ReactSharedInternals from 'shared/ReactSharedInternals';
3235
import enqueueTask from 'shared/enqueueTask';
3336
const {IsSomeRendererActing} = ReactSharedInternals;
@@ -372,6 +375,8 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
372375
scheduleTimeout: setTimeout,
373376
cancelTimeout: clearTimeout,
374377
noTimeout: -1,
378+
379+
supportsMicrotasks: enableDiscreteEventMicroTasks,
375380
scheduleMicrotask:
376381
typeof queueMicrotask === 'function'
377382
? queueMicrotask

‎packages/react-reconciler/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,12 @@ You can proxy this to `clearTimeout` or its equivalent in your environment.
203203

204204
This is a property (not a function) that should be set to something that can never be a valid timeout ID. For example, you can set it to `-1`.
205205

206+
#### `supportsMicrotask`
207+
208+
Set this to true to indicate that your renderer supports `scheduleMicrotask`. We use microtasks as part of our discrete event implementation in React DOM. If you're not sure if your renderer should support this, you probably should. The option to not implement `scheduleMicrotask` exists so that platforms with more control over user events, like React Native, can choose to use a different mechanism.
206209
#### `scheduleMicrotask(fn)`
207210

208-
You can proxy this to `queueMicrotask` or its equivalent in your environment.
211+
Optional. You can proxy this to `queueMicrotask` or its equivalent in your environment.
209212

210213
#### `isPrimaryRenderer`
211214

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import invariant from 'shared/invariant';
11+
12+
// Renderers that don't support microtasks
13+
// can re-export everything from this module.
14+
15+
function shim(...args: any) {
16+
invariant(
17+
false,
18+
'The current renderer does not support microtasks. ' +
19+
'This error is likely caused by a bug in React. ' +
20+
'Please file an issue.',
21+
);
22+
}
23+
24+
// Test selectors (when unsupported)
25+
export const supportsMicrotasks = false;
26+
export const scheduleMicrotask = shim;

‎packages/react-reconciler/src/ReactFiberWorkLoop.new.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ import {
9393
warnsIfNotActing,
9494
afterActiveInstanceBlur,
9595
clearContainer,
96-
scheduleMicrotask,
9796
getCurrentEventPriority,
97+
supportsMicrotasks,
98+
scheduleMicrotask,
9899
} from './ReactFiberHostConfig';
99100

100101
import {
@@ -219,7 +220,6 @@ import {
219220
syncNestedUpdateFlag,
220221
} from './ReactProfilerTimer.new';
221222

222-
import {enableDiscreteEventMicroTasks} from 'shared/ReactFeatureFlags';
223223
// DEV stuff
224224
import getComponentName from 'shared/getComponentName';
225225
import ReactStrictModeWarnings from './ReactStrictModeWarnings.new';
@@ -756,7 +756,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
756756
performSyncWorkOnRoot.bind(null, root),
757757
);
758758
} else if (
759-
enableDiscreteEventMicroTasks &&
759+
supportsMicrotasks &&
760760
newCallbackPriority === InputDiscreteLanePriority
761761
) {
762762
scheduleMicrotask(performSyncWorkOnRoot.bind(null, root));

‎packages/react-reconciler/src/ReactFiberWorkLoop.old.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ import {
9393
warnsIfNotActing,
9494
afterActiveInstanceBlur,
9595
clearContainer,
96-
scheduleMicrotask,
9796
getCurrentEventPriority,
97+
supportsMicrotasks,
98+
scheduleMicrotask,
9899
} from './ReactFiberHostConfig';
99100

100101
import {
@@ -219,7 +220,6 @@ import {
219220
syncNestedUpdateFlag,
220221
} from './ReactProfilerTimer.old';
221222

222-
import {enableDiscreteEventMicroTasks} from 'shared/ReactFeatureFlags';
223223
// DEV stuff
224224
import getComponentName from 'shared/getComponentName';
225225
import ReactStrictModeWarnings from './ReactStrictModeWarnings.old';
@@ -756,7 +756,7 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
756756
performSyncWorkOnRoot.bind(null, root),
757757
);
758758
} else if (
759-
enableDiscreteEventMicroTasks &&
759+
supportsMicrotasks &&
760760
newCallbackPriority === InputDiscreteLanePriority
761761
) {
762762
scheduleMicrotask(performSyncWorkOnRoot.bind(null, root));

‎packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export const shouldSetTextContent = $$$hostConfig.shouldSetTextContent;
5454
export const createTextInstance = $$$hostConfig.createTextInstance;
5555
export const scheduleTimeout = $$$hostConfig.scheduleTimeout;
5656
export const cancelTimeout = $$$hostConfig.cancelTimeout;
57-
export const scheduleMicrotask = $$$hostConfig.scheduleMicrotask;
5857
export const noTimeout = $$$hostConfig.noTimeout;
5958
export const now = $$$hostConfig.now;
6059
export const isPrimaryRenderer = $$$hostConfig.isPrimaryRenderer;
@@ -75,6 +74,13 @@ export const prepareScopeUpdate = $$$hostConfig.preparePortalMount;
7574
export const getInstanceFromScope = $$$hostConfig.getInstanceFromScope;
7675
export const getCurrentEventPriority = $$$hostConfig.getCurrentEventPriority;
7776

77+
// -------------------
78+
// Microtasks
79+
// (optional)
80+
// -------------------
81+
export const supportsMicrotasks = $$$hostConfig.supportsMicrotasks;
82+
export const scheduleMicrotask = $$$hostConfig.scheduleMicrotask;
83+
7884
// -------------------
7985
// Test selectors
8086
// (optional)

‎packages/react-test-renderer/src/ReactTestHostConfig.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export type RendererInspectionConfig = $ReadOnly<{||}>;
5858
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoPersistence';
5959
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration';
6060
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors';
61+
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
6162

6263
const NO_CONTEXT = {};
6364
const UPDATE_SIGNAL = {};
@@ -230,21 +231,7 @@ export const warnsIfNotActing = true;
230231

231232
export const scheduleTimeout = setTimeout;
232233
export const cancelTimeout = clearTimeout;
233-
export const scheduleMicrotask =
234-
typeof queueMicrotask === 'function'
235-
? queueMicrotask
236-
: typeof Promise !== 'undefined'
237-
? (callback: Function) =>
238-
Promise.resolve(null)
239-
.then(callback)
240-
.catch(handleErrorInNextTick)
241-
: scheduleTimeout; // TODO: Determine the best fallback here.
242-
243-
function handleErrorInNextTick(error) {
244-
setTimeout(() => {
245-
throw error;
246-
});
247-
}
234+
248235
export const noTimeout = -1;
249236

250237
// -------------------

‎scripts/error-codes/codes.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -373,5 +373,6 @@
373373
"382": "This query has received more parameters than the last time the same query was used. Always pass the exact number of parameters that the query needs.",
374374
"383": "This query has received fewer parameters than the last time the same query was used. Always pass the exact number of parameters that the query needs.",
375375
"384": "Refreshing the cache is not supported in Server Components.",
376-
"385": "A mutable source was mutated while the %s component was rendering. This is not supported. Move any mutations into event handlers or effects."
376+
"385": "A mutable source was mutated while the %s component was rendering. This is not supported. Move any mutations into event handlers or effects.",
377+
"386": "The current renderer does not support microtasks. This error is likely caused by a bug in React. Please file an issue."
377378
}

0 commit comments

Comments
 (0)
Please sign in to comment.