Skip to content

Commit f26cea3

Browse files
committed
wip:
1 parent f169934 commit f26cea3

File tree

9 files changed

+391
-21
lines changed

9 files changed

+391
-21
lines changed

packages/core/playground/src/pages/devtools.vue

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@ import { getDevToolsRpcClient } from '@vitejs/devtools-kit/client'
33
import { onMounted, shallowRef } from 'vue'
44
55
const stateRef = shallowRef<any>(undefined)
6+
const isTrustedRef = shallowRef<boolean | null>(null)
67
78
onMounted(async () => {
89
const client = await getDevToolsRpcClient()
10+
isTrustedRef.value = client.isTrusted
911
const state = await client.sharedState.get('counter')
1012
11-
stateRef.value = state.get()
12-
state.on('updated', () => {
13-
stateRef.value = state.get()
13+
client.events.on('rpc:is-trusted:updated', (isTrusted) => {
14+
isTrustedRef.value = isTrusted
15+
})
16+
17+
stateRef.value = state.value()
18+
state.on('updated', (newState) => {
19+
console.log('updated', newState)
20+
stateRef.value = newState
1421
})
1522
})
1623
</script>
1724

1825
<template>
1926
<div>
20-
<h1>DevTools {{ stateRef }}</h1>
27+
<h1>DevTools </h1>
28+
<div>{{ isTrustedRef }}</div>
29+
<pre>{{ stateRef }}</pre>
2130
</div>
2231
</template>

packages/core/playground/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export default defineConfig({
142142
counterState.mutate((current) => {
143143
current.count = (current.count + 1) % 5
144144
})
145-
const count = counterState.get().count
145+
const count = counterState.value().count
146146
ctx.docks.update({
147147
id: 'counter',
148148
type: 'action',

packages/core/src/node/rpc-shared-state.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ export function createRpcSharedStateServerHost(
1010
function registerSharedState<T extends object>(key: string, state: SharedState<T>) {
1111
const offs: (() => void)[] = []
1212

13-
offs.push(state.on('updated', (_fullState, patches, syncId) => {
14-
if (patches) {
15-
rpc.broadcast('vite:internal:rpc:client-state:patch', key, patches, syncId)
16-
}
17-
else {
18-
rpc.broadcast('vite:internal:rpc:client-state:updated', key, syncId)
19-
}
20-
}))
13+
offs.push(
14+
state.on('updated', (_fullState, patches, syncId) => {
15+
if (patches) {
16+
rpc.broadcast('vite:internal:rpc:client-state:patch', key, patches, syncId)
17+
}
18+
else {
19+
rpc.broadcast('vite:internal:rpc:client-state:updated', key, syncId)
20+
}
21+
}),
22+
)
2123

2224
return () => {
2325
for (const off of offs) {

packages/core/src/node/rpc/anonymous/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const anonymousAuth = defineRpcFunction({
2525
if (!session)
2626
throw new Error('Failed to retrieve the current RPC session')
2727

28-
if (session.meta.isTrusted || storage.get().trusted[query.authId]) {
28+
if (session.meta.isTrusted || storage.value().trusted[query.authId]) {
2929
session.meta.clientAuthId = query.authId
3030
session.meta.isTrusted = true
3131
return {

packages/core/src/node/rpc/internal/state/get.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const sharedStateGet = defineRpcFunction({
77
setup: (context: DevToolsNodeContext) => {
88
return {
99
handler: async (key: string) => {
10-
return await context.rpc.sharedState.get(key)
10+
const state = await context.rpc.sharedState.get(key)
11+
return state.value()
1112
},
1213
}
1314
},

packages/core/src/node/ws.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ export async function createWsServer(options: CreateWsServerOptions) {
4545
if (isClientAuthDisabled) {
4646
meta.isTrusted = true
4747
}
48-
else if (authId && contextInternal.storage.auth.get().trusted[authId]) {
48+
else if (authId && contextInternal.storage.auth.value().trusted[authId]) {
4949
meta.isTrusted = true
5050
meta.clientAuthId = authId
5151
}
5252

5353
wsClients.add(ws)
54-
console.log(c.green`${MARK_CHECK} Websocket client [${meta.id}] connected`)
54+
console.log(c.green`${MARK_CHECK} Websocket client [${meta.id}] connected [${meta.clientAuthId}] (${meta.isTrusted ? 'trusted' : 'untrusted'})`)
5555
},
5656
onDisconnected: (ws, meta) => {
5757
wsClients.delete(ws)

packages/kit/src/client/rpc-shared-state.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import type { RpcSharedStateGetOptions, RpcSharedStateHost } from '../types'
2-
import type { SharedState } from '../utils/shared-state'
2+
import type { SharedState, SharedStatePatch } from '../utils/shared-state'
33
import type { DevToolsRpcClient } from './rpc'
44
import { createSharedState } from '../utils/shared-state'
55

66
export 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

Comments
 (0)