Skip to content

Commit

Permalink
Track root instances in a root Map
Browse files Browse the repository at this point in the history
The FiberRoot is a stateful node that can be tracked this way.

This will let us remove the fiberToFiberInstanceMap.
  • Loading branch information
sebmarkbage committed Sep 4, 2024
1 parent d1afcb4 commit 82a64a6
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ import {getStyleXData} from '../StyleX/utils';
import {createProfilingHooks} from '../profilingHooks';

import type {GetTimelineData, ToggleProfilingStatus} from '../profilingHooks';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {Fiber, FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
import type {
ChangeDescription,
CommitDataBackend,
Expand Down Expand Up @@ -727,6 +727,9 @@ export function getInternalReactConstants(version: string): {
// filters at the same time as removing some other filter.
const knownEnvironmentNames: Set<string> = new Set();

// Map of FiberRoot to their root FiberInstance.
const rootToFiberInstanceMap: Map<FiberRoot, FiberInstance> = new Map();

// Map of one or more Fibers in a pair to their unique id number.
// We track both Fibers to support Fast Refresh,
// which may forcefully replace one of the pair as part of hot reloading.
Expand Down Expand Up @@ -1243,9 +1246,15 @@ export function attach(

// Recursively unmount all roots.
hook.getFiberRoots(rendererID).forEach(root => {
const rootInstance = getFiberInstanceThrows(root.current);
const rootInstance = rootToFiberInstanceMap.get(root);
if (rootInstance === undefined) {
throw new Error(
'Expected the root instance to already exist when applying filters',
);
}
currentRootID = rootInstance.id;
unmountInstanceRecursively(rootInstance);
rootToFiberInstanceMap.delete(root);
flushPendingEvents(root);
currentRootID = -1;
});
Expand All @@ -1260,6 +1269,7 @@ export function attach(
const current = root.current;
const alternate = current.alternate;
const newRoot = createFiberInstance(current);
rootToFiberInstanceMap.set(root, newRoot);
idToDevToolsInstanceMap.set(newRoot.id, newRoot);
fiberToFiberInstanceMap.set(current, newRoot);
if (alternate) {
Expand Down Expand Up @@ -3591,6 +3601,7 @@ export function attach(
const current = root.current;
const alternate = current.alternate;
const newRoot = createFiberInstance(current);
rootToFiberInstanceMap.set(root, newRoot);
idToDevToolsInstanceMap.set(newRoot.id, newRoot);
fiberToFiberInstanceMap.set(current, newRoot);
if (alternate) {
Expand Down Expand Up @@ -3657,15 +3668,17 @@ export function attach(
}
}

function handleCommitFiberRoot(root: any, priorityLevel: void | number) {
function handleCommitFiberRoot(
root: FiberRoot,
priorityLevel: void | number,
) {
const current = root.current;
const alternate = current.alternate;

let rootInstance =
fiberToFiberInstanceMap.get(current) ||
(alternate && fiberToFiberInstanceMap.get(alternate));
let rootInstance = rootToFiberInstanceMap.get(root);
if (!rootInstance) {
rootInstance = createFiberInstance(current);
rootToFiberInstanceMap.set(root, rootInstance);
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
fiberToFiberInstanceMap.set(current, rootInstance);
if (alternate) {
Expand Down Expand Up @@ -3730,8 +3743,9 @@ export function attach(
updateFiberRecursively(rootInstance, current, alternate, false);
} else if (wasMounted && !isMounted) {
// Unmount an existing root.
removeRootPseudoKey(currentRootID);
unmountInstanceRecursively(rootInstance);
removeRootPseudoKey(currentRootID);
rootToFiberInstanceMap.delete(root);
}
} else {
// Mount a new root.
Expand Down Expand Up @@ -5248,7 +5262,12 @@ export function attach(
idToContextsMap = new Map();

hook.getFiberRoots(rendererID).forEach(root => {
const rootInstance = getFiberInstanceThrows(root.current);
const rootInstance = rootToFiberInstanceMap.get(root);
if (rootInstance === undefined) {
throw new Error(
'Expected the root instance to already exist when starting profiling',
);
}
const rootID = rootInstance.id;
((displayNamesByRootID: any): DisplayNamesByRootID).set(
rootID,
Expand Down Expand Up @@ -5645,8 +5664,13 @@ export function attach(
case HostRoot:
// Roots don't have a real displayName, index, or key.
// Instead, we'll use the pseudo key (childDisplayName:indexWithThatName).
const id = getFiberIDThrows(fiber);
const pseudoKey = rootPseudoKeys.get(id);
const rootInstance = rootToFiberInstanceMap.get(fiber.stateNode);
if (rootInstance === undefined) {
throw new Error(
'Expected the root instance to exist when computing a path',
);
}
const pseudoKey = rootPseudoKeys.get(rootInstance.id);
if (pseudoKey === undefined) {
throw new Error('Expected mounted root to have known pseudo key.');
}
Expand Down

0 comments on commit 82a64a6

Please sign in to comment.