Skip to content

Commit 30780b7

Browse files
committed
feat(kit): support ctx.clientRpc registration
1 parent 69d06d2 commit 30780b7

File tree

10 files changed

+82
-89
lines changed

10 files changed

+82
-89
lines changed

packages/core/src/client/inject/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export async function init(): Promise<void> {
1010
// eslint-disable-next-line no-console
1111
console.log('[VITE DEVTOOLS] Client injected')
1212

13-
const { rpc } = await getDevToolsRpcClient()
13+
const rpcReturn = await getDevToolsRpcClient()
14+
const { rpc } = rpcReturn
1415

1516
// eslint-disable-next-line no-console
1617
console.log('[VITE DEVTOOLS] RPC', rpc)
@@ -35,7 +36,7 @@ export async function init(): Promise<void> {
3536

3637
const context = await createDocksContext(
3738
'embedded',
38-
rpc,
39+
rpcReturn,
3940
state,
4041
)
4142

packages/core/src/client/standalone/App.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { createDocksContext } from '../webcomponents/state/dock'
99
import { useStateHandlers } from '../webcomponents/state/state'
1010
import { PresistedDomViewsManager } from '../webcomponents/utils/PresistedDomViewsManager'
1111
12-
const { rpc } = await getDevToolsRpcClient()
12+
const rpcReturn = await getDevToolsRpcClient()
13+
const { rpc } = rpcReturn
1314
1415
// eslint-disable-next-line no-console
1516
console.log('[VITE DEVTOOLS] RPC', rpc)
@@ -19,7 +20,7 @@ const presistedDoms = markRaw(new PresistedDomViewsManager(viewsContainer))
1920
2021
const context: DocksContext = await createDocksContext(
2122
'standalone',
22-
rpc,
23+
rpcReturn,
2324
)
2425
2526
context.docks.selected ||= context.docks.entries[0] || null

packages/core/src/client/webcomponents/state/dock.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DevToolsDockEntry } from '@vitejs/devtools-kit'
2-
import type { DevToolsRpcClient, DockEntryState, DockEntryStateEvents, DockPanelStorage, DocksContext } from '@vitejs/devtools-kit/client'
2+
import type { ClientRpcReturn, DockEntryState, DockEntryStateEvents, DockPanelStorage, DocksContext } from '@vitejs/devtools-kit/client'
33
import type { Ref } from 'vue'
44
import { createNanoEvents } from 'nanoevents'
55
import { computed, markRaw, reactive, ref, shallowRef, watch } from 'vue'
@@ -54,11 +54,11 @@ function createDockEntryState(
5454

5555
export async function createDocksContext(
5656
clientType: 'embedded' | 'standalone',
57-
rpc: DevToolsRpcClient,
57+
rpcReturn: ClientRpcReturn,
5858
panelStore?: Ref<DockPanelStorage>,
5959
): Promise<DocksContext> {
6060
const selected = ref<DevToolsDockEntry | null>(null)
61-
const dockEntries = shallowRef((await rpc.$call('vite:core:list-dock-entries')).map(entry => Object.freeze(entry)))
61+
const dockEntries = shallowRef((await rpcReturn.rpc.$call('vite:core:list-dock-entries')).map(entry => Object.freeze(entry)))
6262
// eslint-disable-next-line no-console
6363
console.log('[VITE DEVTOOLS] Docks Entries', [...dockEntries.value])
6464
// TODO: get board case from rpc when entries updates
@@ -98,7 +98,8 @@ export async function createDocksContext(
9898
return true
9999
},
100100
},
101-
rpc,
101+
rpc: rpcReturn.rpc,
102+
clientRpc: rpcReturn.clientRpc,
102103
clientType,
103104
})
104105
}

packages/kit/src/client/docks.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { RpcFunctionsCollector } from 'birpc-x'
12
import type { Emitter as EventsEmitter } from 'nanoevents'
23
import type { Raw } from 'vue'
3-
import type { DevToolsDockEntry } from '../types'
4+
import type { DevToolsDockEntry, DevToolsRpcClientFunctions } from '../types'
45
import type { DevToolsRpcClient } from './rpc'
56

