@@ -631,8 +631,8 @@ export function attach(
631631
632632 // Note that by calling these functions we may be creating the ID for the first time.
633633 // 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.
636636 // Fortunately we would only leak Fibers that have errors/warnings associated with them,
637637 // which is hopefully only a small set and only in DEV mode– but this is still not great.
638638 // We should clean up Fibers like this when flushing; see recordPendingErrorsAndWarnings().
@@ -983,6 +983,8 @@ export function attach(
983983 // When a mount or update is in progress, this value tracks the root that is being operated on.
984984 let currentRootID: number = -1;
985985
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.
986988 function getOrGenerateFiberID(fiber: Fiber): number {
987989 let id = null ;
988990 if ( fiberToIDMap . has ( fiber ) ) {
@@ -1020,6 +1022,7 @@ export function attach(
10201022 return refinedID ;
10211023 }
10221024
1025+ // Returns an ID if one has already been generated for the Fiber or throws.
10231026 function getFiberIDThrows ( fiber : Fiber ) : number {
10241027 const maybeID = getFiberIDUnsafe ( fiber ) ;
10251028 if ( maybeID !== null ) {
@@ -1030,6 +1033,8 @@ export function attach(
10301033 ) ;
10311034 }
10321035
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.
10331038 function getFiberIDUnsafe(fiber: Fiber): number | null {
10341039 if ( fiberToIDMap . has ( fiber ) ) {
10351040 return ( ( fiberToIDMap . get ( fiber ) : any ) : number ) ;
@@ -1042,6 +1047,8 @@ export function attach(
10421047 return null;
10431048 }
10441049
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.
10451052 function untrackFiberID ( fiber : Fiber ) {
10461053 const fiberID = getFiberIDUnsafe ( fiber ) ;
10471054 if ( fiberID !== null ) {
@@ -1696,8 +1703,6 @@ export function attach(
16961703 // One example of this is a Lazy component that never resolves before being unmounted.
16971704 //
16981705 // 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.
17011706 return ;
17021707 }
17031708
0 commit comments