mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
## Summary This PR improves the Trace Updates feature by letting developers see component names directly on the update overlay. Before this change, the overlay only highlighted updated regions, leaving it unclear which components were involved. With this update, you can now match visual updates to their corresponding components, making it much easier to debug rendering performance. ### New Feature: Show component names while highlighting When the new **"Show component names while highlighting"** setting is enabled, the update overlay display the names of affected components above the rectangles, along with the update count. This gives immediate context about what’s rendering and why. The preference is stored in local storage and synced with the backend, so it’s remembered across sessions. ### Improvements to Drawing Logic The drawing logic has been updated to make the overlay sharper and easier to read. Overlay now respect device pixel ratios, so they look great on high-DPI screens. Outlines have also been made crisper, which makes it easier to spot exactly where updates are happening. > [!NOTE] > **Grouping Logic and Limitations** > Updates are grouped by their screen position `(left, top coordinates)` to combine overlapping or nearby regions into a single group. Groups are sorted by the highest update count within each group, making the most frequently updated components stand out. > Overlapping labels may still occur when multiple updates involve components that overlap but are not in the exact same position. This is intentional, as the logic aims to maintain a straightforward mapping between update regions and component names without introducing unnecessary complexity. ### Testing This PR also adds tests for the new `groupAndSortNodes` utility, which handles the logic for grouping and sorting updates. The tests ensure the behavior is reliable across different scenarios. ## Before & After https://github.com/user-attachments/assets/6ea0fe3e-9354-44fa-95f3-9a867554f74c https://github.com/user-attachments/assets/32af4d98-92a5-47dd-a732-f05c2293e41b --------- Co-authored-by: Ruslan Lesiutin <rdlesyutin@gmail.com>
176 lines
4.7 KiB
JavaScript
176 lines
4.7 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import Agent from 'react-devtools-shared/src/backend/agent';
|
|
import {destroy as destroyCanvas, draw} from './canvas';
|
|
import {extractHOCNames, getNestedBoundingClientRect} from '../utils';
|
|
|
|
import type {HostInstance} from '../../types';
|
|
import type {Rect} from '../utils';
|
|
|
|
// How long the rect should be shown for?
|
|
const DISPLAY_DURATION = 250;
|
|
|
|
// What's the longest we are willing to show the overlay for?
|
|
// This can be important if we're getting a flurry of events (e.g. scroll update).
|
|
const MAX_DISPLAY_DURATION = 3000;
|
|
|
|
// How long should a rect be considered valid for?
|
|
const REMEASUREMENT_AFTER_DURATION = 250;
|
|
|
|
// Markers for different types of HOCs
|
|
const HOC_MARKERS = new Map([
|
|
['Forget', '✨'],
|
|
['Memo', '🧠'],
|
|
]);
|
|
|
|
// Some environments (e.g. React Native / Hermes) don't support the performance API yet.
|
|
const getCurrentTime =
|
|
// $FlowFixMe[method-unbinding]
|
|
typeof performance === 'object' && typeof performance.now === 'function'
|
|
? () => performance.now()
|
|
: () => Date.now();
|
|
|
|
export type Data = {
|
|
count: number,
|
|
expirationTime: number,
|
|
lastMeasuredAt: number,
|
|
rect: Rect | null,
|
|
displayName: string | null,
|
|
};
|
|
|
|
const nodeToData: Map<HostInstance, Data> = new Map();
|
|
|
|
let agent: Agent = ((null: any): Agent);
|
|
let drawAnimationFrameID: AnimationFrameID | null = null;
|
|
let isEnabled: boolean = false;
|
|
let showNames: boolean = false;
|
|
let redrawTimeoutID: TimeoutID | null = null;
|
|
|
|
export function initialize(injectedAgent: Agent): void {
|
|
agent = injectedAgent;
|
|
agent.addListener('traceUpdates', traceUpdates);
|
|
agent.addListener('showNamesWhenTracing', (shouldShowNames: boolean) => {
|
|
showNames = shouldShowNames;
|
|
if (isEnabled) {
|
|
if (drawAnimationFrameID === null) {
|
|
drawAnimationFrameID = requestAnimationFrame(prepareToDraw);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
export function toggleEnabled(value: boolean): void {
|
|
isEnabled = value;
|
|
|
|
if (!isEnabled) {
|
|
nodeToData.clear();
|
|
|
|
if (drawAnimationFrameID !== null) {
|
|
cancelAnimationFrame(drawAnimationFrameID);
|
|
drawAnimationFrameID = null;
|
|
}
|
|
|
|
if (redrawTimeoutID !== null) {
|
|
clearTimeout(redrawTimeoutID);
|
|
redrawTimeoutID = null;
|
|
}
|
|
|
|
destroyCanvas(agent);
|
|
}
|
|
}
|
|
|
|
function traceUpdates(nodes: Set<HostInstance>): void {
|
|
if (!isEnabled) return;
|
|
|
|
nodes.forEach(node => {
|
|
const data = nodeToData.get(node);
|
|
const now = getCurrentTime();
|
|
|
|
let lastMeasuredAt = data != null ? data.lastMeasuredAt : 0;
|
|
let rect = data != null ? data.rect : null;
|
|
|
|
if (rect === null || lastMeasuredAt + REMEASUREMENT_AFTER_DURATION < now) {
|
|
lastMeasuredAt = now;
|
|
rect = measureNode(node);
|
|
}
|
|
|
|
let displayName = showNames
|
|
? agent.getComponentNameForHostInstance(node)
|
|
: null;
|
|
if (displayName) {
|
|
const {baseComponentName, hocNames} = extractHOCNames(displayName);
|
|
|
|
const markers = hocNames.map(hoc => HOC_MARKERS.get(hoc) || '').join('');
|
|
|
|
const enhancedDisplayName = markers
|
|
? `${markers}${baseComponentName}`
|
|
: baseComponentName;
|
|
|
|
displayName = enhancedDisplayName;
|
|
}
|
|
|
|
nodeToData.set(node, {
|
|
count: data != null ? data.count + 1 : 1,
|
|
expirationTime:
|
|
data != null
|
|
? Math.min(
|
|
now + MAX_DISPLAY_DURATION,
|
|
data.expirationTime + DISPLAY_DURATION,
|
|
)
|
|
: now + DISPLAY_DURATION,
|
|
lastMeasuredAt,
|
|
rect,
|
|
displayName: showNames ? displayName : null,
|
|
});
|
|
});
|
|
|
|
if (redrawTimeoutID !== null) {
|
|
clearTimeout(redrawTimeoutID);
|
|
redrawTimeoutID = null;
|
|
}
|
|
|
|
if (drawAnimationFrameID === null) {
|
|
drawAnimationFrameID = requestAnimationFrame(prepareToDraw);
|
|
}
|
|
}
|
|
|
|
function prepareToDraw(): void {
|
|
drawAnimationFrameID = null;
|
|
redrawTimeoutID = null;
|
|
|
|
const now = getCurrentTime();
|
|
let earliestExpiration = Number.MAX_VALUE;
|
|
|
|
// Remove any items that have already expired.
|
|
nodeToData.forEach((data, node) => {
|
|
if (data.expirationTime < now) {
|
|
nodeToData.delete(node);
|
|
} else {
|
|
earliestExpiration = Math.min(earliestExpiration, data.expirationTime);
|
|
}
|
|
});
|
|
|
|
draw(nodeToData, agent);
|
|
|
|
if (earliestExpiration !== Number.MAX_VALUE) {
|
|
redrawTimeoutID = setTimeout(prepareToDraw, earliestExpiration - now);
|
|
}
|
|
}
|
|
|
|
function measureNode(node: Object): Rect | null {
|
|
if (!node || typeof node.getBoundingClientRect !== 'function') {
|
|
return null;
|
|
}
|
|
|
|
const currentWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
|
|
|
|
return getNestedBoundingClientRect(node, currentWindow);
|
|
}
|