Skip to content

Commit 5912bea

Browse files
committed
Split mountChildrenRecursively into mountFiberRecursively
Instead of using a boolean flag we can just have this be a separate function and follow control flow.
1 parent 6c70be0 commit 5912bea

File tree

1 file changed

+88
-85
lines changed
  • packages/react-devtools-shared/src/backend/fiber

1 file changed

+88
-85
lines changed

packages/react-devtools-shared/src/backend/fiber/renderer.js

+88-85
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ export function attach(
10941094
hook.getFiberRoots(rendererID).forEach(root => {
10951095
currentRootID = getOrGenerateFiberInstance(root.current).id;
10961096
setRootPseudoKey(currentRootID, root.current);
1097-
mountChildrenRecursively(root.current, null, false, false);
1097+
mountFiberRecursively(root.current, null, false);
10981098
flushPendingEvents(root);
10991099
currentRootID = -1;
11001100
});
@@ -2231,101 +2231,108 @@ export function attach(
22312231
function mountChildrenRecursively(
22322232
firstChild: Fiber,
22332233
parentInstance: DevToolsInstance | null,
2234-
traverseSiblings: boolean,
22352234
traceNearestHostComponentUpdate: boolean,
2236-
) {
2235+
): void {
22372236
// Iterate over siblings rather than recursing.
22382237
// This reduces the chance of stack overflow for wide trees (e.g. lists with many items).
22392238
let fiber: Fiber | null = firstChild;
22402239
while (fiber !== null) {
2241-
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling).
2242-
// TODO: Do we really need to do this eagerly?
2243-
getOrGenerateFiberInstance(fiber);
2240+
mountFiberRecursively(
2241+
fiber,
2242+
parentInstance,
2243+
traceNearestHostComponentUpdate,
2244+
);
2245+
fiber = fiber.sibling;
2246+
}
2247+
}
22442248

2245-
if (__DEBUG__) {
2246-
debug('mountChildrenRecursively()', fiber, parentInstance);
2247-
}
2249+
function mountFiberRecursively(
2250+
fiber: Fiber,
2251+
parentInstance: DevToolsInstance | null,
2252+
traceNearestHostComponentUpdate: boolean,
2253+
): void {
2254+
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling).
2255+
// TODO: Do we really need to do this eagerly?
2256+
getOrGenerateFiberInstance(fiber);
22482257

2249-
// If we have the tree selection from previous reload, try to match this Fiber.
2250-
// Also remember whether to do the same for siblings.
2251-
const mightSiblingsBeOnTrackedPath =
2252-
updateTrackedPathStateBeforeMount(fiber);
2253-
2254-
const shouldIncludeInTree = !shouldFilterFiber(fiber);
2255-
const newParentInstance = shouldIncludeInTree
2256-
? recordMount(fiber, parentInstance)
2257-
: parentInstance;
2258-
2259-
if (traceUpdatesEnabled) {
2260-
if (traceNearestHostComponentUpdate) {
2261-
const elementType = getElementTypeForFiber(fiber);
2262-
// If an ancestor updated, we should mark the nearest host nodes for highlighting.
2263-
if (elementType === ElementTypeHostComponent) {
2264-
traceUpdatesForNodes.add(fiber.stateNode);
2265-
traceNearestHostComponentUpdate = false;
2266-
}
2267-
}
2258+
if (__DEBUG__) {
2259+
debug('mountChildrenRecursively()', fiber, parentInstance);
2260+
}
2261+
2262+
// If we have the tree selection from previous reload, try to match this Fiber.
2263+
// Also remember whether to do the same for siblings.
2264+
const mightSiblingsBeOnTrackedPath =
2265+
updateTrackedPathStateBeforeMount(fiber);
22682266

2269-
// We intentionally do not re-enable the traceNearestHostComponentUpdate flag in this branch,
2270-
// because we don't want to highlight every host node inside of a newly mounted subtree.
2267+
const shouldIncludeInTree = !shouldFilterFiber(fiber);
2268+
const newParentInstance = shouldIncludeInTree
2269+
? recordMount(fiber, parentInstance)
2270+
: parentInstance;
2271+
2272+
if (traceUpdatesEnabled) {
2273+
if (traceNearestHostComponentUpdate) {
2274+
const elementType = getElementTypeForFiber(fiber);
2275+
// If an ancestor updated, we should mark the nearest host nodes for highlighting.
2276+
if (elementType === ElementTypeHostComponent) {
2277+
traceUpdatesForNodes.add(fiber.stateNode);
2278+
traceNearestHostComponentUpdate = false;
2279+
}
22712280
}
22722281

2273-
if (fiber.tag === SuspenseComponent) {
2274-
const isTimedOut = fiber.memoizedState !== null;
2275-
if (isTimedOut) {
2276-
// Special case: if Suspense mounts in a timed-out state,
2277-
// get the fallback child from the inner fragment and mount
2278-
// it as if it was our own child. Updates handle this too.
2279-
const primaryChildFragment = fiber.child;
2280-
const fallbackChildFragment = primaryChildFragment
2281-
? primaryChildFragment.sibling
2282-
: null;
2283-
const fallbackChild = fallbackChildFragment
2284-
? fallbackChildFragment.child
2285-
: null;
2286-
if (fallbackChild !== null) {
2287-
mountChildrenRecursively(
2288-
fallbackChild,
2289-
newParentInstance,
2290-
true,
2291-
traceNearestHostComponentUpdate,
2292-
);
2293-
}
2294-
} else {
2295-
let primaryChild: Fiber | null = null;
2296-
const areSuspenseChildrenConditionallyWrapped =
2297-
OffscreenComponent === -1;
2298-
if (areSuspenseChildrenConditionallyWrapped) {
2299-
primaryChild = fiber.child;
2300-
} else if (fiber.child !== null) {
2301-
primaryChild = fiber.child.child;
2302-
}
2303-
if (primaryChild !== null) {
2304-
mountChildrenRecursively(
2305-
primaryChild,
2306-
newParentInstance,
2307-
true,
2308-
traceNearestHostComponentUpdate,
2309-
);
2310-
}
2282+
// We intentionally do not re-enable the traceNearestHostComponentUpdate flag in this branch,
2283+
// because we don't want to highlight every host node inside of a newly mounted subtree.
2284+
}
2285+
2286+
if (fiber.tag === SuspenseComponent) {
2287+
const isTimedOut = fiber.memoizedState !== null;
2288+
if (isTimedOut) {
2289+
// Special case: if Suspense mounts in a timed-out state,
2290+
// get the fallback child from the inner fragment and mount
2291+
// it as if it was our own child. Updates handle this too.
2292+
const primaryChildFragment = fiber.child;
2293+
const fallbackChildFragment = primaryChildFragment
2294+
? primaryChildFragment.sibling
2295+
: null;
2296+
const fallbackChild = fallbackChildFragment
2297+
? fallbackChildFragment.child
2298+
: null;
2299+
if (fallbackChild !== null) {
2300+
mountChildrenRecursively(
2301+
fallbackChild,
2302+
newParentInstance,
2303+
traceNearestHostComponentUpdate,
2304+
);
23112305
}
23122306
} else {
2313-
if (fiber.child !== null) {
2307+
let primaryChild: Fiber | null = null;
2308+
const areSuspenseChildrenConditionallyWrapped =
2309+
OffscreenComponent === -1;
2310+
if (areSuspenseChildrenConditionallyWrapped) {
2311+
primaryChild = fiber.child;
2312+
} else if (fiber.child !== null) {
2313+
primaryChild = fiber.child.child;
2314+
}
2315+
if (primaryChild !== null) {
23142316
mountChildrenRecursively(
2315-
fiber.child,
2317+
primaryChild,
23162318
newParentInstance,
2317-
true,
23182319
traceNearestHostComponentUpdate,
23192320
);
23202321
}
23212322
}
2322-
2323-
// We're exiting this Fiber now, and entering its siblings.
2324-
// If we have selection to restore, we might need to re-activate tracking.
2325-
updateTrackedPathStateAfterMount(mightSiblingsBeOnTrackedPath);
2326-
2327-
fiber = traverseSiblings ? fiber.sibling : null;
2323+
} else {
2324+
if (fiber.child !== null) {
2325+
mountChildrenRecursively(
2326+
fiber.child,
2327+
newParentInstance,
2328+
traceNearestHostComponentUpdate,
2329+
);
2330+
}
23282331
}
2332+
2333+
// We're exiting this Fiber now, and entering its siblings.
2334+
// If we have selection to restore, we might need to re-activate tracking.
2335+
updateTrackedPathStateAfterMount(mightSiblingsBeOnTrackedPath);
23292336
}
23302337

23312338
// We use this to simulate unmounting for Suspense trees
@@ -2533,10 +2540,9 @@ export function attach(
25332540
shouldResetChildren = true;
25342541
}
25352542
} else {
2536-
mountChildrenRecursively(
2543+
mountFiberRecursively(
25372544
nextChild,
25382545
parentInstance,
2539-
false,
25402546
traceNearestHostComponentUpdate,
25412547
);
25422548
shouldResetChildren = true;
@@ -2642,7 +2648,6 @@ export function attach(
26422648
mountChildrenRecursively(
26432649
nextFallbackChildSet,
26442650
newParentInstance,
2645-
true,
26462651
traceNearestHostComponentUpdate,
26472652
);
26482653

@@ -2671,7 +2676,6 @@ export function attach(
26712676
mountChildrenRecursively(
26722677
nextPrimaryChildSet,
26732678
newParentInstance,
2674-
true,
26752679
traceNearestHostComponentUpdate,
26762680
);
26772681
}
@@ -2691,7 +2695,6 @@ export function attach(
26912695
mountChildrenRecursively(
26922696
nextFallbackChildSet,
26932697
newParentInstance,
2694-
true,
26952698
traceNearestHostComponentUpdate,
26962699
);
26972700
shouldResetChildren = true;
@@ -2819,7 +2822,7 @@ export function attach(
28192822
};
28202823
}
28212824

2822-
mountChildrenRecursively(root.current, null, false, false);
2825+
mountFiberRecursively(root.current, null, false);
28232826
flushPendingEvents(root);
28242827
currentRootID = -1;
28252828
});
@@ -2918,7 +2921,7 @@ export function attach(
29182921
if (!wasMounted && isMounted) {
29192922
// Mount a new root.
29202923
setRootPseudoKey(currentRootID, current);
2921-
mountChildrenRecursively(current, null, false, false);
2924+
mountFiberRecursively(current, null, false);
29222925
} else if (wasMounted && isMounted) {
29232926
// Update an existing root.
29242927
updateFiberRecursively(current, alternate, null, false);
@@ -2930,7 +2933,7 @@ export function attach(
29302933
} else {
29312934
// Mount a new root.
29322935
setRootPseudoKey(currentRootID, current);
2933-
mountChildrenRecursively(current, null, false, false);
2936+
mountFiberRecursively(current, null, false);
29342937
}
29352938

29362939
if (isProfiling && isProfilingSupported) {

0 commit comments

Comments
 (0)