@@ -114,7 +114,7 @@ import {getStyleXData} from '../StyleX/utils';
114
114
import { createProfilingHooks } from '../profilingHooks' ;
115
115
116
116
import type { GetTimelineData , ToggleProfilingStatus } from '../profilingHooks' ;
117
- import type { Fiber } from 'react-reconciler/src/ReactInternalTypes' ;
117
+ import type { Fiber , FiberRoot } from 'react-reconciler/src/ReactInternalTypes' ;
118
118
import type {
119
119
ChangeDescription ,
120
120
CommitDataBackend ,
@@ -727,6 +727,9 @@ export function getInternalReactConstants(version: string): {
727
727
// filters at the same time as removing some other filter.
728
728
const knownEnvironmentNames : Set < string > = new Set ( ) ;
729
729
730
+ // Map of FiberRoot to their root FiberInstance.
731
+ const rootToFiberInstanceMap : Map < FiberRoot , FiberInstance > = new Map ( ) ;
732
+
730
733
// Map of one or more Fibers in a pair to their unique id number.
731
734
// We track both Fibers to support Fast Refresh,
732
735
// which may forcefully replace one of the pair as part of hot reloading.
@@ -1243,9 +1246,15 @@ export function attach(
1243
1246
1244
1247
// Recursively unmount all roots.
1245
1248
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
+ }
1247
1255
currentRootID = rootInstance . id ;
1248
1256
unmountInstanceRecursively ( rootInstance ) ;
1257
+ rootToFiberInstanceMap . delete ( root ) ;
1249
1258
flushPendingEvents ( root ) ;
1250
1259
currentRootID = - 1 ;
1251
1260
} ) ;
@@ -1260,6 +1269,7 @@ export function attach(
1260
1269
const current = root . current ;
1261
1270
const alternate = current . alternate ;
1262
1271
const newRoot = createFiberInstance ( current ) ;
1272
+ rootToFiberInstanceMap . set ( root , newRoot ) ;
1263
1273
idToDevToolsInstanceMap . set ( newRoot . id , newRoot ) ;
1264
1274
fiberToFiberInstanceMap . set ( current , newRoot ) ;
1265
1275
if ( alternate ) {
@@ -3591,6 +3601,7 @@ export function attach(
3591
3601
const current = root . current ;
3592
3602
const alternate = current . alternate ;
3593
3603
const newRoot = createFiberInstance ( current ) ;
3604
+ rootToFiberInstanceMap . set ( root , newRoot ) ;
3594
3605
idToDevToolsInstanceMap . set ( newRoot . id , newRoot ) ;
3595
3606
fiberToFiberInstanceMap . set ( current , newRoot ) ;
3596
3607
if ( alternate ) {
@@ -3657,15 +3668,17 @@ export function attach(
3657
3668
}
3658
3669
}
3659
3670
3660
- function handleCommitFiberRoot ( root : any , priorityLevel : void | number ) {
3671
+ function handleCommitFiberRoot (
3672
+ root : FiberRoot ,
3673
+ priorityLevel : void | number ,
3674
+ ) {
3661
3675
const current = root . current ;
3662
3676
const alternate = current . alternate ;
3663
3677
3664
- let rootInstance =
3665
- fiberToFiberInstanceMap . get ( current ) ||
3666
- ( alternate && fiberToFiberInstanceMap . get ( alternate ) ) ;
3678
+ let rootInstance = rootToFiberInstanceMap . get ( root ) ;
3667
3679
if ( ! rootInstance ) {
3668
3680
rootInstance = createFiberInstance ( current ) ;
3681
+ rootToFiberInstanceMap . set ( root , rootInstance ) ;
3669
3682
idToDevToolsInstanceMap . set ( rootInstance . id , rootInstance ) ;
3670
3683
fiberToFiberInstanceMap . set ( current , rootInstance ) ;
3671
3684
if ( alternate ) {
@@ -3730,8 +3743,9 @@ export function attach(
3730
3743
updateFiberRecursively ( rootInstance , current , alternate , false ) ;
3731
3744
} else if ( wasMounted && ! isMounted ) {
3732
3745
// Unmount an existing root.
3733
- removeRootPseudoKey ( currentRootID ) ;
3734
3746
unmountInstanceRecursively ( rootInstance ) ;
3747
+ removeRootPseudoKey ( currentRootID ) ;
3748
+ rootToFiberInstanceMap . delete ( root ) ;
3735
3749
}
3736
3750
} else {
3737
3751
// Mount a new root.
@@ -5248,7 +5262,12 @@ export function attach(
5248
5262
idToContextsMap = new Map();
5249
5263
5250
5264
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
+ }
5252
5271
const rootID = rootInstance . id ;
5253
5272
( ( displayNamesByRootID : any ) : DisplayNamesByRootID ) . set (
5254
5273
rootID ,
@@ -5645,8 +5664,13 @@ export function attach(
5645
5664
case HostRoot :
5646
5665
// Roots don't have a real displayName, index, or key.
5647
5666
// 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 ) ;
5650
5674
if ( pseudoKey === undefined ) {
5651
5675
throw new Error ( 'Expected mounted root to have known pseudo key.' ) ;
5652
5676
}
0 commit comments