@@ -11,6 +11,8 @@ import type {Data} from './index';
1111import type { Rect } from '../utils' ;
1212import type { NativeType } from '../../types' ;
1313
14+ import Agent from 'react-devtools-shared/src/backend/agent' ;
15+
1416const OUTLINE_COLOR = '#f0f0f0' ;
1517
1618// Note these colors are in sync with DevTools Profiler chart colors.
@@ -29,7 +31,17 @@ const COLORS = [
2931
3032let canvas : HTMLCanvasElement | null = null ;
3133
32- export function draw ( nodeToData : Map < NativeType , Data > ) : void {
34+ export function draw ( nodeToData : Map < NativeType , Data > , agent : Agent) : void {
35+ if ( window . document == null ) {
36+ const nodesToDraw = [ ] ;
37+ iterateNodes ( nodeToData , ( _ , color , node ) => {
38+ nodesToDraw . push ( { node, color } ) ;
39+ } ) ;
40+
41+ agent . emit ( 'drawTraceUpdates' , nodesToDraw ) ;
42+ return ;
43+ }
44+
3345 if ( canvas === null ) {
3446 initialize ( ) ;
3547 }
@@ -40,17 +52,21 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
4052
4153 const context = canvasFlow . getContext ( '2d' ) ;
4254 context . clearRect ( 0 , 0 , canvasFlow . width , canvasFlow . height ) ;
43-
44- nodeToData . forEach ( ( { count, rect} ) => {
55+ iterateNodes ( nodeToData , ( rect , color ) => {
4556 if ( rect !== null ) {
46- const colorIndex = Math . min ( COLORS . length - 1 , count - 1 ) ;
47- const color = COLORS [ colorIndex ] ;
48-
49- drawBorder ( context , rect , color ) ;
57+ drawBorder ( context , rect , color )
5058 }
5159 } ) ;
5260}
5361
62+ function iterateNodes ( nodeToData : Map < NativeType , Data > , execute : ( rect : Rect , color : String ) = > void , node : NativeType ) {
63+ nodeToData . forEach ( ( { count, rect} , node ) => {
64+ const colorIndex = Math . min ( COLORS . length - 1 , count - 1 ) ;
65+ const color = COLORS [ colorIndex ] ;
66+ execute ( rect , color , node ) ;
67+ } ) ;
68+ }
69+
5470function drawBorder (
5571 context : CanvasRenderingContext2D ,
5672 rect : Rect ,
@@ -79,7 +95,11 @@ function drawBorder(
7995 context . setLineDash ( [ 0 ] ) ;
8096}
8197
82- export function destroy ( ) : void {
98+ export function destroy ( agent : Agent ) : void {
99+ if ( window . document == null ) {
100+ return ;
101+ }
102+
83103 if ( canvas !== null ) {
84104 if ( canvas . parentNode != null ) {
85105 canvas . parentNode . removeChild ( canvas ) ;
0 commit comments