11import type { RpcSharedStateGetOptions , RpcSharedStateHost } from '../types'
2- import type { SharedState } from '../utils/shared-state'
2+ import type { SharedState , SharedStatePatch } from '../utils/shared-state'
33import type { DevToolsRpcClient } from './rpc'
44import { createSharedState } from '../utils/shared-state'
55
66export function createRpcSharedStateClientHost ( rpc : DevToolsRpcClient ) : RpcSharedStateHost {
77 const sharedState = new Map < string , SharedState < any > > ( )
88
9+ console . log ( 'createRpcSharedStateClientHost' , { rpc } )
10+
911 rpc . client . register ( {
1012 name : 'vite:internal:rpc:client-state:updated' ,
1113 type : 'event' ,
1214 handler : async ( key : string , syncId : string ) => {
15+ console . log ( 'vite:internal:rpc:client-state:updated' , { key, syncId } )
1316 const state = sharedState . get ( key )
1417 if ( ! state || state . syncIds . has ( syncId ) )
1518 return
@@ -18,6 +21,16 @@ export function createRpcSharedStateClientHost(rpc: DevToolsRpcClient): RpcShare
1821 } ,
1922 } )
2023
24+ rpc . client . register ( {
25+ name : 'vite:internal:rpc:client-state:patch' ,
26+ type : 'event' ,
27+ handler : async ( key : string , patches : SharedStatePatch [ ] , syncId : string ) => {
28+ const state = sharedState . get ( key )
29+ if ( state )
30+ state . patch ( patches , syncId )
31+ } ,
32+ } )
33+
2134 function registerSharedState < T extends object > ( key : string , state : SharedState < T > ) {
2235 const offs : ( ( ) => void ) [ ] = [ ]
2336 offs . push ( state . on ( 'updated' , ( fullState , patches , syncId ) => {
@@ -28,6 +41,7 @@ export function createRpcSharedStateClientHost(rpc: DevToolsRpcClient): RpcShare
2841 rpc . callEvent ( 'vite:internal:rpc:server-state:set' , key , fullState , syncId )
2942 }
3043 } ) )
44+
3145 return ( ) => {
3246 for ( const off of offs ) {
3347 off ( )
0 commit comments