Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DevTools] Support secondary environment name when it changes #30842

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {ReactComponentInfo} from 'shared/ReactTypes';
import type {ReactComponentInfo, ReactDebugInfo} from 'shared/ReactTypes';

import {
ComponentFilterDisplayName,
Expand Down Expand Up @@ -2207,6 +2207,7 @@ export function attach(
function recordVirtualMount(
instance: VirtualInstance,
parentInstance: DevToolsInstance | null,
secondaryEnv: null | string,
): void {
const id = instance.id;

Expand All @@ -2220,6 +2221,9 @@ export function attach(
let displayName = componentInfo.name || '';
if (typeof env === 'string') {
// We model environment as an HoC name for now.
if (secondaryEnv !== null) {
displayName = secondaryEnv + '(' + displayName + ')';

This comment was marked as off-topic.

}
displayName = env + '(' + displayName + ')';
}
const elementType = ElementTypeVirtual;
Expand Down Expand Up @@ -2419,6 +2423,25 @@ export function attach(
pendingRealUnmountedIDs.push(id);
}

function getSecondaryEnvironmentName(
debugInfo: ?ReactDebugInfo,
index: number,
): null | string {
if (debugInfo != null) {
const componentInfo: ReactComponentInfo = (debugInfo[index]: any);
for (let i = index + 1; i < debugInfo.length; i++) {
const debugEntry = debugInfo[i];
if (typeof debugEntry.env === 'string') {
// If the next environment is different then this component was the boundary
// and it changed before entering the next component. So we assign this
// component a secondary environment.
return componentInfo.env !== debugEntry.env ? debugEntry.env : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if componentInfo.env is nullish, but debugEntry.env is not? Like when VirtualInstance is a descendant of FiberInstance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no instances here. This is just walking the debugInfo array and comparing it to other entires in that array.

componentInfo.env really can't be nullish right now since there's always an environment for a server component. It's mainly just defensive in case we support other types of virtual components in the future. If it differs, then it's a change in environment so should specify the secondary if available.

However, I don't actually use the secondary environment later on if the first one isn't specified. I assume that then it was just a client component or something that was inlined. But that case doesn't really exist yet.

}
}
}
return null;
}

function mountVirtualChildrenRecursively(
firstChild: Fiber,
lastChild: null | Fiber, // non-inclusive
Expand All @@ -2439,6 +2462,7 @@ export function attach(
// Not a Component. Some other Debug Info.
continue;
}
// Scan up until the next Component to see if this component changed environment.
const componentInfo: ReactComponentInfo = (debugEntry: any);
if (level === virtualLevel) {
if (
Expand All @@ -2458,7 +2482,15 @@ export function attach(
);
}
previousVirtualInstance = createVirtualInstance(componentInfo);
recordVirtualMount(previousVirtualInstance, reconcilingParent);
const secondaryEnv = getSecondaryEnvironmentName(
fiber._debugInfo,
i,
);
recordVirtualMount(
previousVirtualInstance,
reconcilingParent,
secondaryEnv,
);
insertChild(previousVirtualInstance);
previousVirtualInstanceFirstFiber = fiber;
}
Expand Down Expand Up @@ -2914,7 +2946,15 @@ export function attach(
} else {
// Otherwise we create a new instance.
const newVirtualInstance = createVirtualInstance(componentInfo);
recordVirtualMount(newVirtualInstance, reconcilingParent);
const secondaryEnv = getSecondaryEnvironmentName(
nextChild._debugInfo,
i,
);
recordVirtualMount(
newVirtualInstance,
reconcilingParent,
secondaryEnv,
);
insertChild(newVirtualInstance);
previousVirtualInstance = newVirtualInstance;
previousVirtualInstanceWasMount = true;
Expand Down
Loading