Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update assets in real time #212

Merged
merged 2 commits into from
Feb 1, 2024
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
11 changes: 10 additions & 1 deletion packages/client/src/pages/assets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ const byTree = computed(() => {
return root.children
})

onDevToolsClientConnected(() => {
let cleanupAssetsUpdatedEffect: Function

function fetchAssets() {
bridgeRpc.getStaticAssets().then((res) => {
assets.value = res
})
}

onDevToolsClientConnected(() => {
fetchAssets()
cleanupAssetsUpdatedEffect = bridgeRpc.assetsUpdated(() => {
fetchAssets()
})
})
function toggleView() {
view.value = view.value === 'list' ? 'grid' : 'list'
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/bridge/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const devtoolsBridge: {
api: ReturnType<typeof setupViteRPCClient>
on: {
moduleUpdated: (fn: Function) => void
assetsUpdated: (fn: Function) => void
}
off: {
moduleUpdated: () => void
assetsUpdated: () => void
}
}
rpc: InstanceType<typeof BridgeRpcCore>
Expand All @@ -27,9 +29,11 @@ const devtoolsBridge: {
api: null!,
on: {
moduleUpdated() {},
assetsUpdated() {},
},
off: {
moduleUpdated() {},
assetsUpdated() {},
},
},
rpc: null!,
Expand All @@ -44,10 +48,15 @@ export function registerBridgeRpc(options: BridgeRpcOptions) {
devtoolsBridge.rpc = new BridgeRpcCore(options.bridge)

const moduleUpdatedFn: Function[] = []
const assetsUpdatedFn: Function[] = []

const rpc = setupViteRPCClient(options.viteRPCContext, {
moduleUpdated: () => {
moduleUpdatedFn.forEach(fn => fn())
},
assetsUpdated: () => {
assetsUpdatedFn.forEach(fn => fn())
},
})

if (rpc) {
Expand All @@ -58,11 +67,17 @@ export function registerBridgeRpc(options: BridgeRpcOptions) {
moduleUpdated(fn: Function) {
moduleUpdatedFn.push(fn)
},
assetsUpdated(fn) {
assetsUpdatedFn.push(fn)
},
},
off: {
moduleUpdated() {
moduleUpdatedFn.length = 0
},
assetsUpdated() {
assetsUpdatedFn.length = 0
},
},
}
}
Expand Down Expand Up @@ -211,4 +226,10 @@ export class BridgeRpc {
devtoolsBridge.viteRpc!.on.moduleUpdated(fn)
return () => devtoolsBridge.viteRpc!.off.moduleUpdated()
}

// assets updated
static assetsUpdated(fn: Function) {
devtoolsBridge.viteRpc!.on.assetsUpdated(fn)
return () => devtoolsBridge.viteRpc!.off.assetsUpdated()
}
}
22 changes: 22 additions & 0 deletions packages/core/src/vite-rpc/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import fsp from 'node:fs/promises'
import fg from 'fast-glob'
import { join, resolve } from 'pathe'
import { imageMeta } from 'image-meta'
import { BirpcGroupReturn } from 'birpc'
import type { ViteDevServer } from 'vite'
LoTwT marked this conversation as resolved.
Show resolved Hide resolved
import { debounce } from 'perfect-debounce'
import type { AssetInfo, AssetType, ImageMeta, ViteRPCFunctions } from './types'

const defaultAllowedExtensions = [
Expand Down Expand Up @@ -52,9 +55,14 @@ function guessType(path: string): AssetType {
interface SetupAssetsOptions {
root: string
base: string
server: any
getRpcServer: () => BirpcGroupReturn<ViteRPCFunctions>
}

export function setupAssetsRPC(config: SetupAssetsOptions) {
const server = config.server as ViteDevServer
const getRpcServer = config.getRpcServer

const _imageMetaCache = new Map<string, ImageMeta | undefined>()
let cache: AssetInfo[] | null = null

Expand Down Expand Up @@ -103,6 +111,19 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
return cache
}

function watchAssets() {
const debouncedAssetsUpdated = debounce(() => {
getRpcServer().assetsUpdated()
}, 100)

server.watcher.on('all', (event) => {
if (event !== 'change')
debouncedAssetsUpdated()
})
}

watchAssets()

return {
async getStaticAssets() {
return await scan()
Expand Down Expand Up @@ -131,5 +152,6 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
return undefined
}
},
assetsUpdated() {},
} satisfies Partial<ViteRPCFunctions>
}
4 changes: 3 additions & 1 deletion packages/core/src/vite-rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import type { ViteRPCFunctions } from './types'

export interface SetupViteRPCClientOptions {
moduleUpdated?: () => void
assetsUpdated?: () => void
}
export function setupViteRPCClient(ctx: ViteHotContext | undefined, options: SetupViteRPCClientOptions = {}): BirpcReturn<ViteRPCFunctions, unknown> {
if (!ctx)
return null!

const { moduleUpdated = () => {} } = options
const { moduleUpdated = () => {}, assetsUpdated = () => {} } = options
const rpcClient = createRPCClient<ViteRPCFunctions>('vite-plugin-vue-devtools', ctx, {
moduleUpdated,
assetsUpdated,
}, {
timeout: -1,
})
Expand Down
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 @@ -45,4 +45,5 @@ export interface ViteRPCFunctions {
getImageMeta(filepath: string): Promise<ImageMeta | undefined>
getTextAssetContent(filepath: string, limit?: number): Promise<string | undefined>
moduleUpdated: () => void
assetsUpdated: () => void
}
8 changes: 7 additions & 1 deletion packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@ export default function VitePluginVueDevTools(options?: VitePluginVueDevToolsOpt
...setupAssetsRPC({
root: config.root,
base,
server,
getRpcServer,
}),
...setupGraphRPC({
rpc: inspect.api.rpc,
server,
getRpcServer: () => rpcServer,
getRpcServer,
}),
})

function getRpcServer() {
return rpcServer
}

const _printUrls = server.printUrls
const colorUrl = (url: string) =>
cyan(url.replace(/:(\d+)\//, (_, port) => `:${bold(port)}/`))
Expand Down
Loading