Skip to content

Commit 4f5545b

Browse files
committed
Use RootTag field to configure type of root
There are three types of roots: Legacy, Batched, and Concurrent.
1 parent 944dd85 commit 4f5545b

File tree

13 files changed

+73
-74
lines changed

13 files changed

+73
-74
lines changed

packages/react-art/src/ReactART.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import React from 'react';
99
import ReactVersion from 'shared/ReactVersion';
10+
import {LegacyRoot} from 'shared/ReactRootTags';
1011
import {
1112
createContainer,
1213
updateContainer,
@@ -65,7 +66,7 @@ class Surface extends React.Component {
6566

6667
this._surface = Mode.Surface(+width, +height, this._tagRef);
6768

68-
this._mountNode = createContainer(this._surface);
69+
this._mountNode = createContainer(this._surface, LegacyRoot, false);
6970
updateContainer(this.props.children, this._mountNode, this);
7071
}
7172

packages/react-dom/src/client/ReactDOM.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import type {ReactNodeList} from 'shared/ReactTypes';
11+
import type {RootTag} from 'shared/ReactRootTags';
1112
// TODO: This type is shared between the reconciler and ReactDOM, but will
1213
// eventually be lifted out to the renderer.
1314
import type {
@@ -52,6 +53,7 @@ import {
5253
accumulateTwoPhaseDispatches,
5354
accumulateDirectDispatches,
5455
} from 'events/EventPropagators';
56+
import {LegacyRoot, ConcurrentRoot} from 'shared/ReactRootTags';
5557
import {has as hasInstance} from 'shared/ReactInstanceMap';
5658
import ReactVersion from 'shared/ReactVersion';
5759
import ReactSharedInternals from 'shared/ReactSharedInternals';
@@ -361,13 +363,8 @@ ReactWork.prototype._onCommit = function(): void {
361363
}
362364
};
363365

364-
function ReactRoot(
365-
container: DOMContainer,
366-
isConcurrent: boolean,
367-
hydrate: boolean,
368-
) {
369-
const isBatched = false;
370-
const root = createContainer(container, isBatched, isConcurrent, hydrate);
366+
function ReactRoot(container: DOMContainer, tag: RootTag, hydrate: boolean) {
367+
const root = createContainer(container, tag, hydrate);
371368
this._internalRoot = root;
372369
}
373370
ReactRoot.prototype.render = function(
@@ -532,9 +529,7 @@ function legacyCreateRootFromDOMContainer(
532529
);
533530
}
534531
}
535-
// Legacy roots are not async by default.
536-
const isConcurrent = false;
537-
return new ReactRoot(container, isConcurrent, shouldHydrate);
532+
return new ReactRoot(container, LegacyRoot, shouldHydrate);
538533
}
539534

540535
function legacyRenderSubtreeIntoContainer(
@@ -850,7 +845,7 @@ function createRoot(container: DOMContainer, options?: RootOptions): ReactRoot {
850845
container._reactHasBeenPassedToCreateRootDEV = true;
851846
}
852847
const hydrate = options != null && options.hydrate === true;
853-
return new ReactRoot(container, true, hydrate);
848+
return new ReactRoot(container, ConcurrentRoot, hydrate);
854849
}
855850

856851
if (enableStableConcurrentModeAPIs) {

packages/react-dom/src/fire/ReactFire.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// console.log('Hello from Fire entry point.');
1414

1515
import type {ReactNodeList} from 'shared/ReactTypes';
16+
import type {RootTag} from 'shared/ReactRootTags';
1617
// TODO: This type is shared between the reconciler and ReactDOM, but will
1718
// eventually be lifted out to the renderer.
1819
import type {
@@ -58,6 +59,7 @@ import {
5859
accumulateTwoPhaseDispatches,
5960
accumulateDirectDispatches,
6061
} from 'events/EventPropagators';
62+
import {LegacyRoot, ConcurrentRoot} from 'shared/ReactRootTags';
6163
import {has as hasInstance} from 'shared/ReactInstanceMap';
6264
import ReactVersion from 'shared/ReactVersion';
6365
import ReactSharedInternals from 'shared/ReactSharedInternals';
@@ -367,13 +369,8 @@ ReactWork.prototype._onCommit = function(): void {
367369
}
368370
};
369371

370-
function ReactRoot(
371-
container: DOMContainer,
372-
isConcurrent: boolean,
373-
hydrate: boolean,
374-
) {
375-
const isBatched = false;
376-
const root = createContainer(container, isBatched, isConcurrent, hydrate);
372+
function ReactRoot(container: DOMContainer, tag: RootTag, hydrate: boolean) {
373+
const root = createContainer(container, tag, hydrate);
377374
this._internalRoot = root;
378375
}
379376
ReactRoot.prototype.render = function(
@@ -538,9 +535,7 @@ function legacyCreateRootFromDOMContainer(
538535
);
539536
}
540537
}
541-
// Legacy roots are not async by default.
542-
const isConcurrent = false;
543-
return new ReactRoot(container, isConcurrent, shouldHydrate);
538+
return new ReactRoot(container, LegacyRoot, shouldHydrate);
544539
}
545540

