Skip to content

Commit 1777538

Browse files
committed
feat: created cacheKeys
1 parent 5ce3807 commit 1777538

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export type CachedFunctionInitializerOptions =
1010
{store: Store};
1111

1212
export type CachedFunctionOptions<F extends AnyFunction> = Partial<CachedFunctionInitializerOptions> & {
13-
selector: ArgumentPaths<F>;
13+
selector?: ArgumentPaths<F>;
1414
ttl?: number;
1515
};

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function resetCache() {
2525

2626
export 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

4448
export 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+
}

src/paths.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export type SingleOrArray<T> = T | T[];
1313
/**
1414
* Represents any function that takes any number of arguments and returns any value.
1515
*/
16-
export type AnyFunction = (...arguments_: any[]) => any;
16+
export type AnyFunction = ((...arguments_: any[]) => any) & {
17+
cacheKeys?: string[];
18+
};
1719

1820
/**
1921
* Represents the type for generating paths of a given object.

tests/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('selectorToCacheKey', () => {
9494
const selector: any = [];
9595
const key = selectorToCacheKey(arguments_, selector);
9696

97-
expect(key).toBe('{}');
97+
expect(key).toBe('[]');
9898
});
9999

100100
it('should throw an error for non-existent paths', () => {
@@ -153,7 +153,7 @@ describe('selectorToCacheKey', () => {
153153
const selector: any = [];
154154
const key = selectorToCacheKey(arguments_, selector);
155155

156-
expect(key).toBe('{}');
156+
expect(key).toBe(JSON.stringify(arguments_));
157157
});
158158

159159
it('should handle paths that skip levels correctly', () => {

0 commit comments

Comments
 (0)