Skip to content

Commit e65ba84

Browse files
authored
feat: update assets in real time (#212)
1 parent 3da9850 commit e65ba84

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

packages/client/src/pages/assets.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,19 @@ const byTree = computed(() => {
7878
return root.children
7979
})
8080
81-
onDevToolsClientConnected(() => {
81+
let cleanupAssetsUpdatedEffect: Function
82+
83+
function fetchAssets() {
8284
bridgeRpc.getStaticAssets().then((res) => {
8385
assets.value = res
8486
})
87+
}
88+
89+
onDevToolsClientConnected(() => {
90+
fetchAssets()
91+
cleanupAssetsUpdatedEffect = bridgeRpc.assetsUpdated(() => {
92+
fetchAssets()
93+
})
8594
})
8695
function toggleView() {
8796
view.value = view.value === 'list' ? 'grid' : 'list'

packages/core/src/bridge/devtools.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ const devtoolsBridge: {
1414
api: ReturnType<typeof setupViteRPCClient>
1515
on: {
1616
moduleUpdated: (fn: Function) => void
17+
assetsUpdated: (fn: Function) => void
1718
}
1819
off: {
1920
moduleUpdated: () => void
21+
assetsUpdated: () => void
2022
}
2123
}
2224
rpc: InstanceType<typeof BridgeRpcCore>
@@ -27,9 +29,11 @@ const devtoolsBridge: {
2729
api: null!,
2830
on: {
2931
moduleUpdated() {},
32+
assetsUpdated() {},
3033
},
3134
off: {
3235
moduleUpdated() {},
36+
assetsUpdated() {},
3337
},
3438
},
3539
rpc: null!,
@@ -44,10 +48,15 @@ export function registerBridgeRpc(options: BridgeRpcOptions) {
4448
devtoolsBridge.rpc = new BridgeRpcCore(options.bridge)
4549

4650
const moduleUpdatedFn: Function[] = []
51+
const assetsUpdatedFn: Function[] = []
52+
4753
const rpc = setupViteRPCClient(options.viteRPCContext, {
4854
moduleUpdated: () => {
4955
moduleUpdatedFn.forEach(fn => fn())
5056
},
57+
assetsUpdated: () => {
58+
assetsUpdatedFn.forEach(fn => fn())
59+
},
5160
})
5261

5362
if (rpc) {
@@ -58,11 +67,17 @@ export function registerBridgeRpc(options: BridgeRpcOptions) {
5867
moduleUpdated(fn: Function) {
5968
moduleUpdatedFn.push(fn)
6069
},
70+
assetsUpdated(fn) {
71+
assetsUpdatedFn.push(fn)
72+
},
6173
},
6274
off: {
6375
moduleUpdated() {
6476
moduleUpdatedFn.length = 0
6577
},
78+
assetsUpdated() {
79+
assetsUpdatedFn.length = 0
80+
},
6681
},
6782
}
6883
}
@@ -211,4 +226,10 @@ export class BridgeRpc {
211226
devtoolsBridge.viteRpc!.on.moduleUpdated(fn)
212227
return () => devtoolsBridge.viteRpc!.off.moduleUpdated()
213228
}
229+
230+
// assets updated
231+
static assetsUpdated(fn: Function) {
232+
devtoolsBridge.viteRpc!.on.assetsUpdated(fn)
233+
return () => devtoolsBridge.viteRpc!.off.assetsUpdated()
234+
}
214235
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import fsp from 'node:fs/promises'
22
import fg from 'fast-glob'
33
import { join, resolve } from 'pathe'
44
import { imageMeta } from 'image-meta'
5+
import { BirpcGroupReturn } from 'birpc'
6+
import { debounce } from 'perfect-debounce'
57
import type { AssetInfo, AssetType, ImageMeta, ViteRPCFunctions } from './types'
68

79
const defaultAllowedExtensions = [
@@ -52,9 +54,14 @@ function guessType(path: string): AssetType {
5254
interface SetupAssetsOptions {
5355
root: string
5456
base: string
57+
server: any
58+
getRpcServer: () => BirpcGroupReturn<ViteRPCFunctions>
5559
}
5660

5761
export function setupAssetsRPC(config: SetupAssetsOptions) {
62+
const server = config.server
63+
const getRpcServer = config.getRpcServer
64+
5865
const _imageMetaCache = new Map<string, ImageMeta | undefined>()
5966
let cache: AssetInfo[] | null = null
6067

@@ -103,6 +110,19 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
103110
return cache
104111
}
105112

113+
function watchAssets() {
114+
const debouncedAssetsUpdated = debounce(() => {
115+
getRpcServer().assetsUpdated()
116+
}, 100)
117+
118+
server.watcher.on('all', (event) => {
119+
if (event !== 'change')
120+
debouncedAssetsUpdated()
121+
})
122+
}
123+
124+
watchAssets()
125+
106126
return {
107127
async getStaticAssets() {
108128
return await scan()
@@ -131,5 +151,6 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
131151
return undefined
132152
}
133153
},
154+
assetsUpdated() {},
134155
} satisfies Partial<ViteRPCFunctions>
135156
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import type { ViteRPCFunctions } from './types'
55

66
export interface SetupViteRPCClientOptions {
77
moduleUpdated?: () => void
8+
assetsUpdated?: () => void
89
}
910
export function setupViteRPCClient(ctx: ViteHotContext | undefined, options: SetupViteRPCClientOptions = {}): BirpcReturn<ViteRPCFunctions, unknown> {
1011
if (!ctx)
1112
return null!
1213

13-
const { moduleUpdated = () => {} } = options
14+
const { moduleUpdated = () => {}, assetsUpdated = () => {} } = options
1415
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {
1516
moduleUpdated,
17+
assetsUpdated,
1618
}, {
1719
timeout: -1,
1820
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ export interface ViteRPCFunctions {
4545
getImageMeta(filepath: string): Promise<ImageMeta | undefined>
4646
getTextAssetContent(filepath: string, limit?: number): Promise<string | undefined>
4747
moduleUpdated: () => void
48+
assetsUpdated: () => void
4849
}

packages/vite/src/vite.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,20 @@ export default function VitePluginVueDevTools(options?: VitePluginVueDevToolsOpt
8383
...setupAssetsRPC({
8484
root: config.root,
8585
base,
86+
server,
87+
getRpcServer,
8688
}),
8789
...setupGraphRPC({
8890
rpc: inspect.api.rpc,
8991
server,
90-
getRpcServer: () => rpcServer,
92+
getRpcServer,
9193
}),
9294
})
9395

96+
function getRpcServer() {
97+
return rpcServer
98+
}
99+
94100
const _printUrls = server.printUrls
95101
const colorUrl = (url: string) =>
96102
cyan(url.replace(/:(\d+)\//, (_, port) => `:${bold(port)}/`))

0 commit comments

Comments
 (0)