@@ -10,7 +10,15 @@ import type {AnyFunction as CacheableFunction, ArgumentPaths} from './paths.d';
10
10
11
11
let cache : Cache | undefined ;
12
12
13
- export async function getOrInitializeCache < S extends Store > ( options ?: CachedFunctionInitializerOptions ) {
13
+ /**
14
+ * Retrieves or initializes the cache.
15
+ *
16
+ * @template S - The type of the store.
17
+ * @param {CachedFunctionInitializerOptions } [options] - The options for initializing the cache.
18
+ * @returns {Promise<Cache<S>> } - A promise that resolves to the cache.
19
+ * @throws {Error } - If the cache is not initialized and no options are provided, or if the store is not provided in the options but is required to initialize the cache.
20
+ */
21
+ export async function getOrInitializeCache < S extends Store > ( options ?: CachedFunctionInitializerOptions ) : Promise < Cache < S > > {
14
22
if ( ! cache && ! options ) {
15
23
throw new Error ( 'cache is not initialized and no options provided' ) ;
16
24
}
@@ -25,12 +33,22 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
25
33
}
26
34
27
35
/**
28
- * @deprecated To close any open connections, please retrieve the cache object from `getOrInitializeCache` and close it directly.
29
- */
36
+ * @deprecated To close any open connections, please retrieve the cache object from `getOrInitializeCache` and close it directly.
37
+ */
30
38
export function resetCache ( ) {
31
39
cache = undefined ;
32
40
}
33
41
42
+ /**
43
+ * Generates a cache key based on the provided arguments and selector.
44
+ *
45
+ * @template F - The type of the cacheable function.
46
+ * @param arguments_ - The arguments of the cacheable function.
47
+ * @param selector - The selector to determine which arguments to include in the cache key.
48
+ * @returns The cache key generated as a string.
49
+ * @throws {Error } If a path in the selector does not exist in the provided arguments.
50
+ * @throws {TypeError } If a path in the selector points to a function, which is not serializable.
51
+ */
34
52
export function selectorToCacheKey < F extends CacheableFunction > ( arguments_ : Parameters < F > , selector : ArgumentPaths < F > ) {
35
53
const selectors = _ . castArray ( selector ) ;
36
54
if ( selectors . length === 0 ) {
@@ -53,6 +71,15 @@ export function selectorToCacheKey<F extends CacheableFunction>(arguments_: Para
53
71
return JSON . stringify ( result ) ;
54
72
}
55
73
74
+ /**
75
+ * Caches the result of a function and returns the cached value if available.
76
+ * If the cached value is not available, it executes the function and caches the result for future use.
77
+ *
78
+ * @template F - The type of the function to be cached.
79
+ * @param function_ - The function to be cached.
80
+ * @param options - Optional configuration options for the cached function.
81
+ * @returns A promise that resolves to the result of the function.
82
+ */
56
83
export function cachedFunction < F extends CacheableFunction > ( function_ : F , options ?: CachedFunctionOptions < F > ) {
57
84
return async ( ...arguments_ : Parameters < F > ) : Promise < ReturnType < F > > => {
58
85
const cacheOptions = _ . merge ( { } , options ?? { } , function_ . cacheOptions ?? { } ) ;
@@ -75,11 +102,19 @@ export function cachedFunction<F extends CacheableFunction>(function_: F, option
75
102
} ;
76
103
}
77
104
78
- // eslint-disable-next-line @typescript-eslint/naming-convention
79
- export function CacheOptions < F extends CacheableFunction > (
105
+ /**
106
+ * Specify the options used when `cachedFunction` is invoked with this function.
107
+ * The decorator arguments take precedence over the options provided to `cachedFunction`.
108
+ *
109
+ * @template F - The type of the cacheable function.
110
+ * @param {ArgumentPaths<F> | CachedFunctionOptions<F> } selectorOrOptions - The selector or options for caching.
111
+ * @param {number } [ttl] - The time-to-live (TTL) for the cache.
112
+ * @returns {any } - The modified descriptor object.
113
+ */
114
+ export function CacheOptions < F extends CacheableFunction > ( // eslint-disable-line @typescript-eslint/naming-convention
80
115
selectorOrOptions : ArgumentPaths < F > | CachedFunctionOptions < F > ,
81
116
ttl ?: number ,
82
- ) {
117
+ ) : any {
83
118
const options = ( _ . isArrayLike ( selectorOrOptions ) || _ . isString ( selectorOrOptions ) )
84
119
? { selector : selectorOrOptions , ttl}
85
120
: selectorOrOptions as CachedFunctionOptions < F > ;
0 commit comments