@samitouri / QOS-React / commits / 758fc7fde1

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.

Xin Chen committed Feb 7, 2023 at 14:47 UTC 758fc7fde10f49912b18496299506cba30d6029b
4 files changed +35 -9
packages/react-devtools-core/src/standalone.js
+1
@@ -262,6 +262,7 @@ function initialize(socket: WebSocket) {
262 store = new Store(bridge, {
263 checkBridgeProtocolCompatibility: true,
264 supportsNativeInspection: true,
265 + supportsTraceUpdates: true,
266 });
267
268 log('Connected');
packages/react-devtools-shared/src/backend/agent.js
+2
@@ -148,6 +148,8 @@ export default class Agent extends EventEmitter<{
148 stopInspectingNative: [],
149 shutdown: [],
150 traceUpdates: [Set<NativeType>],
151 + drawTraceUpdates: [Array<NativeType>],
152 + disableTraceUpdates: [],
153 }> {
154 _bridge: BackendBridge;
155 _isProfiling: boolean = false;
packages/react-devtools-shared/src/backend/views/TraceUpdates/canvas.js
+30 -7
@@ -10,6 +10,7 @@
10 import type {Data} from './index';
11 import type {Rect} from '../utils';
12 import type {NativeType} from '../../types';
13 +import type Agent from '../../agent';
14
15 const OUTLINE_COLOR = '#f0f0f0';
16
@@ -29,7 +30,17 @@ const COLORS = [
30
31 let canvas: HTMLCanvasElement | null = null;
32
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 +
44 if (canvas === null) {
45 initialize();
46 }
@@ -40,17 +51,24 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
51
52 const context = canvasFlow.getContext('2d');
53 context.clearRect(0, 0, canvasFlow.width, canvasFlow.height);
43 -
44 - nodeToData.forEach(({count, rect}) => {
54 + iterateNodes(nodeToData, (rect, color) => {
55 if (rect !== null) {
46 - const colorIndex = Math.min(COLORS.length - 1, count - 1);
47 - const color = COLORS[colorIndex];
48 -
56 drawBorder(context, rect, color);
57 }
58 });
59 }
60
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 +
72 function drawBorder(
73 context: CanvasRenderingContext2D,
74 rect: Rect,
@@ -79,7 +97,12 @@ function drawBorder(
97 context.setLineDash([0]);
98 }
99
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 +
106 if (canvas !== null) {
107 if (canvas.parentNode != null) {
108 canvas.parentNode.removeChild(canvas);
packages/react-devtools-shared/src/backend/views/TraceUpdates/index.js
+2 -2
@@ -66,7 +66,7 @@ export function toggleEnabled(value: boolean): void {
66 redrawTimeoutID = null;
67 }
68
69 - destroyCanvas();
69 + destroyCanvas(agent);
70 }
71 }
72
@@ -126,7 +126,7 @@ function prepareToDraw(): void {
126 }
127 });
128
129 - draw(nodeToData);
129 + draw(nodeToData, agent);
130
131 if (earliestExpiration !== Number.MAX_VALUE) {
132 redrawTimeoutID = setTimeout(prepareToDraw, earliestExpiration - now);