1010import type { Data } from './index' ;
1111import type { Rect } from '../utils' ;
1212import type { NativeType } from '../../types' ;
13+ import type Agent from '../../agent' ;
1314
1415const OUTLINE_COLOR = '#f0f0f0' ;
1516
@@ -29,7 +30,17 @@ const COLORS = [
2930
3031let 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,25 @@ 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 , color : String ) = > void ,
64+ node : NativeType ,
65+ ) {
66+ nodeToData . forEach ( ( { count, rect} , node ) => {
67+ const colorIndex = Math . min ( COLORS . length - 1 , count - 1 ) ;
68+ const color = COLORS [ colorIndex ] ;
69+ execute ( rect , color , node ) ;
70+ } ) ;
71+ }
72+
5473function drawBorder (
5574 context : CanvasRenderingContext2D ,
5675 rect : Rect ,
@@ -79,7 +98,12 @@ function drawBorder(
7998 context . setLineDash ( [ 0 ] ) ;
8099}
81100
82- export function destroy ( ) : void {
101+ export function destroy ( agent : Agent ) : void {
102+ if ( window . document == null ) {
103+ agent . emit ( 'disableTraceUpdates' ) ;
104+ return ;
105+ }
106+
83107 if ( canvas !== null ) {
84108 if ( canvas . parentNode != null ) {
85109 canvas . parentNode . removeChild ( canvas ) ;
0 commit comments