Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/devtools/client/composables/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { useRoute, useRouter } from '#imports'
import type { NuxtDevtoolsClient, NuxtDevtoolsHostClient, NuxtDevtoolsIframeClient } from '@nuxt/devtools-kit/types'
import type { Unhead } from '@unhead/schema'
import type { DevToolsRpcClient } from '@vitejs/devtools-kit/client'
import type { ComputedRef } from 'vue'
import { useState } from '#imports'
import { useColorMode } from '@vueuse/core'
import { computed, ref } from 'vue'
import { renderMarkdown } from './client-services/markdown'
import { renderCodeHighlight } from './client-services/shiki'
import { extendedRpcMap, rpc } from './rpc'
import { connectPromise, rpc, rpcClient } from './rpc'

export function useClient() {
return useState<NuxtDevtoolsHostClient>('devtools-client')
Expand Down Expand Up @@ -59,13 +60,31 @@ export function useInjectionClient(): ComputedRef<NuxtDevtoolsIframeClient> {
return renderMarkdown(code)
},
extendClientRpc(namespace, functions) {
extendedRpcMap.set(namespace, functions)
const register = (client: DevToolsRpcClient) => {
for (const [name, handler] of Object.entries(functions)) {
if (typeof handler === 'function') {
client.client.register({
name: `${namespace}:${name}`,
type: 'event',
handler: handler as any,
})
}
}
}

if (rpcClient)
register(rpcClient)
else
connectPromise.then(register)

return new Proxy({}, {
get(_, key) {
if (typeof key !== 'string')
return
return (rpc as any)[`${namespace}:${key}`]
return async (...args: any[]) => {
const client = rpcClient || await connectPromise
return client.call(`${namespace}:${key}` as any, ...args as any)
}
},
})
},
Expand Down
26 changes: 2 additions & 24 deletions packages/devtools/client/composables/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// will be added in setup/client-rpc.ts
} as ClientFunctions

export const extendedRpcMap = new Map<string, any>()
export let rpcClient: DevToolsRpcClient | undefined

Check failure on line 15 in packages/devtools/client/composables/rpc.ts

View workflow job for this annotation

GitHub Actions / ci

Exporting mutable 'let' binding, use 'const' instead

let rpcClient: DevToolsRpcClient | undefined

const connectPromise = connectDevToolsRpc()
export const connectPromise = connectDevToolsRpc()

/**
* Proxy-based RPC object that provides backward-compatible `rpc.functionName()` interface.
Expand All @@ -26,13 +24,6 @@
get: (_, method: string) => {
return async (...args: any[]) => {
const client = rpcClient || await connectPromise
// Check extended RPC map first for namespaced functions
if (method.includes(':')) {
const [namespace, fnName] = method.split(':') as [string, string]
const extFn = extendedRpcMap.get(namespace)?.[fnName]
if (extFn)
return extFn(...args)
}
return client.call(method as any, ...args as any)
}
},
Expand All @@ -55,19 +46,6 @@
}
}

// Register extended client RPC functions
for (const [namespace, fns] of extendedRpcMap) {
for (const [fnName, handler] of Object.entries(fns)) {
if (typeof handler === 'function') {
client.client.register({
name: `${namespace}:${fnName}`,
type: 'event',
handler: handler as any,
})
}
}
}

// eslint-disable-next-line no-console
console.log('[nuxt-devtools] Connected to Vite DevTools RPC')
wsConnecting.value = false
Expand Down
Loading