Skip to content

Commit 9c37dcd

Browse files
committed
feat: Refactor getOrInitializeCache to use async/await syntax
1 parent 6bb0fe2 commit 9c37dcd

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/index.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import type {AnyFunction as CacheableFunction, ArgumentPaths} from './paths.d';
1010

1111
let cache: Cache | undefined;
1212

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>> {
1422
if (!cache && !options) {
1523
throw new Error('cache is not initialized and no options provided');
1624
}
@@ -25,12 +33,22 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
2533
}
2634

2735
/**
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+
*/
3038
export function resetCache() {
3139
cache = undefined;
3240
}
3341

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+
*/
3452
export function selectorToCacheKey<F extends CacheableFunction>(arguments_: Parameters<F>, selector: ArgumentPaths<F>) {
3553
const selectors = _.castArray(selector);
3654
if (selectors.length === 0) {
@@ -53,6 +71,15 @@ export function selectorToCacheKey<F extends CacheableFunction>(arguments_: Para
5371
return JSON.stringify(result);
5472
}
5573

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+
*/
5683
export function cachedFunction<F extends CacheableFunction>(function_: F, options?: CachedFunctionOptions<F>) {
5784
return async (...arguments_: Parameters<F>): Promise<ReturnType<F>> => {
5885
const cacheOptions = _.merge({}, options ?? {}, function_.cacheOptions ?? {});
@@ -75,11 +102,19 @@ export function cachedFunction<F extends CacheableFunction>(function_: F, option
75102
};
76103
}
77104

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
80115
selectorOrOptions: ArgumentPaths<F> | CachedFunctionOptions<F>,
81116
ttl?: number,
82-
) {
117+
): any {
83118
const options = (_.isArrayLike(selectorOrOptions) || _.isString(selectorOrOptions))
84119
? {selector: selectorOrOptions, ttl}
85120
: selectorOrOptions as CachedFunctionOptions<F>;

0 commit comments

Comments
 (0)