1- import { hovered , root , selected } from './store' ;
1+ import { type DebugNode , hovered , root , selected } from './store' ;
22
33const tabId = chrome . devtools . inspectedWindow . tabId ;
44const port = chrome . runtime . connect ( { name : `${ tabId } ` } ) ;
@@ -11,21 +11,7 @@ export const background = {
1111 } ,
1212} ;
1313
14- const nodes = new Map ( ) ;
15-
16- function insertNode ( node : any , target : any , anchorId : number ) {
17- node . parent = target ;
18-
19- const index = anchorId ? target . children . findIndex ( ( o : any ) => o . id === anchorId ) : - 1 ;
20-
21- if ( index !== - 1 ) {
22- target . children . splice ( index , 0 , node ) ;
23- } else {
24- target . children . push ( node ) ;
25- }
26-
27- target . invalidate ( ) ;
28- }
14+ const nodes = new Map < null | number , DebugNode > ( ) ;
2915
3016function resolveEventBubble ( node : any ) {
3117 if ( ! node . detail || ! node . detail . listeners ) return ;
@@ -65,47 +51,43 @@ port.onMessage.addListener(({ type, payload }) => {
6551 }
6652
6753 case 'courier/node:add' : {
68- const { node, target, anchor } = payload ;
54+ const { node, target, anchor } = payload as {
55+ node : DebugNode ;
56+ target : null | number ;
57+ anchor : null | number ;
58+ } ;
6959
7060 node . children = [ ] ;
7161 node . expanded = false ;
7262 node . invalidate = ( ) => { } ;
7363 resolveEventBubble ( node ) ;
7464
75- const map_node = nodes . get ( target ) ;
65+ const parent = nodes . get ( target ) ;
7666 nodes . set ( node . id , node ) ;
67+ if ( ! parent ) return root . update ( ( n ) => [ ...n , node ] ) ;
7768
78- if ( map_node ) {
79- insertNode ( node , map_node , anchor ) ;
80- return ;
81- }
82-
83- if ( node . _timeout ) return ;
84-
85- node . _timeout = setTimeout ( ( ) => {
86- delete node . _timeout ;
87- const targetNode = nodes . get ( target ) ;
88- if ( targetNode ) insertNode ( node , targetNode , anchor ) ;
89- else root . update ( ( o ) => [ ...o , node ] ) ;
90- } , 100 ) ;
69+ const index = parent . children . findIndex ( ( n ) => n . id === anchor ) ;
70+ if ( index === - 1 ) parent . children . push ( node ) ;
71+ else parent . children . splice ( index , 0 , node ) ;
9172
92- break ;
73+ return ( node . parent = parent ) . invalidate ( ) ;
9374 }
9475
9576 case 'courier/node:remove' : {
96- const current = nodes . get ( payload . node . id ) ;
97- nodes . delete ( current . id ) ;
98- if ( ! current . parent ) break ;
77+ const node = payload . node as SvelteBlockDetail ;
78+ const current = nodes . get ( node . id ) ;
79+ if ( current ) nodes . delete ( current . id ) ;
80+ if ( ! current ?. parent ) return ;
9981
100- const index = current . parent . children . findIndex ( ( o : any ) => o . id === current . id ) ;
82+ const index = current . parent . children . findIndex ( ( o ) => o . id === current . id ) ;
10183 current . parent . children . splice ( index , 1 ) ;
102- current . parent . invalidate ( ) ;
103- break ;
84+ return current . parent . invalidate ( ) ;
10485 }
10586
10687 case 'courier/node:update' : {
107- const current = nodes . get ( payload . node . id ) ;
108- if ( ! current ) return ; // TODO: investigate why this happens
88+ const node = payload . node as SvelteBlockDetail ;
89+ const current = nodes . get ( node . id ) ;
90+ if ( ! current ) return ;
10991 Object . assign ( current , payload . node ) ;
11092 resolveEventBubble ( current ) ;
11193
0 commit comments