@@ -11,6 +11,8 @@ import type {Data} from './index';
11
11
import type { Rect } from '../utils' ;
12
12
import type { NativeType } from '../../types' ;
13
13
14
+ import Agent from 'react-devtools-shared/src/backend/agent' ;
15
+
14
16
const OUTLINE_COLOR = '#f0f0f0' ;
15
17
16
18
// Note these colors are in sync with DevTools Profiler chart colors.
@@ -29,7 +31,17 @@ const COLORS = [
29
31
30
32
let canvas : HTMLCanvasElement | null = null ;
31
33
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
+
33
45
if ( canvas === null ) {
34
46
initialize ( ) ;
35
47
}
@@ -40,17 +52,21 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
40
52
41
53
const context = canvasFlow . getContext ( '2d' ) ;
42
54
context . clearRect ( 0 , 0 , canvasFlow . width , canvasFlow . height ) ;
43
-
44
- nodeToData . forEach ( ( { count, rect} ) => {
55
+ iterateNodes ( nodeToData , ( rect , color ) => {
45
56
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 )
50
58
}
51
59
} ) ;
52
60
}
53
61
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
+
54
70
function drawBorder (
55
71
context : CanvasRenderingContext2D ,
56
72
rect : Rect ,
@@ -79,7 +95,12 @@ function drawBorder(
79
95
context . setLineDash ( [ 0 ] ) ;
80
96
}
81
97
82
- export function destroy ( ) : void {
98
+ export function destroy ( agent : Agent ) : void {
99
+ if ( window . document == null ) {
100
+ agent . emit ( 'disableTraceUpdates' ) ;
101
+ return ;
102
+ }
103
+
83
104
if ( canvas !== null ) {
84
105
if ( canvas . parentNode != null ) {
85
106
canvas . parentNode . removeChild ( canvas ) ;
0 commit comments