11import type { DevToolsDockEntry } from '@vitejs/devtools-kit'
22import type { ClientRpcReturn , DockEntryState , DockEntryStateEvents , DockPanelStorage , DocksContext } from '@vitejs/devtools-kit/client'
3- import type { Ref } from 'vue'
3+ import type { Ref , ShallowRef } from 'vue'
44import { createNanoEvents } from 'nanoevents'
5- import { computed , markRaw , reactive , ref , shallowRef , watch } from 'vue'
5+ import { computed , markRaw , reactive , ref , shallowRef , watch , watchEffect } from 'vue'
66
77export function DEFAULT_DOCK_PANEL_STORE ( ) : DockPanelStorage {
88 return {
@@ -52,28 +52,56 @@ function createDockEntryState(
5252 return state
5353}
5454
55+ let _docksEntriesRef : ShallowRef < DevToolsDockEntry [ ] > | undefined
56+ export async function useDocksEntries ( rpcReturn : ClientRpcReturn ) : Promise < Ref < DevToolsDockEntry [ ] > > {
57+ if ( _docksEntriesRef ) {
58+ return _docksEntriesRef
59+ }
60+ const dockEntries = _docksEntriesRef = shallowRef < DevToolsDockEntry [ ] > ( [ ] )
61+ async function updateDocksEntries ( ) {
62+ dockEntries . value = ( await rpcReturn . rpc . $call ( 'vite:core:list-dock-entries' ) ) . map ( entry => Object . freeze ( entry ) )
63+ // eslint-disable-next-line no-console
64+ console . log ( '[VITE DEVTOOLS] Docks Entries Updated' , [ ...dockEntries . value ] )
65+ }
66+ rpcReturn . clientRpc . register ( {
67+ name : 'vite:core:list-dock-entries:updated' ,
68+ type : 'action' ,
69+ handler : ( ) => updateDocksEntries ( ) ,
70+ } )
71+ await updateDocksEntries ( )
72+ return dockEntries
73+ }
74+
75+ let _docksContext : DocksContext | undefined
5576export async function createDocksContext (
5677 clientType : 'embedded' | 'standalone' ,
5778 rpcReturn : ClientRpcReturn ,
5879 panelStore ?: Ref < DockPanelStorage > ,
5980) : Promise < DocksContext > {
81+ if ( _docksContext ) {
82+ return _docksContext
83+ }
84+
6085 const selected = ref < DevToolsDockEntry | null > ( null )
61- const dockEntries = shallowRef ( ( await rpcReturn . rpc . $call ( 'vite:core:list-dock-entries' ) ) . map ( entry => Object . freeze ( entry ) ) )
62- // eslint-disable-next-line no-console
63- console . log ( '[VITE DEVTOOLS] Docks Entries' , [ ...dockEntries . value ] )
64- // TODO: get board case from rpc when entries updates
86+ const dockEntries = await useDocksEntries ( rpcReturn )
87+
6588 const dockEntryStateMap : Map < string , DockEntryState > = reactive ( new Map ( ) )
66- for ( const entry of dockEntries . value ) {
67- // TODO: handle update
68- dockEntryStateMap . set (
69- entry . id ,
70- createDockEntryState ( entry , selected ) ,
71- )
72- }
89+ watchEffect ( ( ) => {
90+ for ( const entry of dockEntries . value ) {
91+ if ( dockEntryStateMap . has ( entry . id ) ) {
92+ dockEntryStateMap . get ( entry . id ) ! . entryMeta = entry
93+ continue
94+ }
95+ dockEntryStateMap . set (
96+ entry . id ,
97+ createDockEntryState ( entry , selected ) ,
98+ )
99+ }
100+ } )
73101
74102 panelStore ||= ref ( DEFAULT_DOCK_PANEL_STORE ( ) )
75103
76- return reactive ( {
104+ _docksContext = reactive ( {
77105 panel : {
78106 store : panelStore ,
79107 isDragging : false ,
@@ -82,7 +110,7 @@ export async function createDocksContext(
82110 } ,
83111 docks : {
84112 selected,
85- entries : dockEntries . value ,
113+ entries : dockEntries ,
86114 entryToStateMap : markRaw ( dockEntryStateMap ) ,
87115 getStateById : ( id : string ) => dockEntryStateMap . get ( id ) ,
88116 switchEntry : async ( id : string | null ) => {
@@ -102,4 +130,6 @@ export async function createDocksContext(
102130 clientRpc : rpcReturn . clientRpc ,
103131 clientType,
104132 } )
133+
134+ return _docksContext
105135}
0 commit comments