1+ import type { RpcCacheOptions } from '@vitejs/devtools-rpc'
12import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client'
23import type { BirpcOptions , BirpcReturn } from 'birpc'
34import type { ConnectionMeta , DevToolsRpcClientFunctions , DevToolsRpcServerFunctions , EventEmitter , RpcSharedStateHost } from '../types'
45import type { DevToolsClientRpcHost , DevToolsRpcContext , RpcClientEvents } from './docks'
5- import { RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
6+ import { RpcCacheManager , RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
67import {
78 DEVTOOLS_CONNECTION_META_FILENAME ,
89 DEVTOOLS_MOUNT_PATH ,
@@ -25,6 +26,7 @@ export interface DevToolsRpcClientOptions {
2526 authToken ?: string
2627 wsOptions ?: Partial < WebSocketRpcClientOptions >
2728 rpcOptions ?: Partial < BirpcOptions < DevToolsRpcServerFunctions , DevToolsRpcClientFunctions , boolean > >
29+ cacheOptions ?: boolean | Partial < RpcCacheOptions >
2830}
2931
3032export type DevToolsRpcClientCall = BirpcReturn < DevToolsRpcServerFunctions , DevToolsRpcClientFunctions > [ '$call' ]
@@ -86,6 +88,10 @@ export interface DevToolsRpcClient {
8688 * The shared state host
8789 */
8890 sharedState : RpcSharedStateHost
91+ /**
92+ * The RPC cache manager
93+ */
94+ cacheManager : RpcCacheManager
8995}
9096
9197export interface DevToolsRpcClientMode {
@@ -149,6 +155,7 @@ export async function getDevToolsRpcClient(
149155 const {
150156 baseURL = DEVTOOLS_MOUNT_PATH ,
151157 rpcOptions = { } ,
158+ cacheOptions = false ,
152159 } = options
153160 const events = createEventEmitter < RpcClientEvents > ( )
154161 const bases = Array . isArray ( baseURL ) ? baseURL : [ baseURL ]
@@ -188,6 +195,7 @@ export async function getDevToolsRpcClient(
188195 }
189196 }
190197
198+ const cacheManager = new RpcCacheManager ( { functions : [ ] , ...( typeof options . cacheOptions === 'object' ? options . cacheOptions : { } ) } )
191199 const context : DevToolsRpcContext = {
192200 rpc : undefined ! ,
193201 }
@@ -229,7 +237,25 @@ export async function getDevToolsRpcClient(
229237 connectionMeta,
230238 events,
231239 clientRpc,
232- rpcOptions,
240+ rpcOptions : {
241+ ...rpcOptions ,
242+ async onRequest ( req , next , resolve ) {
243+ await rpcOptions . onRequest ?. call ( this , req , next , resolve )
244+ if ( cacheOptions && cacheManager ?. validate ( req . m ) ) {
245+ const cached = cacheManager . cached ( req . m , req . a )
246+ if ( cached ) {
247+ return resolve ( cached )
248+ }
249+ else {
250+ const res = await next ( req )
251+ cacheManager ?. apply ( req , res )
252+ }
253+ }
254+ else {
255+ await next ( req )
256+ }
257+ } ,
258+ } ,
233259 wsOptions : options . wsOptions ,
234260 } )
235261
@@ -252,6 +278,7 @@ export async function getDevToolsRpcClient(
252278 callOptional : mode . callOptional ,
253279 client : clientRpc ,
254280 sharedState : undefined ! ,
281+ cacheManager,
255282 }
256283
257284 rpc . sharedState = createRpcSharedStateClientHost ( rpc )
0 commit comments