Skip to content

Commit

Permalink
feat(client): module update track for graph page (#195)
Browse files Browse the repository at this point in the history

Co-authored-by: arlo <webfansplz@gmail.com>
  • Loading branch information
LoTwT and webfansplz committed Jan 26, 2024
1 parent 93b33b8 commit 98fc8ed
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 43 deletions.
12 changes: 11 additions & 1 deletion packages/client/src/pages/graph.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import { Network } from 'vis-network'
const bridgeRpc = useDevToolsBridgeRpc()
onDevToolsClientConnected(async () => {
async function fetchGraph() {
const root = await bridgeRpc.root()
bridgeRpc.getGraph().then((res) => {
parseGraphRawData(res, root)
})
}
let cleanupModuleUpdatedEffect: Function
onDevToolsClientConnected(() => {
fetchGraph()
cleanupModuleUpdatedEffect = bridgeRpc.graphModuleUpdated(() => {
fetchGraph()
})
})
const container = ref<HTMLDivElement>()
Expand Down Expand Up @@ -45,6 +54,7 @@ onMounted(() => {
onUnmounted(() => {
cleanupGraphRelatedStates()
networkRef.value?.destroy()
cleanupModuleUpdatedEffect?.()
})
const navbarRef = ref<HTMLElement>()
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
"image-meta": "^0.2.0",
"mitt": "^3.0.1",
"pathe": "^1.1.2",
"perfect-debounce": "^1.0.0",
"vite-dev-rpc": "^0.1.4",
"vite-hot-client": "^0.2.3",
"vite-plugin-inspect": "^0.8.2"
"vite-plugin-inspect": "^0.8.3"
},
"devDependencies": {
"vue": "^3.4.15"
Expand Down
37 changes: 36 additions & 1 deletion packages/core/src/bridge/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ const devtoolsBridge: {
viteRpc: {
enabled: boolean
api: ReturnType<typeof setupViteRPCClient>
on: {
moduleUpdated: (fn: Function) => void
}
off: {
moduleUpdated: () => void
}
}
rpc: InstanceType<typeof BridgeRpcCore>
} = {
value: null!,
viteRpc: {
enabled: false,
api: null!,
on: {
moduleUpdated() {},
},
off: {
moduleUpdated() {},
},
},
rpc: null!,
}
Expand All @@ -28,13 +40,30 @@ export interface BridgeRpcOptions {
bridge: BridgeInstanceType
}
export function registerBridgeRpc(options: BridgeRpcOptions) {
const rpc = setupViteRPCClient(options.viteRPCContext)
devtoolsBridge.value = options.bridge
devtoolsBridge.rpc = new BridgeRpcCore(options.bridge)

const moduleUpdatedFn: Function[] = []
const rpc = setupViteRPCClient(options.viteRPCContext, {
moduleUpdated: () => {
moduleUpdatedFn.forEach(fn => fn())
},
})

if (rpc) {
devtoolsBridge.viteRpc = {
enabled: true,
api: rpc,
on: {
moduleUpdated(fn: Function) {
moduleUpdatedFn.push(fn)
},
},
off: {
moduleUpdated() {
moduleUpdatedFn.length = 0
},
},
}
}
}
Expand Down Expand Up @@ -176,4 +205,10 @@ export class BridgeRpc {
static getGraph() {
return devtoolsBridge.viteRpc!.api!.getGraph()
}

// graph module udpated
static graphModuleUpdated(fn: Function) {
devtoolsBridge.viteRpc!.on.moduleUpdated(fn)
return () => devtoolsBridge.viteRpc!.off.moduleUpdated()
}
}
13 changes: 11 additions & 2 deletions packages/core/src/vite-rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import { createRPCClient } from 'vite-dev-rpc'
import type { BirpcReturn } from 'birpc'
import type { ViteRPCFunctions } from './types'

export function setupViteRPCClient(ctx: ViteHotContext | undefined): BirpcReturn<ViteRPCFunctions, unknown> {
export interface SetupViteRPCClientOptions {
moduleUpdated?: () => void
}
export function setupViteRPCClient(ctx: ViteHotContext | undefined, options: SetupViteRPCClientOptions = {}): BirpcReturn<ViteRPCFunctions, unknown> {
if (!ctx)
return null!
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {})

const { moduleUpdated = () => {} } = options
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {
moduleUpdated,
}, {
timeout: -1,
})
return rpcClient
}
19 changes: 17 additions & 2 deletions packages/core/src/vite-rpc/graph.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import type { ViteInspectAPI } from 'vite-plugin-inspect'
import type { ModuleInfo } from './types'
import { debounce } from 'perfect-debounce'
import type { BirpcGroupReturn } from 'birpc'
import type { ModuleInfo, ViteRPCFunctions } from './types'

export interface SetupGraphOptions {
rpc: ViteInspectAPI['rpc']
server: any
getRpcServer: () => BirpcGroupReturn<ViteRPCFunctions>
}
export function setupGraphRPC(options: SetupGraphOptions) {
const { rpc } = options
const { rpc, server, getRpcServer } = options

const debouncedModuleUpdated = debounce(() => {
getRpcServer().moduleUpdated()
}, 100)

server.middlewares.use((req, res, next) => {
debouncedModuleUpdated()
next()
})

return {
async getGraph(): Promise<ModuleInfo[]> {
const list = await rpc.list()
const modules = list?.modules || []

return modules
},
moduleUpdated: () => {},
}
}
6 changes: 4 additions & 2 deletions packages/core/src/vite-rpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { BirpcGroupReturn } from 'birpc'
import { createRPCServer } from 'vite-dev-rpc'
import type { ViteRPCFunctions } from './types'

export async function setupViteRPCServer(ws: WebSocketServer, functions: ViteRPCFunctions): Promise<BirpcGroupReturn<ViteRPCFunctions>> {
const rpcServer = createRPCServer<ViteRPCFunctions>('vite-plugin-vue-devtools', ws, functions)
export function setupViteRPCServer(ws: WebSocketServer, functions: ViteRPCFunctions): BirpcGroupReturn<ViteRPCFunctions> {
const rpcServer = createRPCServer<ViteRPCFunctions>('vite-plugin-vue-devtools', ws, functions, {
timeout: -1,
})
return rpcServer
}
1 change: 1 addition & 0 deletions packages/core/src/vite-rpc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export interface ViteRPCFunctions {
getStaticAssets(): Promise<AssetInfo[]>
getImageMeta(filepath: string): Promise<ImageMeta | undefined>
getTextAssetContent(filepath: string, limit?: number): Promise<string | undefined>
moduleUpdated: () => void
}
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"serve": "^14.2.1",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-inspect": "^0.8.2",
"vite-plugin-inspect": "^0.8.3",
"vite-plugin-vue-devtools": "workspace:*"
}
}
6 changes: 4 additions & 2 deletions packages/playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { addCustomCommand, addCustomTab } from '@vue/devtools-api'
import App2 from './App.vue'
import App from './App.preview.vue'
import Home from './pages/Home.vue'
import Hello from './pages/Hello.vue'

// import Hello from './pages/Hello.vue'
import Hey from './pages/Hey.vue'
import './style.css'

Expand All @@ -33,7 +34,8 @@ const routes: RouteRecordRaw[] = [
},
{
path: '/hello',
component: Hello,
// component: Hello,
component: () => import('./pages/Hello.vue'),
name: 'hello',
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"birpc": "^0.2.14",
"execa": "^8.0.1",
"sirv": "^2.0.4",
"vite-plugin-inspect": "^0.8.2",
"vite-plugin-inspect": "^0.8.3",
"vite-plugin-vue-inspector": "^4.0.2"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ export default function VitePluginVueDevTools(options?: VitePluginVueDevToolsOpt
single: true,
dev: true,
}))
setupViteRPCServer(server.ws, {
const rpcServer = setupViteRPCServer(server.ws, {
root: () => config.root,
...setupAssetsRPC({
root: config.root,
base,
}),
...setupGraphRPC({
rpc: inspect.api.rpc,
server,
getRpcServer: () => rpcServer,
}),
})

Expand Down
Loading

0 comments on commit 98fc8ed

Please sign in to comment.