Skip to content

Commit 10a4d8e

Browse files
committed
feat: make it working
1 parent a941fb7 commit 10a4d8e

File tree

8 files changed

+72
-30
lines changed

8 files changed

+72
-30
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ import { onMounted, shallowRef } from 'vue'
55
const stateRef = shallowRef<any>(undefined)
66
const isTrustedRef = shallowRef<boolean | null>(null)
77
8+
let increment = () => {}
9+
810
onMounted(async () => {
911
const client = await getDevToolsRpcClient()
10-
isTrustedRef.value = client.isTrusted
11-
const state = await client.sharedState.get('counter')
1212
13+
isTrustedRef.value = client.isTrusted
1314
client.events.on('rpc:is-trusted:updated', (isTrusted) => {
1415
isTrustedRef.value = isTrusted
1516
})
1617
18+
const state = await client.sharedState.get<{ count: number }>('counter')
19+
20+
increment = () => {
21+
state.mutate((state) => {
22+
state.count++
23+
})
24+
}
25+
1726
stateRef.value = state.value()
1827
state.on('updated', (newState) => {
19-
console.log('updated', newState)
2028
stateRef.value = newState
2129
})
2230
})
@@ -27,5 +35,8 @@ onMounted(async () => {
2735
<h1>DevTools </h1>
2836
<div>{{ isTrustedRef }}</div>
2937
<pre>{{ stateRef }}</pre>
38+
<button @click="increment">
39+
Increment
40+
</button>
3041
</div>
3142
</template>

packages/core/playground/vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import UnoCSS from 'unocss/vite'
44
import VueRouter from 'unplugin-vue-router/vite'
55
import { defineConfig } from 'vite'
66
import Tracer from 'vite-plugin-vue-tracer'
7+
import { alias } from '../../../alias'
78
// eslint-disable-next-line ts/ban-ts-comment
89
// @ts-ignore ignore the type error
910
import { DevToolsViteUI } from '../../vite/src/node'
@@ -16,6 +17,9 @@ export default defineConfig({
1617
'import.meta.env.VITE_DEVTOOLS_LOCAL_DEV': JSON.stringify(process.env.VITE_DEVTOOLS_LOCAL_DEV),
1718
},
1819
base: './',
20+
resolve: {
21+
alias,
22+
},
1923
plugins: [
2024
VueRouter(),
2125
Vue(),

packages/core/src/node/context.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,27 @@ export async function createDevToolsContext(
4646

4747
// Register hosts side effects
4848
docksHost.events.on('dock:entry:updated', debounce(() => {
49-
rpcHost.broadcast('vite:internal:docks:updated')
49+
rpcHost.broadcast({
50+
method: 'vite:internal:docks:updated',
51+
args: [],
52+
})
5053
}, 10))
5154
terminalsHost.events.on('terminal:session:updated', debounce(() => {
52-
rpcHost.broadcast('vite:internal:terminals:updated')
55+
rpcHost.broadcast({
56+
method: 'vite:internal:terminals:updated',
57+
args: [],
58+
})
5359
// New terminals might affect the visibility of the terminals dock entry, we trigger it here as well
54-
rpcHost.broadcast('vite:internal:docks:updated')
60+
rpcHost.broadcast({
61+
method: 'vite:internal:docks:updated',
62+
args: [],
63+
})
5564
}, 10))
5665
terminalsHost.events.on('terminal:session:stream-chunk', (data) => {
57-
rpcHost.broadcast('vite:internal:terminals:stream-chunk', data)
66+
rpcHost.broadcast({
67+
method: 'vite:internal:terminals:stream-chunk',
68+
args: [data],
69+
})
5870
})
5971

6072
// Register plugins

packages/core/src/node/host-functions.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DevToolsNodeContext, DevToolsNodeRpcSession, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, RpcFunctionsHost as RpcFunctionsHostType, RpcSharedStateHost } from '@vitejs/devtools-kit'
1+
import type { DevToolsNodeContext, DevToolsNodeRpcSession, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, RpcBroadcastOptions, RpcFunctionsHost as RpcFunctionsHostType, RpcSharedStateHost } from '@vitejs/devtools-kit'
22
import type { BirpcGroup } from 'birpc'
33
import type { AsyncLocalStorage } from 'node:async_hooks'
44
import { RpcFunctionsCollectorBase } from 'birpc-x'
@@ -19,22 +19,26 @@ export class RpcFunctionsHost extends RpcFunctionsCollectorBase<DevToolsRpcServe
1919

2020
sharedState: RpcSharedStateHost
2121

22-
broadcast<
22+
async broadcast<
2323
T extends keyof DevToolsRpcClientFunctions,
2424
Args extends Parameters<DevToolsRpcClientFunctions[T]>,
2525
>(
26-
name: T,
27-
...args: Args
28-
): Promise<(Awaited<ReturnType<DevToolsRpcClientFunctions[T]>> | undefined)[]> {
26+
options: RpcBroadcastOptions<T, Args>,
27+
): Promise<void> {
2928
if (!this._rpcGroup)
3029
throw new Error('RpcFunctionsHost] RpcGroup is not set, it likely to be an internal bug of Vite DevTools')
31-
// @ts-expect-error - BirpcGroup.broadcast.$callOptional is not typed correctly
32-
return this._rpcGroup.broadcast.$callRaw<T>({
33-
name,
34-
args,
35-
optional: true,
36-
event: true,
37-
})
30+
31+
await Promise.all(
32+
this._rpcGroup.clients.map((client) => {
33+
if (options.filter?.(client) === false)
34+
return undefined
35+
return client.$callRaw({
36+
optional: true,
37+
event: true,
38+
...options,
39+
})
40+
}),
41+
)
3842
}
3943

4044
getCurrentRpcSession(): DevToolsNodeRpcSession | undefined {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ export function createRpcSharedStateServerHost(
1313
offs.push(
1414
state.on('updated', (_fullState, patches, syncId) => {
1515
if (patches) {
16-
rpc.broadcast('vite:internal:rpc:client-state:patch', key, patches, syncId)
16+
rpc.broadcast({
17+
method: 'vite:internal:rpc:client-state:patch',
18+
args: [key, patches, syncId],
19+
})
1720
}
1821
else {
19-
rpc.broadcast('vite:internal:rpc:client-state:updated', key, syncId)
22+
rpc.broadcast({
23+
method: 'vite:internal:rpc:client-state:updated',
24+
args: [key, syncId],
25+
})
2026
}
2127
}),
2228
)

packages/core/src/node/ws.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createRpcServer } from '@vitejs/devtools-rpc'
88
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/server'
99
import c from 'ansis'
1010
import { getPort } from 'get-port-please'
11-
import { MARK_CHECK } from './constants'
11+
import { MARK_INFO } from './constants'
1212
import { getInternalContext } from './context-internal'
1313

1414
export interface CreateWsServerOptions {
@@ -51,11 +51,12 @@ export async function createWsServer(options: CreateWsServerOptions) {
5151
}
5252

5353
wsClients.add(ws)
54-
console.log(c.green`${MARK_CHECK} Websocket client [${meta.id}] connected [${meta.clientAuthId}] (${meta.isTrusted ? 'trusted' : 'untrusted'})`)
54+
const color = meta.isTrusted ? c.green : c.yellow
55+
console.log(color`${MARK_INFO} Websocket client connected. [${meta.id}] [${meta.clientAuthId}] (${meta.isTrusted ? 'trusted' : 'untrusted'})`)
5556
},
5657
onDisconnected: (ws, meta) => {
5758
wsClients.delete(ws)
58-
console.log(c.red`${MARK_CHECK} Websocket client [${meta.id}] disconnected`)
59+
console.log(c.red`${MARK_INFO} Websocket client disconnected. [${meta.id}]`)
5960
},
6061
})
6162

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import { createSharedState } from '../utils/shared-state'
66
export function createRpcSharedStateClientHost(rpc: DevToolsRpcClient): RpcSharedStateHost {
77
const sharedState = new Map<string, SharedState<any>>()
88

9-
console.log('createRpcSharedStateClientHost', { rpc })
10-
119
rpc.client.register({
1210
name: 'vite:internal:rpc:client-state:updated',
1311
type: 'event',
1412
handler: async (key: string, syncId: string) => {
15-
console.log('vite:internal:rpc:client-state:updated', { key, syncId })
1613
const state = sharedState.get(key)
1714
if (!state || state.syncIds.has(syncId))
1815
return

packages/kit/src/types/rpc.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export interface DevToolsNodeRpcSession {
1212
rpc: BirpcReturn<DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, false>
1313
}
1414

15+
export interface RpcBroadcastOptions<METHOD, Args extends any[]> {
16+
method: METHOD
17+
args: Args
18+
optional?: boolean
19+
event?: boolean
20+
filter?: (client: BirpcReturn<DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, false>) => boolean | void
21+
}
22+
1523
export type RpcFunctionsHost = RpcFunctionsCollectorBase<DevToolsRpcServerFunctions, DevToolsNodeContext> & {
1624
/**
1725
* Broadcast a message to all connected clients
@@ -20,9 +28,8 @@ export type RpcFunctionsHost = RpcFunctionsCollectorBase<DevToolsRpcServerFuncti
2028
T extends keyof DevToolsRpcClientFunctions,
2129
Args extends Parameters<DevToolsRpcClientFunctions[T]>,
2230
>(
23-
name: T,
24-
...args: Args
25-
) => Promise<(Awaited<ReturnType<DevToolsRpcClientFunctions[T]>> | undefined)[]>
31+
options: RpcBroadcastOptions<T, Args>,
32+
) => Promise<void>
2633

2734
/**
2835
* Get the current RPC client

0 commit comments

Comments
 (0)