Skip to content

Commit 758fc7f

Browse files
authored
Support highlights for React Native apps in dev tools (#26060)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn debug-test --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> This pull request emit the trace update events `drawTraceUpdates` with the trace frame information when the trace update drawer runs outside of web environment. This allows React Devtool running in mobile or other platforms have a chance to render such highlights and provide similar feature on web to provide re-render highlights. This is a feature needed for identifying unnecessary re-renders. ## How did you test this change? <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. --> I tested this change with Flipper desktop app running against mobile app, and verified that the event with correct array of frames are passing through properly.
1 parent 01a0c4e commit 758fc7f

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

packages/react-devtools-core/src/standalone.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ function initialize(socket: WebSocket) {
262262
store = new Store(bridge, {
263263
checkBridgeProtocolCompatibility: true,
264264
supportsNativeInspection: true,
265+
supportsTraceUpdates: true,
265266
});
266267

267268
log('Connected');

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ export default class Agent extends EventEmitter<{
148148
stopInspectingNative: [],
149149
shutdown: [],
150150
traceUpdates: [Set<NativeType>],
151+
drawTraceUpdates: [Array<NativeType>],
152+
disableTraceUpdates: [],
151153
}> {
152154
_bridge: BackendBridge;
153155
_isProfiling: boolean = false;

packages/react-devtools-shared/src/backend/views/TraceUpdates/canvas.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {Data} from './index';
1111
import type {Rect} from '../utils';
1212
import type {NativeType} from '../../types';
13+
import type Agent from '../../agent';
1314

1415
const OUTLINE_COLOR = '#f0f0f0';
1516

@@ -29,7 +30,17 @@ const COLORS = [
2930

3031
let canvas: HTMLCanvasElement | null = null;
3132

32-
export function draw(nodeToData: Map<NativeType, Data>): void {
33+
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
34+
if (window.document == null) {
35+
const nodesToDraw = [];
36+
iterateNodes(nodeToData, (_, color, node) => {
37+
nodesToDraw.push({node, color});
38+
});
39+
40+
agent.emit('drawTraceUpdates', nodesToDraw);
41+
return;
42+
}
43+
3344
if (canvas === null) {
3445
initialize();
3546
}
@@ -40,17 +51,24 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
4051

4152
const context = canvasFlow.getContext('2d');
4253
context.clearRect(0, 0, canvasFlow.width, canvasFlow.height);
43-
44-
nodeToData.forEach(({count, rect}) => {
54+
iterateNodes(nodeToData, (rect, color) => {
4555
if (rect !== null) {
46-
const colorIndex = Math.min(COLORS.length - 1, count - 1);
47-
const color = COLORS[colorIndex];
48-
4956
drawBorder(context, rect, color);
5057
}
5158
});
5259
}
5360

61+
function iterateNodes(
62+
nodeToData: Map<NativeType, Data>,
63+
execute: (rect: Rect | null, color: string, node: NativeType) => void,
64+
) {
65+
nodeToData.forEach(({count, rect}, node) => {
66+
const colorIndex = Math.min(COLORS.length - 1, count - 1);
67+
const color = COLORS[colorIndex];
68+
execute(rect, color, node);
69+
});
70+
}
71+
5472
function drawBorder(
5573
context: CanvasRenderingContext2D,
5674
rect: Rect,
@@ -79,7 +97,12 @@ function drawBorder(
7997
context.setLineDash([0]);
8098
}
8199

82-
export function destroy(): void {
100+
export function destroy(agent: Agent): void {
101+
if (window.document == null) {
102+
agent.emit('disableTraceUpdates');
103+
return;
104+
}
105+
83106
if (canvas !== null) {
84107
if (canvas.parentNode != null) {
85108
canvas.parentNode.removeChild(canvas);

packages/react-devtools-shared/src/backend/views/TraceUpdates/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function toggleEnabled(value: boolean): void {
6666
redrawTimeoutID = null;
6767
}
6868

69-
destroyCanvas();
69+
destroyCanvas(agent);
7070
}
7171
}
7272

@@ -126,7 +126,7 @@ function prepareToDraw(): void {
126126
}
127127
});
128128

129-
draw(nodeToData);
129+
draw(nodeToData, agent);
130130

131131
if (earliestExpiration !== Number.MAX_VALUE) {
132132
redrawTimeoutID = setTimeout(prepareToDraw, earliestExpiration - now);

0 commit comments

Comments
 (0)