@@ -114,7 +114,7 @@ import {getStyleXData} from '../StyleX/utils';
114114import { createProfilingHooks } from '../profilingHooks' ;
115115
116116import type { GetTimelineData , ToggleProfilingStatus } from '../profilingHooks' ;
117- import type { Fiber } from 'react-reconciler/src/ReactInternalTypes' ;
117+ import type { Fiber , FiberRoot } from 'react-reconciler/src/ReactInternalTypes' ;
118118import type {
119119 ChangeDescription ,
120120 CommitDataBackend ,
@@ -727,6 +727,9 @@ export function getInternalReactConstants(version: string): {
727727// filters at the same time as removing some other filter.
728728const knownEnvironmentNames : Set < string > = new Set ( ) ;
729729
730+ // Map of FiberRoot to their root FiberInstance.
731+ const rootToFiberInstanceMap : Map < FiberRoot , FiberInstance > = new Map ( ) ;
732+
730733// Map of one or more Fibers in a pair to their unique id number.
731734// We track both Fibers to support Fast Refresh,
732735// which may forcefully replace one of the pair as part of hot reloading.
@@ -1243,9 +1246,15 @@ export function attach(
12431246
12441247 // Recursively unmount all roots.
12451248 hook . getFiberRoots ( rendererID ) . forEach ( root => {
1246- const rootInstance = getFiberInstanceThrows ( root . current ) ;
1249+ const rootInstance = rootToFiberInstanceMap . get ( root ) ;
1250+ if ( rootInstance === undefined ) {
1251+ throw new Error (
1252+ 'Expected the root instance to already exist when applying filters' ,
1253+ ) ;
1254+ }
12471255 currentRootID = rootInstance . id ;
12481256 unmountInstanceRecursively ( rootInstance ) ;
1257+ rootToFiberInstanceMap . delete ( root ) ;
12491258 flushPendingEvents ( root ) ;
12501259 currentRootID = - 1 ;
12511260 } ) ;
@@ -1260,6 +1269,7 @@ export function attach(
12601269 const current = root . current ;
12611270 const alternate = current . alternate ;
12621271 const newRoot = createFiberInstance ( current ) ;
1272+ rootToFiberInstanceMap . set ( root , newRoot ) ;
12631273 idToDevToolsInstanceMap . set ( newRoot . id , newRoot ) ;
12641274 fiberToFiberInstanceMap . set ( current , newRoot ) ;
12651275 if ( alternate ) {
@@ -2181,7 +2191,7 @@ export function attach(
21812191 const isRoot = fiber . tag === HostRoot ;
21822192 let fiberInstance ;
21832193 if ( isRoot ) {
2184- const entry = fiberToFiberInstanceMap . get ( fiber ) ;
2194+ const entry = rootToFiberInstanceMap . get ( fiber . stateNode ) ;
21852195 if ( entry === undefined ) {
21862196 throw new Error ( 'The root should have been registered at this point' ) ;
21872197 }
@@ -3591,6 +3601,7 @@ export function attach(
35913601 const current = root . current ;
35923602 const alternate = current . alternate ;
35933603 const newRoot = createFiberInstance ( current ) ;
3604+ rootToFiberInstanceMap . set ( root , newRoot ) ;
35943605 idToDevToolsInstanceMap . set ( newRoot . id , newRoot ) ;
35953606 fiberToFiberInstanceMap . set ( current , newRoot ) ;
35963607 if ( alternate ) {
@@ -3657,15 +3668,17 @@ export function attach(
36573668 }
36583669 }
36593670
3660- function handleCommitFiberRoot ( root : any , priorityLevel : void | number ) {
3671+ function handleCommitFiberRoot (
3672+ root : FiberRoot ,
3673+ priorityLevel : void | number ,
3674+ ) {
36613675 const current = root . current ;
36623676 const alternate = current . alternate ;
36633677
3664- let rootInstance =
3665- fiberToFiberInstanceMap . get ( current ) ||
3666- ( alternate && fiberToFiberInstanceMap . get ( alternate ) ) ;
3678+ let rootInstance = rootToFiberInstanceMap . get ( root ) ;
36673679 if ( ! rootInstance ) {
36683680 rootInstance = createFiberInstance ( current ) ;
3681+ rootToFiberInstanceMap . set ( root , rootInstance ) ;
36693682 idToDevToolsInstanceMap . set ( rootInstance . id , rootInstance ) ;
36703683 fiberToFiberInstanceMap . set ( current , rootInstance ) ;
36713684 if ( alternate ) {
@@ -3730,8 +3743,9 @@ export function attach(
37303743 updateFiberRecursively ( rootInstance , current , alternate , false ) ;
37313744 } else if ( wasMounted && ! isMounted ) {
37323745 // Unmount an existing root.
3733- removeRootPseudoKey ( currentRootID ) ;
37343746 unmountInstanceRecursively ( rootInstance ) ;
3747+ removeRootPseudoKey ( currentRootID ) ;
3748+ rootToFiberInstanceMap . delete ( root ) ;
37353749 }
37363750 } else {
37373751 // Mount a new root.
@@ -5248,7 +5262,12 @@ export function attach(
52485262 idToContextsMap = new Map();
52495263
52505264 hook.getFiberRoots(rendererID).forEach(root => {
5251- const rootInstance = getFiberInstanceThrows ( root . current ) ;
5265+ const rootInstance = rootToFiberInstanceMap . get ( root ) ;
5266+ if ( rootInstance === undefined ) {
5267+ throw new Error (
5268+ 'Expected the root instance to already exist when starting profiling' ,
5269+ ) ;
5270+ }
52525271 const rootID = rootInstance . id ;
52535272 ( ( displayNamesByRootID : any ) : DisplayNamesByRootID ) . set (
52545273 rootID ,
@@ -5645,8 +5664,13 @@ export function attach(
56455664 case HostRoot :
56465665 // Roots don't have a real displayName, index, or key.
56475666 // Instead, we'll use the pseudo key (childDisplayName:indexWithThatName).
5648- const id = getFiberIDThrows ( fiber ) ;
5649- const pseudoKey = rootPseudoKeys . get ( id ) ;
5667+ const rootInstance = rootToFiberInstanceMap . get ( fiber . stateNode ) ;
5668+ if ( rootInstance === undefined ) {
5669+ throw new Error (
5670+ 'Expected the root instance to exist when computing a path' ,
5671+ ) ;
5672+ }
5673+ const pseudoKey = rootPseudoKeys . get ( rootInstance . id ) ;
56505674 if ( pseudoKey === undefined ) {
56515675 throw new Error ( 'Expected mounted root to have known pseudo key.' ) ;
56525676 }
0 commit comments