Skip to content

Commit d91a348

Browse files
committed
chore: update
1 parent 76ba595 commit d91a348

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

packages/kit/src/client/rpc.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function isNumeric(str: string | number | undefined) {
1515
export interface DevToolsRpcClientOptions {
1616
connectionMeta?: ConnectionMeta
1717
baseURL?: string[]
18+
cacheResponse?: boolean
1819
wsOptions?: Partial<WebSocketRpcClientOptions>
1920
rpcOptions?: Partial<BirpcOptions<DevToolsRpcServerFunctions>>
2021
}
@@ -25,6 +26,7 @@ export interface ClientRpcReturn {
2526
connectionMeta: ConnectionMeta
2627
rpc: DevToolsRpcClient
2728
clientRpc: DevToolsClientRpcHost
29+
invalidateCache: () => void
2830
}
2931

3032
export async function getDevToolsRpcClient(
@@ -33,8 +35,10 @@ export async function getDevToolsRpcClient(
3335
const {
3436
baseURL = '/.devtools/',
3537
rpcOptions = {},
38+
cacheResponse = false,
3639
} = options
3740
const urls = Array.isArray(baseURL) ? baseURL : [baseURL]
41+
const responseCacheMap = new Map<string, unknown>()
3842
let connectionMeta: ConnectionMeta | undefined = options.connectionMeta
3943

4044
if (!connectionMeta) {
@@ -71,15 +75,42 @@ export async function getDevToolsRpcClient(
7175
url,
7276
...options.wsOptions,
7377
}),
74-
rpcOptions,
78+
rpcOptions: {
79+
...rpcOptions,
80+
onRequest: async (req, next, resolve) => {
81+
await rpcOptions.onRequest?.(req, next, resolve)
82+
if (cacheResponse) {
83+
const cacheKey = `${req.m}-${JSON.stringify(req.a)}`
84+
if (responseCacheMap.has(cacheKey)) {
85+
resolve(responseCacheMap.get(cacheKey))
86+
}
87+
else {
88+
responseCacheMap.set(cacheKey, await next(req))
89+
}
90+
}
91+
else {
92+
await next(req)
93+
}
94+
},
95+
},
7596
},
7697
)
7798
// @ts-expect-error assign to readonly property
7899
context.rpc = rpc
79100

101+
function invalidateCache(key?: string) {
102+
if (key) {
103+
responseCacheMap.delete(key)
104+
}
105+
else {
106+
responseCacheMap.clear()
107+
}
108+
}
109+
80110
return {
81111
connectionMeta,
82112
rpc,
83113
clientRpc,
114+
invalidateCache,
84115
}
85116
}

packages/vite/src/app/composables/rpc.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export const connectionState = reactive<{
1414
})
1515

1616
const rpc = shallowRef<ClientRpcReturn['rpc']>(undefined!)
17-
const responseCacheMap = new Map<string, unknown>()
1817

1918
export async function connect() {
2019
const runtimeConfig = useRuntimeConfig()
@@ -24,6 +23,7 @@ export async function connect() {
2423
'/.devtools/',
2524
runtimeConfig.app.baseURL,
2625
],
26+
cacheResponse: true,
2727
connectionMeta: runtimeConfig.app.connection,
2828
wsOptions: {
2929
onConnected: () => {
@@ -41,15 +41,6 @@ export async function connect() {
4141
connectionState.error = e
4242
console.error(`[vite-devtools] RPC error on executing "${name}":`)
4343
},
44-
onRequest: async (req, next, resolve) => {
45-
const cacheKey = `${req.m}-${JSON.stringify(req.a)}`
46-
if (responseCacheMap.has(cacheKey)) {
47-
resolve(responseCacheMap.get(cacheKey))
48-
}
49-
else {
50-
responseCacheMap.set(cacheKey, await next(req))
51-
}
52-
},
5344
},
5445
})
5546

0 commit comments

Comments
 (0)