1- import { type Debugger , DebuggerModule , DevtoolsMainView , type NodeID } from '@solid-devtools/debugger/types'
21import { SECOND } from '@solid-primitives/date'
3- import { type EventBus , batchEmits , createEventBus , createEventHub } from '@solid-primitives/event-bus'
2+ import { type EventBus , createEventBus , createEventHub } from '@solid-primitives/event-bus'
43import { debounce } from '@solid-primitives/scheduled'
54import { defer } from '@solid-primitives/utils'
5+ import { type Debugger , DebuggerModule , DevtoolsMainView , type NodeID } from '@solid-devtools/debugger/types'
6+ import { mutate_remove } from '@solid-devtools/shared/utils'
67import * as s from 'solid-js'
78import { App } from './App.tsx'
89import createInspector from './inspector.tsx'
@@ -14,7 +15,16 @@ type ToEventBusChannels<T extends Record<string, any>> = {
1415 [ K in keyof T ] : EventBus < T [ K ] >
1516}
1617
18+ export type InputMessage = {
19+ [ K in keyof Debugger . OutputChannels ] : {
20+ name : K ,
21+ details : Debugger . OutputChannels [ K ] ,
22+ }
23+ } [ keyof Debugger . OutputChannels ]
24+ export type InputListener = ( e : InputMessage ) => void
25+
1726function createDebuggerBridge ( ) {
27+
1828 const output = createEventHub < ToEventBusChannels < Debugger . InputChannels > > ( $ => ( {
1929 ResetState : $ ( ) ,
2030 InspectNode : $ ( ) ,
@@ -26,21 +36,22 @@ function createDebuggerBridge() {
2636 ToggleModule : $ ( ) ,
2737 } ) )
2838
29- // Listener of the client events (from the debugger) will be called synchronously under `batch`
30- // to make sure that the state is updated before the effect queue is flushed.
31- const input = createEventHub < ToEventBusChannels < Debugger . OutputChannels > > ( $ => ( {
32- DebuggerEnabled : batchEmits ( $ ( ) ) ,
33- ResetPanel : batchEmits ( $ ( ) ) ,
34- InspectedState : batchEmits ( $ ( ) ) ,
35- InspectedNodeDetails : batchEmits ( $ ( ) ) ,
36- StructureUpdates : batchEmits ( $ ( ) ) ,
37- NodeUpdates : batchEmits ( $ ( ) ) ,
38- InspectorUpdate : batchEmits ( $ ( ) ) ,
39- LocatorModeChange : batchEmits ( $ ( ) ) ,
40- HoveredComponent : batchEmits ( $ ( ) ) ,
41- InspectedComponent : batchEmits ( $ ( ) ) ,
42- DgraphUpdate : batchEmits ( $ ( ) ) ,
43- } ) )
39+ let input_listeners : InputListener [ ] = [ ]
40+ const input = {
41+ listen ( listener : InputListener ) {
42+ input_listeners . push ( listener )
43+ s . onCleanup ( ( ) => {
44+ mutate_remove ( input_listeners , listener )
45+ } )
46+ } ,
47+ emit ( e : InputMessage ) {
48+ s . batch ( ( ) => {
49+ for ( let fn of input_listeners ) {
50+ fn ( e )
51+ }
52+ } )
53+ } ,
54+ }
4455
4556 return { input, output}
4657}
@@ -152,11 +163,9 @@ function createController(bridge: DebuggerBridge, options: DevtoolsOptions) {
152163 const locatorEnabled = ( ) => devtoolsLocatorEnabled ( ) || clientLocatorEnabled ( )
153164
154165 // send devtools locator state
155- s . createEffect (
156- defer ( devtoolsLocatorEnabled , enabled =>
157- bridge . output . ToggleModule . emit ( { module : DebuggerModule . Locator , enabled} ) ,
158- ) ,
159- )
166+ s . createEffect ( defer ( devtoolsLocatorEnabled , enabled =>
167+ bridge . output . ToggleModule . emit ( { module : DebuggerModule . Locator , enabled} ) ,
168+ ) )
160169
161170 function setClientLocatorState ( enabled : boolean ) {
162171 s . batch ( ( ) => {
@@ -210,7 +219,6 @@ function createController(bridge: DebuggerBridge, options: DevtoolsOptions) {
210219 // Node updates - signals and computations updating
211220 //
212221 const nodeUpdates = createEventBus < NodeID > ( )
213- bridge . input . NodeUpdates . listen ( updated => updated . forEach ( id => nodeUpdates . emit ( id ) ) )
214222
215223 //
216224 // INSPECTOR
@@ -220,23 +228,43 @@ function createController(bridge: DebuggerBridge, options: DevtoolsOptions) {
220228 //
221229 // Client events
222230 //
223- bridge . input . ResetPanel . listen ( ( ) => {
224- setClientLocatorState ( false )
225- setDevtoolsLocatorState ( false )
226- inspector . setInspectedOwner ( null )
227- } )
228-
229- bridge . input . HoveredComponent . listen ( ( { nodeId, state} ) => {
230- setClientHoveredNode ( p => ( state ? nodeId : p && p === nodeId ? null : p ) )
231+ bridge . input . listen ( e => {
232+ switch ( e . name ) {
233+ case 'NodeUpdates' :
234+ for ( let id of e . details ) {
235+ nodeUpdates . emit ( id )
236+ }
237+ break
238+ case 'ResetPanel' :
239+ setClientLocatorState ( false )
240+ setDevtoolsLocatorState ( false )
241+ inspector . setInspectedOwner ( null )
242+ break
243+ case 'HoveredComponent' :
244+ setClientHoveredNode ( p => {
245+ return e . details . state
246+ ? e . details . nodeId
247+ : p && p === e . details . nodeId ? null : p
248+ } )
249+ break
250+ case 'InspectedComponent' :
251+ inspector . setInspectedOwner ( e . details )
252+ setDevtoolsLocatorState ( false )
253+ break
254+ case 'LocatorModeChange' :
255+ setClientLocatorState ( e . details )
256+ break
257+ case 'DebuggerEnabled' :
258+ case 'InspectedState' :
259+ case 'InspectedNodeDetails' :
260+ case 'StructureUpdates' :
261+ case 'InspectorUpdate' :
262+ case 'DgraphUpdate' :
263+ // handled elsewhere for now
264+ break
265+ }
231266 } )
232267
233- bridge . input . InspectedComponent . listen ( node => {
234- inspector . setInspectedOwner ( node )
235- setDevtoolsLocatorState ( false )
236- } )
237-
238- bridge . input . LocatorModeChange . listen ( setClientLocatorState )
239-
240268 return {
241269 locator : {
242270 locatorEnabled,
0 commit comments