@@ -159,6 +159,55 @@ export const generateCacheKey = async (
159
159
return h64ToString ( serialized ) ;
160
160
} ;
161
161
162
+ /**
163
+ * Configuration options for the persistent caching utility.
164
+ *
165
+ * @template TResult - Result type returned by the generator function.
166
+ */
167
+ interface BaseCacheConfig < TResult > {
168
+ /**
169
+ * Keys to exclude when generating a stable cache key from input arguments.
170
+ */
171
+ ignoreKeys ?: string [ ] ;
172
+
173
+ /**
174
+ * In-memory cache object, useful for cache sharing across plugins/tabs.
175
+ */
176
+ cache ?: Record < string , Promise < TResult > > ;
177
+ }
178
+
179
+ interface MemoryOnlyCacheConfig < TResult > extends BaseCacheConfig < TResult > {
180
+ /**
181
+ * Store only in RAM (fast, temporary). No IndexedDB fallback.
182
+ */
183
+ cacheMode : "memory" ;
184
+
185
+ /**
186
+ * No parallel computation needed since memory cache is synchronous.
187
+ */
188
+ parallel ?: never ;
189
+ }
190
+
191
+ interface IdbOrBothCacheConfig < TResult > extends BaseCacheConfig < TResult > {
192
+ /**
193
+ * Store in IndexedDB, or both memory and IndexedDB.
194
+ */
195
+ cacheMode : "idb" | "both" ;
196
+
197
+ /**
198
+ * If true, read from cache and compute in parallel.
199
+ * Defaults to true.
200
+ */
201
+ parallel ?: boolean ;
202
+ }
203
+
204
+ /**
205
+ * Type-safe configuration for createPersistentCache utility.
206
+ */
207
+ export type CacheConfigType < TResult > =
208
+ | MemoryOnlyCacheConfig < TResult >
209
+ | IdbOrBothCacheConfig < TResult > ;
210
+
162
211
/**
163
212
* Creates a cached version of an async function with memory and/or persistent caching capabilities.
164
213
*
@@ -191,35 +240,30 @@ export const generateCacheKey = async (
191
240
* const fetchWithCache = createPersistentCache(fetchJson, "remote-data");
192
241
* await fetchWithCache("https://example.com/api/data");
193
242
*/
194
- export const createPersistentCache = < Args extends unknown [ ] , Result > (
195
- generator : ( ...args : Args ) => Promise < Result > ,
243
+ export const createPersistentCache = < TArgs extends unknown [ ] , TResult > (
244
+ generator : ( ...args : TArgs ) => Promise < TResult > ,
196
245
namespace : string ,
197
- config ?: {
198
- ignoreKeys ?: string [ ] ;
199
- cache ?: Record < string , Promise < Result > > ;
200
- cacheTarget ?: "idb" | "memory" | "both" ;
201
- parallel ?: boolean ;
202
- } ,
203
- ) : ( ( ...args : Args ) => Promise < Result > ) => {
246
+ config ?: CacheConfigType < TResult > ,
247
+ ) : ( ( ...args : TArgs ) => Promise < TResult > ) => {
204
248
const {
205
249
ignoreKeys = [ ] ,
206
- cache = defaultCache as Record < string , Promise < Result > > ,
207
- cacheTarget = "both" ,
250
+ cache = defaultCache as Record < string , Promise < TResult > > ,
251
+ cacheMode = "both" ,
208
252
parallel = true ,
209
253
} = config ?? { } ;
210
254
211
- return async ( ...args : Args ) : Promise < Result > => {
255
+ return async ( ...args : TArgs ) : Promise < TResult > => {
212
256
const cacheKey = await generateCacheKey ( ignoreKeys , ...args ) ;
213
257
214
- if ( cacheTarget === "memory" ) return ( cache [ cacheKey ] ??= generator ( ...args ) ) ;
258
+ if ( cacheMode === "memory" ) return ( cache [ cacheKey ] ??= generator ( ...args ) ) ;
215
259
216
260
const resultPromise = ( async ( ) => {
217
261
const result = parallel
218
262
? await Promise . any ( [
219
- readFromCache < Result > ( cacheKey ) . then ( result => result ?? Promise . reject ( ) ) ,
263
+ readFromCache < TResult > ( cacheKey ) . then ( result => result ?? Promise . reject ( ) ) ,
220
264
generator ( ...args ) ,
221
265
] )
222
- : ( ( await readFromCache < Result > ( cacheKey ) ) ?? ( await generator ( ...args ) ) ) ;
266
+ : ( ( await readFromCache < TResult > ( cacheKey ) ) ?? ( await generator ( ...args ) ) ) ;
223
267
224
268
const resultsToCache = { id : cacheKey , namespace } as {
225
269
id : string ;
@@ -234,7 +278,7 @@ export const createPersistentCache = <Args extends unknown[], Result>(
234
278
return result ;
235
279
} ) ( ) ;
236
280
237
- if ( cacheTarget === "both" ) cache [ cacheKey ] = resultPromise ;
281
+ if ( cacheMode === "both" ) cache [ cacheKey ] = resultPromise ;
238
282
239
283
return resultPromise ;
240
284
} ;
0 commit comments