Skip to content

Commit 98fc8ed

Browse files
LoTwTwebfansplz
andauthored
feat(client): module update track for graph page (#195)
Co-authored-by: arlo <[email protected]>
1 parent 93b33b8 commit 98fc8ed

File tree

12 files changed

+337
-43
lines changed

12 files changed

+337
-43
lines changed

packages/client/src/pages/graph.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import { Network } from 'vis-network'
44
55
const bridgeRpc = useDevToolsBridgeRpc()
66
7-
onDevToolsClientConnected(async () => {
7+
async function fetchGraph() {
88
const root = await bridgeRpc.root()
99
bridgeRpc.getGraph().then((res) => {
1010
parseGraphRawData(res, root)
1111
})
12+
}
13+
14+
let cleanupModuleUpdatedEffect: Function
15+
16+
onDevToolsClientConnected(() => {
17+
fetchGraph()
18+
cleanupModuleUpdatedEffect = bridgeRpc.graphModuleUpdated(() => {
19+
fetchGraph()
20+
})
1221
})
1322
1423
const container = ref<HTMLDivElement>()
@@ -45,6 +54,7 @@ onMounted(() => {
4554
onUnmounted(() => {
4655
cleanupGraphRelatedStates()
4756
networkRef.value?.destroy()
57+
cleanupModuleUpdatedEffect?.()
4858
})
4959
5060
const navbarRef = ref<HTMLElement>()

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
"image-meta": "^0.2.0",
3636
"mitt": "^3.0.1",
3737
"pathe": "^1.1.2",
38+
"perfect-debounce": "^1.0.0",
3839
"vite-dev-rpc": "^0.1.4",
3940
"vite-hot-client": "^0.2.3",
40-
"vite-plugin-inspect": "^0.8.2"
41+
"vite-plugin-inspect": "^0.8.3"
4142
},
4243
"devDependencies": {
4344
"vue": "^3.4.15"

packages/core/src/bridge/devtools.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ const devtoolsBridge: {
1212
viteRpc: {
1313
enabled: boolean
1414
api: ReturnType<typeof setupViteRPCClient>
15+
on: {
16+
moduleUpdated: (fn: Function) => void
17+
}
18+
off: {
19+
moduleUpdated: () => void
20+
}
1521
}
1622
rpc: InstanceType<typeof BridgeRpcCore>
1723
} = {
1824
value: null!,
1925
viteRpc: {
2026
enabled: false,
2127
api: null!,
28+
on: {
29+
moduleUpdated() {},
30+
},
31+
off: {
32+
moduleUpdated() {},
33+
},
2234
},
2335
rpc: null!,
2436
}
@@ -28,13 +40,30 @@ export interface BridgeRpcOptions {
2840
bridge: BridgeInstanceType
2941
}
3042
export function registerBridgeRpc(options: BridgeRpcOptions) {
31-
const rpc = setupViteRPCClient(options.viteRPCContext)
3243
devtoolsBridge.value = options.bridge
3344
devtoolsBridge.rpc = new BridgeRpcCore(options.bridge)
45+
46+
const moduleUpdatedFn: Function[] = []
47+
const rpc = setupViteRPCClient(options.viteRPCContext, {
48+
moduleUpdated: () => {
49+
moduleUpdatedFn.forEach(fn => fn())
50+
},
51+
})
52+
3453
if (rpc) {
3554
devtoolsBridge.viteRpc = {
3655
enabled: true,
3756
api: rpc,
57+
on: {
58+
moduleUpdated(fn: Function) {
59+
moduleUpdatedFn.push(fn)
60+
},
61+
},
62+
off: {
63+
moduleUpdated() {
64+
moduleUpdatedFn.length = 0
65+
},
66+
},
3867
}
3968
}
4069
}
@@ -176,4 +205,10 @@ export class BridgeRpc {
176205
static getGraph() {
177206
return devtoolsBridge.viteRpc!.api!.getGraph()
178207
}
208+
209+
// graph module udpated
210+
static graphModuleUpdated(fn: Function) {
211+
devtoolsBridge.viteRpc!.on.moduleUpdated(fn)
212+
return () => devtoolsBridge.viteRpc!.off.moduleUpdated()
213+
}
179214
}

packages/core/src/vite-rpc/client.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import { createRPCClient } from 'vite-dev-rpc'
33
import type { BirpcReturn } from 'birpc'
44
import type { ViteRPCFunctions } from './types'
55

6-
export function setupViteRPCClient(ctx: ViteHotContext | undefined): BirpcReturn<ViteRPCFunctions, unknown> {
6+
export interface SetupViteRPCClientOptions {
7+
moduleUpdated?: () => void
8+
}
9+
export function setupViteRPCClient(ctx: ViteHotContext | undefined, options: SetupViteRPCClientOptions = {}): BirpcReturn<ViteRPCFunctions, unknown> {
710
if (!ctx)
811
return null!
9-
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {})
12+
13+
const { moduleUpdated = () => {} } = options
14+
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {
15+
moduleUpdated,
16+
}, {
17+
timeout: -1,
18+
})
1019
return rpcClient
1120
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import type { ViteInspectAPI } from 'vite-plugin-inspect'
2-
import type { ModuleInfo } from './types'
2+
import { debounce } from 'perfect-debounce'
3+
import type { BirpcGroupReturn } from 'birpc'
4+
import type { ModuleInfo, ViteRPCFunctions } from './types'
35

46
export interface SetupGraphOptions {
57
rpc: ViteInspectAPI['rpc']
8+
server: any
9+
getRpcServer: () => BirpcGroupReturn<ViteRPCFunctions>
610
}
711
export function setupGraphRPC(options: SetupGraphOptions) {
8-
const { rpc } = options
12+
const { rpc, server, getRpcServer } = options
13+
14+
const debouncedModuleUpdated = debounce(() => {
15+
getRpcServer().moduleUpdated()
16+
}, 100)
17+
18+
server.middlewares.use((req, res, next) => {
19+
debouncedModuleUpdated()
20+
next()
21+
})
22+
923
return {
1024
async getGraph(): Promise<ModuleInfo[]> {
1125
const list = await rpc.list()
1226
const modules = list?.modules || []
1327

1428
return modules
1529
},
30+
moduleUpdated: () => {},
1631
}
1732
}

packages/core/src/vite-rpc/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import type { BirpcGroupReturn } from 'birpc'
33
import { createRPCServer } from 'vite-dev-rpc'
44
import type { ViteRPCFunctions } from './types'
55

6-
export async function setupViteRPCServer(ws: WebSocketServer, functions: ViteRPCFunctions): Promise<BirpcGroupReturn<ViteRPCFunctions>> {
7-
const rpcServer = createRPCServer<ViteRPCFunctions>('vite-plugin-vue-devtools', ws, functions)
6+
export function setupViteRPCServer(ws: WebSocketServer, functions: ViteRPCFunctions): BirpcGroupReturn<ViteRPCFunctions> {
7+
const rpcServer = createRPCServer<ViteRPCFunctions>('vite-plugin-vue-devtools', ws, functions, {
8+
timeout: -1,
9+
})
810
return rpcServer
911
}

packages/core/src/vite-rpc/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ export interface ViteRPCFunctions {
4444
getStaticAssets(): Promise<AssetInfo[]>
4545
getImageMeta(filepath: string): Promise<ImageMeta | undefined>
4646
getTextAssetContent(filepath: string, limit?: number): Promise<string | undefined>
47+
moduleUpdated: () => void
4748
}

packages/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"serve": "^14.2.1",
2626
"typescript": "^5.3.3",
2727
"vite": "^5.0.12",
28-
"vite-plugin-inspect": "^0.8.2",
28+
"vite-plugin-inspect": "^0.8.3",
2929
"vite-plugin-vue-devtools": "workspace:*"
3030
}
3131
}

packages/playground/src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { addCustomCommand, addCustomTab } from '@vue/devtools-api'
88
import App2 from './App.vue'
99
import App from './App.preview.vue'
1010
import Home from './pages/Home.vue'
11-
import Hello from './pages/Hello.vue'
11+
12+
// import Hello from './pages/Hello.vue'
1213
import Hey from './pages/Hey.vue'
1314
import './style.css'
1415

@@ -33,7 +34,8 @@ const routes: RouteRecordRaw[] = [
3334
},
3435
{
3536
path: '/hello',
36-
component: Hello,
37+
// component: Hello,
38+
component: () => import('./pages/Hello.vue'),
3739
name: 'hello',
3840
},
3941
{

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"birpc": "^0.2.14",
5656
"execa": "^8.0.1",
5757
"sirv": "^2.0.4",
58-
"vite-plugin-inspect": "^0.8.2",
58+
"vite-plugin-inspect": "^0.8.3",
5959
"vite-plugin-vue-inspector": "^4.0.2"
6060
},
6161
"devDependencies": {

0 commit comments

Comments
 (0)