67
export interface DockPanelStorage {
@@ -15,28 +16,37 @@ export interface DockPanelStorage {
1516

1617
export type DockClientType = 'embedded' | 'standalone'
1718

18-
export interface DocksContext {
19+
export interface DevToolsClientContext {
20+
/**
21+
* The RPC client to interact with the server
22+
*/
23+
readonly rpc: DevToolsRpcClient
24+
}
25+
26+
export interface DocksContext extends DevToolsClientContext {
1927
/**
2028
* Type of the client environment
2129
*
2230
* 'embedded' - running inside an embedded floating panel
2331
* 'standalone' - running inside a standlone window (no user app)
2432
*/
2533
readonly clientType: 'embedded' | 'standalone'
26-
/**
27-
* The RPC client to interact with the server
28-
*/
29-
readonly rpc: DevToolsRpcClient
3034
/**
3135
* The panel context
3236
*/
33-
panel: DocksPanelContext
37+
readonly panel: DocksPanelContext
3438
/**
3539
* The docks entries context
3640
*/
37-
docks: DocksEntriesContext
41+
readonly docks: DocksEntriesContext
42+
/**
43+
* The client-side RPC functions to be called from the server
44+
*/
45+
readonly clientRpc: DevToolsClientRpcHost
3846
}
3947

48+
export type DevToolsClientRpcHost = RpcFunctionsCollector<DevToolsRpcClientFunctions, DevToolsClientContext>
49+
4050
export interface DocksPanelContext {
4151
store: DockPanelStorage
4252
isDragging: boolean

packages/kit/src/client/rpc.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client'
22
import type { BirpcOptions, BirpcReturn } from 'birpc'
33
import type { ConnectionMeta, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions } from '../types'
4+
import type { DevToolsClientContext, DevToolsClientRpcHost } from './docks'
45
import { createRpcClient } from '@vitejs/devtools-rpc'
56
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
7+
import { RpcFunctionsCollectorBase } from 'birpc-x'
68

79
function isNumeric(str: string | number | undefined) {
810
if (str == null)
@@ -19,12 +21,15 @@ export interface DevToolsRpcClientOptions {
1921

2022
export type DevToolsRpcClient = BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>
2123

22-
export async function getDevToolsRpcClient(
23-
options: DevToolsRpcClientOptions = {},
24-
): Promise<{
24+
export interface ClientRpcReturn {
2525
connectionMeta: ConnectionMeta
2626
rpc: DevToolsRpcClient
27-
}> {
27+
clientRpc: DevToolsClientRpcHost
28+
}
29+
30+
export async function getDevToolsRpcClient(
31+
options: DevToolsRpcClientOptions = {},
32+
): Promise<ClientRpcReturn> {
2833
const {
2934
baseURL = '/.devtools/',
3035
rpcOptions = {},
@@ -55,16 +60,26 @@ export async function getDevToolsRpcClient(
5560
? `${location.protocol.replace('http', 'ws')}//${location.hostname}:${connectionMeta.websocket}`
5661
: connectionMeta.websocket as string
5762

58-
const rpc = createRpcClient<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>({}, {
59-
preset: createWsRpcPreset({
60-
url,
61-
...options.wsOptions,
62-
}),
63-
rpcOptions,
64-
})
63+
const context: DevToolsClientContext = {
64+
rpc: undefined!,
65+
}
66+
const clientRpc: DevToolsClientRpcHost = new RpcFunctionsCollectorBase<DevToolsRpcClientFunctions, DevToolsClientContext>(context)
67+
const rpc = createRpcClient<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>(
68+
clientRpc.functions,
69+
{
70+
preset: createWsRpcPreset({
71+
url,
72+
...options.wsOptions,
73+
}),
74+
rpcOptions,
75+
},
76+
)
77+
// @ts-expect-error assign to readonly property
78+
context.rpc = rpc
6579

6680
return {
6781
connectionMeta,
6882
rpc,
83+
clientRpc,
6984
}
7085
}

packages/kit/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export * from './utils'
44
export * from './views'
55
export * from './vite-augment'
66
export * from './vite-plugin'
7+
8+
export type { RpcDefinitionsFilter, RpcDefinitionsToFunctions } from 'birpc-x'

packages/kit/src/types/rpc.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,5 @@
11
import type { RpcFunctionsCollectorBase } from 'birpc-x'
22
import type { DevToolsRpcServerFunctions } from './rpc-augments'
3-
import type { EntriesToObject, Thenable } from './utils'
43
import type { DevToolsNodeContext } from './vite-plugin'
54

6-
export type { BirpcFn, BirpcReturn } from 'birpc'
7-
8-
/**
9-
* Type of the RPC function,
10-
* - static: A function that returns a static data (can be cached and dumped)
11-
* - action: A function that performs an action (no data returned)
12-
* - query: A function that queries a resource
13-
*/
14-
export type RpcFunctionType = 'static' | 'action' | 'query'
15-
165
export type RpcFunctionsHost = RpcFunctionsCollectorBase<DevToolsRpcServerFunctions, DevToolsNodeContext>
17-
18-
export interface RpcFunctionSetupResult<
19-
ARGS extends any[],
20-
RETURN = void,
21-
> {
22-
handler: (...args: ARGS) => RETURN
23-
}
24-
25-
// TODO: maybe we should introduce schema system with valibot
26-
27-
export interface RpcFunctionDefinition<
28-
NAME extends string,
29-
TYPE extends RpcFunctionType,
30-
ARGS extends any[] = [],
31-
RETURN = void,
32-
> {
33-
name: NAME
34-
type: TYPE
35-
setup: (context: DevToolsNodeContext) => Thenable<RpcFunctionSetupResult<ARGS, RETURN>>
36-
handler?: (...args: ARGS) => RETURN
37-
__resolved?: RpcFunctionSetupResult<ARGS, RETURN>
38-
__promise?: Thenable<RpcFunctionSetupResult<ARGS, RETURN>>
39-
}
40-
41-
export type RpcDefinitionsToFunctions<T extends readonly RpcFunctionDefinition<any, any, any>[]> = EntriesToObject<{
42-
[K in keyof T]: [T[K]['name'], Awaited<ReturnType<T[K]['setup']>>['handler']]
43-
}>
44-
45-
export type RpcDefinitionsFilter<
46-
T extends readonly RpcFunctionDefinition<any, any, any>[],
47-
Type extends RpcFunctionType,
48-
> = {
49-
[K in keyof T]: T[K] extends { type: Type } ? T[K] : never
50-
}

packages/vite/src/app/composables/rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {} from '@vitejs/devtools'
2-
import type { BirpcReturn, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions } from '@vitejs/devtools-kit'
2+
import type { ClientRpcReturn } from '@vitejs/devtools-kit/client'
33
import type {} from '../../node/rpc'
44
import { useRuntimeConfig } from '#app/nuxt'
55
import { getDevToolsRpcClient } from '@vitejs/devtools-kit/client'
@@ -13,7 +13,7 @@ export const connectionState = reactive<{
1313
error: null,
1414
})
1515

16-
const rpc = shallowRef<BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>>(undefined!)
16+
const rpc = shallowRef<ClientRpcReturn['rpc']>(undefined!)
1717

1818
export async function connect() {
1919
const runtimeConfig = useRuntimeConfig()

pnpm-lock.yaml

Lines changed: 22 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)