Skip to content

Commit 0a9f582

Browse files
committed
feat: Key file memoization cache on path and args
1 parent 46dcda7 commit 0a9f582

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/utils/memoize.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type WithCache<T> = T & { cache: Map<string, any> }
2-
interface HasParent {
2+
interface FileLike {
3+
path: string,
34
parent: { path: string }
45
}
56

@@ -14,20 +15,21 @@ export const memoize = <T>(
1415
return cached
1516
}
1617

17-
export function filememoizeAsync<F extends HasParent, T>(
18+
export function filememoizeAsync<F extends FileLike, T>(
1819
fn: (file: F, ...args: any[]) => Promise<T>,
1920
): WithCache<(file: F, ...args: any[]) => Promise<T>> {
20-
const cache = new Map<string, Map<F, T>>()
21+
const cache = new Map<string, Map<string, T>>()
2122
const cached = async function (this: any, file: F, ...args: any[]): Promise<T> {
2223
let subcache = cache.get(file.parent.path)
2324
if (!subcache) {
2425
subcache = new Map()
2526
cache.set(file.parent.path, subcache)
2627
}
27-
let val = subcache.get(file)
28+
const key = `${file.path}:${args.join(',')}`
29+
let val = subcache.get(key)
2830
if (!val) {
2931
val = await fn.call(this, file, ...args)
30-
subcache.set(file, val)
32+
subcache.set(key, val)
3133
}
3234
return val
3335
}

0 commit comments

Comments
 (0)