@@ -25,6 +25,10 @@ export function resetCache() {
2525
2626export function selectorToCacheKey < F extends AnyFunction > ( arguments_ : Parameters < F > , selector : ArgumentPaths < F > ) {
2727 const selectors = _ . castArray ( selector ) ;
28+ if ( selectors . length === 0 ) {
29+ return JSON . stringify ( arguments_ ) ;
30+ }
31+
2832 const values = selectors . map ( path => {
2933 const value = _ . get ( arguments_ , path ) as unknown ;
3034 if ( value === undefined ) {
@@ -43,7 +47,8 @@ export function selectorToCacheKey<F extends AnyFunction>(arguments_: Parameters
4347
4448export function cachedFunction < F extends AnyFunction > ( function_ : F , options : CachedFunctionOptions < F > ) {
4549 return async ( ...arguments_ : Parameters < F > ) : Promise < ReturnType < F > > => {
46- const cacheKey = selectorToCacheKey ( arguments_ , options . selector ) ;
50+ const selector = options . selector ?? function_ . cacheKeys ?? [ ] ;
51+ const cacheKey = selectorToCacheKey ( arguments_ , selector ) ;
4752 const cache = await getOrInitializeCache ( options as CachedFunctionInitializerOptions ) ;
4853
4954 const cacheValue = await cache . get < ReturnType < F > > ( cacheKey ) ;
@@ -57,3 +62,9 @@ export function cachedFunction<F extends AnyFunction>(function_: F, options: Cac
5762 return result ;
5863 } ;
5964}
65+
66+ export function cacheKeys < F extends AnyFunction > ( function_ : F , ...selector : Array < ArgumentPaths < F > > ) {
67+ const selectors = _ ( selector ) . flatMap ( ) . value ( ) ;
68+ function_ . cacheKeys = selectors ;
69+ return function_ ;
70+ }
0 commit comments