Skip to content

Commit ec01142

Browse files
author
Brian Vaughn
committed
Response to PR feedback:
1. Moved mutableSources root option to hydrationOptions object 2. Only initialize root mutableSourceEagerHydrationData if supportsHydration config is true 3. Lazily initialize mutableSourceEagerHydrationData on root object
1 parent eb3e705 commit ec01142

9 files changed

+68
-31
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export type RootOptions = {
2424
hydrationOptions?: {
2525
onHydrated?: (suspenseNode: Comment) => void,
2626
onDeleted?: (suspenseNode: Comment) => void,
27+
mutableSources?: Array<MutableSource<any>>,
2728
...
2829
},
29-
mutableSources?: Array<MutableSource<any>>,
3030
...
3131
};
3232

@@ -126,7 +126,11 @@ function createRootImpl(
126126
const hydrate = options != null && options.hydrate === true;
127127
const hydrationCallbacks =
128128
(options != null && options.hydrationOptions) || null;
129-
const mutableSources = (options != null && options.mutableSources) || null;
129+
const mutableSources =
130+
(options != null &&
131+
options.hydrationOptions != null &&
132+
options.hydrationOptions.mutableSources) ||
133+
null;
130134
const root = createContainer(container, tag, hydrate, hydrationCallbacks);
131135
markContainerAsRoot(root.current, container);
132136
const containerNodeType = container.nodeType;

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import {
127127
isSuspenseInstancePending,
128128
isSuspenseInstanceFallback,
129129
registerSuspenseInstanceRetry,
130+
supportsHydration,
130131
} from './ReactFiberHostConfig';
131132
import type {SuspenseInstance} from './ReactFiberHostConfig';
132133
import {shouldSuspend} from './ReactFiberReconciler';
@@ -1076,14 +1077,18 @@ function updateHostRoot(current, workInProgress, renderLanes) {
10761077
// be any children to hydrate which is effectively the same thing as
10771078
// not hydrating.
10781079

1079-
const mutableSourceEagerHydrationData =
1080-
root.mutableSourceEagerHydrationData;
1081-
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1082-
const mutableSource = ((mutableSourceEagerHydrationData[
1083-
i
1084-
]: any): MutableSource<any>);
1085-
const version = mutableSourceEagerHydrationData[i + 1];
1086-
setWorkInProgressVersion(mutableSource, version);
1080+
if (supportsHydration) {
1081+
const mutableSourceEagerHydrationData =
1082+
root.mutableSourceEagerHydrationData;
1083+
if (mutableSourceEagerHydrationData != null) {
1084+
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1085+
const mutableSource = ((mutableSourceEagerHydrationData[
1086+
i
1087+
]: any): MutableSource<any>);
1088+
const version = mutableSourceEagerHydrationData[i + 1];
1089+
setWorkInProgressVersion(mutableSource, version);
1090+
}
1091+
}
10871092
}
10881093

10891094
const child = mountChildFibers(

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ import {
115115
isSuspenseInstancePending,
116116
isSuspenseInstanceFallback,
117117
registerSuspenseInstanceRetry,
118+
supportsHydration,
118119
} from './ReactFiberHostConfig';
119120
import type {SuspenseInstance} from './ReactFiberHostConfig';
120121
import {shouldSuspend} from './ReactFiberReconciler';
@@ -1043,14 +1044,18 @@ function updateHostRoot(current, workInProgress, renderExpirationTime) {
10431044
// be any children to hydrate which is effectively the same thing as
10441045
// not hydrating.
10451046

1046-
const mutableSourceEagerHydrationData =
1047-
root.mutableSourceEagerHydrationData;
1048-
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1049-
const mutableSource = ((mutableSourceEagerHydrationData[
1050-
i
1051-
]: any): MutableSource<any>);
1052-
const version = mutableSourceEagerHydrationData[i + 1];
1053-
setWorkInProgressVersion(mutableSource, version);
1047+
if (supportsHydration) {
1048+
const mutableSourceEagerHydrationData =
1049+
root.mutableSourceEagerHydrationData;
1050+
if (mutableSourceEagerHydrationData != null) {
1051+
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1052+
const mutableSource = ((mutableSourceEagerHydrationData[
1053+
i
1054+
]: any): MutableSource<any>);
1055+
const version = mutableSourceEagerHydrationData[i + 1];
1056+
setWorkInProgressVersion(mutableSource, version);
1057+
}
1058+
}
10541059
}
10551060

10561061
const child = mountChildFibers(

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import type {FiberRoot, SuspenseHydrationCallbacks} from './ReactInternalTypes';
1111
import type {RootTag} from './ReactRootTags';
1212

13-
import {noTimeout} from './ReactFiberHostConfig';
13+
import {noTimeout, supportsHydration} from './ReactFiberHostConfig';
1414
import {createHostRootFiber} from './ReactFiber.new';
1515
import {
1616
NoLanes,
@@ -51,7 +51,9 @@ function FiberRootNode(containerInfo, tag, hydrate) {
5151
this.entangledLanes = NoLanes;
5252
this.entanglements = createLaneMap(NoLanes);
5353

54-
this.mutableSourceEagerHydrationData = [];
54+
if (supportsHydration) {
55+
this.mutableSourceEagerHydrationData = null;
56+
}
5557

5658
if (enableSchedulerTracing) {
5759
this.interactionThreadID = unstable_getThreadID();

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {FiberRoot, SuspenseHydrationCallbacks} from './ReactInternalTypes';
1111
import type {ExpirationTime} from './ReactFiberExpirationTime.old';
1212
import type {RootTag} from './ReactRootTags';
1313

14-
import {noTimeout} from './ReactFiberHostConfig';
14+
import {noTimeout, supportsHydration} from './ReactFiberHostConfig';
1515
import {createHostRootFiber} from './ReactFiber.old';
1616
import {NoWork} from './ReactFiberExpirationTime.old';
1717
import {
@@ -45,7 +45,10 @@ function FiberRootNode(containerInfo, tag, hydrate) {
4545
this.lastPingedTime = NoWork;
4646
this.lastExpiredTime = NoWork;
4747
this.mutableSourceLastPendingUpdateTime = NoWork;
48-
this.mutableSourceEagerHydrationData = [];
48+
49+
if (supportsHydration) {
50+
this.mutableSourceEagerHydrationData = null;
51+
}
4952

5053
if (enableSchedulerTracing) {
5154
this.interactionThreadID = unstable_getThreadID();

packages/react-reconciler/src/ReactInternalTypes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ type BaseFiberRootProperties = {|
249249
mutableSourceLastPendingUpdateTime: ExpirationTime,
250250

251251
// Used by useMutableSource hook to avoid tearing during hydrtaion.
252-
mutableSourceEagerHydrationData: Array<
252+
mutableSourceEagerHydrationData?: Array<
253253
MutableSource<any> | MutableSourceVersion,
254-
>,
254+
> | null,
255255

256256
// Only used by new reconciler
257257

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,9 @@ export function registerMutableSourceForHydration(
100100

101101
// TODO Clear this data once all pending hydration work is finished.
102102
// Retaining it forever may interfere with GC.
103-
root.mutableSourceEagerHydrationData.push(mutableSource, version);
103+
if (root.mutableSourceEagerHydrationData == null) {
104+
root.mutableSourceEagerHydrationData = [mutableSource, version];
105+
} else {
106+
root.mutableSourceEagerHydrationData.push(mutableSource, version);
107+
}
104108
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,9 @@ export function registerMutableSourceForHydration(
130130

131131
// TODO Clear this data once all pending hydration work is finished.
132132
// Retaining it forever may interfere with GC.
133-
root.mutableSourceEagerHydrationData.push(mutableSource, version);
133+
if (root.mutableSourceEagerHydrationData == null) {
134+
root.mutableSourceEagerHydrationData = [mutableSource, version];
135+
} else {
136+
root.mutableSourceEagerHydrationData.push(mutableSource, version);
137+
}
134138
}

packages/react-reconciler/src/__tests__/useMutableSourceHydration-test.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ describe('useMutableSourceHydration', () => {
159159

160160
const root = ReactDOM.unstable_createRoot(container, {
161161
hydrate: true,
162-
mutableSources: [mutableSource],
162+
hydrationOptions: {
163+
mutableSources: [mutableSource],
164+
},
163165
});
164166
act(() => {
165167
root.render(<TestComponent />);
@@ -194,7 +196,9 @@ describe('useMutableSourceHydration', () => {
194196

195197
const root = ReactDOM.unstable_createRoot(container, {
196198
hydrate: true,
197-
mutableSources: [mutableSource],
199+
hydrationOptions: {
200+
mutableSources: [mutableSource],
201+
},
198202
});
199203
expect(() => {
200204
act(() => {
@@ -244,7 +248,9 @@ describe('useMutableSourceHydration', () => {
244248

245249
const root = ReactDOM.unstable_createRoot(container, {
246250
hydrate: true,
247-
mutableSources: [mutableSource],
251+
hydrationOptions: {
252+
mutableSources: [mutableSource],
253+
},
248254
});
249255
expect(() => {
250256
act(() => {
@@ -295,7 +301,9 @@ describe('useMutableSourceHydration', () => {
295301

296302
const root = ReactDOM.unstable_createRoot(container, {
297303
hydrate: true,
298-
mutableSources: [mutableSource],
304+
hydrationOptions: {
305+
mutableSources: [mutableSource],
306+
},
299307
});
300308
expect(() => {
301309
act(() => {
@@ -361,7 +369,9 @@ describe('useMutableSourceHydration', () => {
361369

362370
const root = ReactDOM.unstable_createRoot(container, {
363371
hydrate: true,
364-
mutableSources: [mutableSource],
372+
hydrationOptions: {
373+
mutableSources: [mutableSource],
374+
},
365375
});
366376
expect(() => {
367377
act(() => {

0 commit comments

Comments
 (0)