546541
function legacyRenderSubtreeIntoContainer(
@@ -856,7 +851,7 @@ function createRoot(container: DOMContainer, options?: RootOptions): ReactRoot {
856851
container._reactHasBeenPassedToCreateRootDEV = true;
857852
}
858853
const hydrate = options != null && options.hydrate === true;
859-
return new ReactRoot(container, true, hydrate);
854+
return new ReactRoot(container, ConcurrentRoot, hydrate);
860855
}
861856

862857
if (enableStableConcurrentModeAPIs) {

packages/react-native-renderer/src/ReactFabric.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import ReactNativeComponent from './ReactNativeComponent';
3333
import {getClosestInstanceFromNode} from './ReactFabricComponentTree';
3434
import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
3535

36+
import {LegacyRoot} from 'shared/ReactRootTags';
3637
import ReactSharedInternals from 'shared/ReactSharedInternals';
3738
import getComponentName from 'shared/getComponentName';
3839
import warningWithoutStack from 'shared/warningWithoutStack';
@@ -119,7 +120,7 @@ const ReactFabric: ReactFabricType = {
119120
if (!root) {
120121
// TODO (bvaughn): If we decide to keep the wrapper component,
121122
// We could create a wrapper for containerTag as well to reduce special casing.
122-
root = createContainer(containerTag, false, false, false);
123+
root = createContainer(containerTag, LegacyRoot, false);
123124
roots.set(containerTag, root);
124125
}
125126
updateContainer(element, root, null, callback);

packages/react-native-renderer/src/ReactNativeRenderer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {getClosestInstanceFromNode} from './ReactNativeComponentTree';
4040
import {getInspectorDataForViewTag} from './ReactNativeFiberInspector';
4141
import {setNativeProps} from './ReactNativeRendererSharedExports';
4242

43+
import {LegacyRoot} from 'shared/ReactRootTags';
4344
import ReactSharedInternals from 'shared/ReactSharedInternals';
4445
import getComponentName from 'shared/getComponentName';
4546
import warningWithoutStack from 'shared/warningWithoutStack';
@@ -125,7 +126,7 @@ const ReactNativeRenderer: ReactNativeType = {
125126
if (!root) {
126127
// TODO (bvaughn): If we decide to keep the wrapper component,
127128
// We could create a wrapper for containerTag as well to reduce special casing.
128-
root = createContainer(containerTag, false, false, false);
129+
root = createContainer(containerTag, LegacyRoot, false);
129130
roots.set(containerTag, root);
130131
}
131132
updateContainer(element, root, null, callback);

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

+8-30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {Thenable} from 'react-reconciler/src/ReactFiberScheduler';
1818
import type {Fiber} from 'react-reconciler/src/ReactFiber';
1919
import type {UpdateQueue} from 'react-reconciler/src/ReactUpdateQueue';
2020
import type {ReactNodeList} from 'shared/ReactTypes';
21+
import type {RootTag} from 'shared/ReactRootTags';
2122

2223
import * as Scheduler from 'scheduler/unstable_mock';
2324
import {createPortal} from 'shared/ReactPortal';
@@ -32,6 +33,7 @@ import enqueueTask from 'shared/enqueueTask';
3233
import ReactSharedInternals from 'shared/ReactSharedInternals';
3334
import warningWithoutStack from 'shared/warningWithoutStack';
3435
import {enableEventAPI} from 'shared/ReactFeatureFlags';
36+
import {ConcurrentRoot, BatchedRoot, LegacyRoot} from 'shared/ReactRootTags';
3537

3638
type EventTargetChildElement = {
3739
type: string,
@@ -901,39 +903,27 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
901903
return getPendingChildren(container);
902904
},
903905

904-
getOrCreateRootContainer(
905-
rootID: string = DEFAULT_ROOT_ID,
906-
isBatched: boolean,
907-
isConcurrent: boolean,
908-
) {
906+
getOrCreateRootContainer(rootID: string = DEFAULT_ROOT_ID, tag: RootTag) {
909907
let root = roots.get(rootID);
910908
if (!root) {
911909
const container = {rootID: rootID, pendingChildren: [], children: []};
912910
rootContainers.set(rootID, container);
913-
root = NoopRenderer.createContainer(
914-
container,
915-
isBatched,
916-
isConcurrent,
917-
false,
918-
);
911+
root = NoopRenderer.createContainer(container, tag, false);
919912
roots.set(rootID, root);
920913
}
921914
return root.current.stateNode.containerInfo;
922915
},
923916

924917
// TODO: Replace ReactNoop.render with createRoot + root.render
925918
createRoot() {
926-
const isBatched = true;
927-
const isConcurrent = true;
928919
const container = {
929920
rootID: '' + idCounter++,
930921
pendingChildren: [],
931922
children: [],
932923
};
933924
const fiberRoot = NoopRenderer.createContainer(
934925
container,
935-
isBatched,
936-
isConcurrent,
926+
ConcurrentRoot,
937927
false,
938928
);
939929
return {
@@ -951,17 +941,14 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
951941
},
952942

953943
createSyncRoot() {
954-
const isBatched = true;
955-
const isConcurrent = false;
956944
const container = {
957945
rootID: '' + idCounter++,
958946
pendingChildren: [],
959947
children: [],
960948
};
961949
const fiberRoot = NoopRenderer.createContainer(
962950
container,
963-
isBatched,
964-
isConcurrent,
951+
BatchedRoot,
965952
false,
966953
);
967954
return {
@@ -1003,13 +990,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
1003990

1004991
renderLegacySyncRoot(element: React$Element<any>, callback: ?Function) {
1005992
const rootID = DEFAULT_ROOT_ID;
1006-
const isBatched = false;
1007-
const isConcurrent = false;
1008-
const container = ReactNoop.getOrCreateRootContainer(
1009-
rootID,
1010-
isBatched,
1011-
isConcurrent,
1012-
);
993+
const container = ReactNoop.getOrCreateRootContainer(rootID, LegacyRoot);
1013994
const root = roots.get(container.rootID);
1014995
NoopRenderer.updateContainer(element, root, null, callback);
1015996
},
@@ -1019,12 +1000,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
10191000
rootID: string,
10201001
callback: ?Function,
10211002
) {
1022-
const isBatched = true;
1023-
const isConcurrent = true;
10241003
const container = ReactNoop.getOrCreateRootContainer(
10251004
rootID,
1026-
isBatched,
1027-
isConcurrent,
1005+
ConcurrentRoot,
10281006
);
10291007
const root = roots.get(container.rootID);
10301008
NoopRenderer.updateContainer(element, root, null, callback);

packages/react-reconciler/src/ReactFiber.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
ReactEventComponent,
1616
ReactEventTarget,
1717
} from 'shared/ReactTypes';
18+
import type {RootTag} from 'shared/ReactRootTags';
1819
import type {WorkTag} from 'shared/ReactWorkTags';
1920
import type {TypeOfMode} from './ReactTypeOfMode';
2021
import type {SideEffectTag} from 'shared/ReactSideEffectTags';
@@ -27,6 +28,7 @@ import invariant from 'shared/invariant';
2728
import warningWithoutStack from 'shared/warningWithoutStack';
2829
import {enableProfilerTimer, enableEventAPI} from 'shared/ReactFeatureFlags';
2930
import {NoEffect} from 'shared/ReactSideEffectTags';
31+
import {ConcurrentRoot, BatchedRoot} from 'shared/ReactRootTags';
3032
import {
3133
IndeterminateComponent,
3234
ClassComponent,
@@ -435,14 +437,11 @@ export function createWorkInProgress(
435437
return workInProgress;
436438
}
437439

438-
export function createHostRootFiber(
439-
isBatched: boolean,
440-
isConcurrent: boolean,
441-
): Fiber {
440+
export function createHostRootFiber(tag: RootTag): Fiber {
442441
let mode;
443-
if (isConcurrent) {
442+
if (tag === ConcurrentRoot) {
444443
mode = ConcurrentMode | BatchedMode | StrictMode;
445-
} else if (isBatched) {
444+
} else if (tag === BatchedRoot) {
446445
mode = BatchedMode | StrictMode;
447446
} else {
448447
mode = NoMode;

packages/react-reconciler/src/ReactFiberReconciler.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import type {Fiber} from './ReactFiber';
1111
import type {FiberRoot} from './ReactFiberRoot';
12+
import type {RootTag} from 'shared/ReactRootTags';
1213
import type {
1314
Instance,
1415
TextInstance,
@@ -273,11 +274,10 @@ function findHostInstanceWithWarning(
273274

274275
export function createContainer(
275276
containerInfo: Container,
276-
isBatched: boolean,
277-
isConcurrent: boolean,
277+
tag: RootTag,
278278
hydrate: boolean,
279279
): OpaqueRoot {
280-
return createFiberRoot(containerInfo, isBatched, isConcurrent, hydrate);
280+
return createFiberRoot(containerInfo, tag, hydrate);
281281
}
282282

283283
export function updateContainer(

packages/react-reconciler/src/ReactFiberRoot.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import type {Fiber} from './ReactFiber';
1111
import type {ExpirationTime} from './ReactFiberExpirationTime';
12+
import type {RootTag} from 'shared/ReactRootTags';
1213
import type {TimeoutHandle, NoTimeout} from './ReactFiberHostConfig';
1314
import type {Thenable} from './ReactFiberScheduler';
1415
import type {Interaction} from 'scheduler/src/Tracing';
@@ -30,6 +31,9 @@ export type Batch = {
3031
export type PendingInteractionMap = Map<ExpirationTime, Set<Interaction>>;
3132

3233
type BaseFiberRootProperties = {|
34+
// The type of root (legacy, batched, concurrent, etc.)
35+
tag: RootTag,
36+
3337
// Any additional information from the host associated with this root.
3438
containerInfo: any,
3539
// Used only by persistent updates.
@@ -89,7 +93,8 @@ export type FiberRoot = {
8993
...ProfilingOnlyFiberRootProperties,
9094
};
9195

92-
function FiberRootNode(containerInfo, hydrate) {
96+
function FiberRootNode(containerInfo, tag, hydrate) {
97+
this.tag = tag;
9398
this.current = null;
9499
this.containerInfo = containerInfo;
95100
this.pendingChildren = null;
@@ -116,15 +121,14 @@ function FiberRootNode(containerInfo, hydrate) {
116121

117122
export function createFiberRoot(
118123
containerInfo: any,
119-
isBatched: boolean,
120-
isConcurrent: boolean,
124+
tag: RootTag,
121125
hydrate: boolean,
122126
): FiberRoot {
123-
const root: FiberRoot = (new FiberRootNode(containerInfo, hydrate): any);
127+
const root: FiberRoot = (new FiberRootNode(containerInfo, tag, hydrate): any);
124128

125129
// Cyclic construction. This cheats the type system right now because
126130
// stateNode is any.
127-
const uninitializedFiber = createHostRootFiber(isBatched, isConcurrent);
131+
const uninitializedFiber = createHostRootFiber(tag);
128132
root.current = uninitializedFiber;
129133
uninitializedFiber.stateNode = root;
130134

packages/react-reconciler/src/ReactTypeOfMode.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type TypeOfMode = number;
1111

1212
export const NoMode = 0b0000;
1313
export const StrictMode = 0b0001;
14+
// TODO: Remove BatchedMode and ConcurrentMode by reading from the root
15+
// tag instead
1416
export const BatchedMode = 0b0010;
1517
export const ConcurrentMode = 0b0100;
1618
export const ProfileMode = 0b1000;

0 commit comments

Comments
 (0)