@@ -631,8 +631,8 @@ export function attach(
631
631
632
632
// Note that by calling these functions we may be creating the ID for the first time.
633
633
// If the Fiber is then never mounted, we are responsible for cleaning up after ourselves.
634
- // This is important because getPrimaryFiber () stores a Fiber in the primaryFibers Set .
635
- // If a Fiber never mounts, and we don't clean up after this code, we could leak.
634
+ // This is important because getOrGenerateFiberID () stores a Fiber in a couple of local Maps .
635
+ // If the Fiber never mounts and we don't clean up after this code, we could leak.
636
636
// Fortunately we would only leak Fibers that have errors/warnings associated with them,
637
637
// which is hopefully only a small set and only in DEV mode– but this is still not great.
638
638
// We should clean up Fibers like this when flushing; see recordPendingErrorsAndWarnings().
@@ -983,6 +983,8 @@ export function attach(
983
983
// When a mount or update is in progress, this value tracks the root that is being operated on.
984
984
let currentRootID: number = -1;
985
985
986
+ // Returns the unique ID for a Fiber or generates and caches a new one if the Fiber hasn't been seen before.
987
+ // Once this method has been called for a Fiber, untrackFiberID() should always be called later to avoid leaking.
986
988
function getOrGenerateFiberID(fiber: Fiber): number {
987
989
let id = null ;
988
990
if ( fiberToIDMap . has ( fiber ) ) {
@@ -1020,6 +1022,7 @@ export function attach(
1020
1022
return refinedID ;
1021
1023
}
1022
1024
1025
+ // Returns an ID if one has already been generated for the Fiber or throws.
1023
1026
function getFiberIDThrows ( fiber : Fiber ) : number {
1024
1027
const maybeID = getFiberIDUnsafe ( fiber ) ;
1025
1028
if ( maybeID !== null ) {
@@ -1030,6 +1033,8 @@ export function attach(
1030
1033
) ;
1031
1034
}
1032
1035
1036
+ // Returns an ID if one has already been generated for the Fiber or null if one has not been generated.
1037
+ // Use this method while e.g. logging to avoid over-retaining Fibers.
1033
1038
function getFiberIDUnsafe(fiber: Fiber): number | null {
1034
1039
if ( fiberToIDMap . has ( fiber ) ) {
1035
1040
return ( ( fiberToIDMap . get ( fiber ) : any ) : number ) ;
@@ -1042,6 +1047,8 @@ export function attach(
1042
1047
return null;
1043
1048
}
1044
1049
1050
+ // Removes a Fiber (and its alternate) from the Maps used to track their id.
1051
+ // This method should always be called when a Fiber is unmounting.
1045
1052
function untrackFiberID ( fiber : Fiber ) {
1046
1053
const fiberID = getFiberIDUnsafe ( fiber ) ;
1047
1054
if ( fiberID !== null ) {
@@ -1696,8 +1703,6 @@ export function attach(
1696
1703
// One example of this is a Lazy component that never resolves before being unmounted.
1697
1704
//
1698
1705
// TODO: This is fragile and can obscure actual bugs.
1699
- //
1700
- // Calling getPrimaryFiber() lazily adds fibers to the Map, so clean up after ourselves before returning.
1701
1706
return ;
1702
1707
}
1703
1708
0 commit comments