Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/cache.ts
3557 views
1
/*
2
* cache.ts
3
*
4
* provides a simple cache for expensive operations
5
*
6
* Copyright (C) 2023 Posit Software, PBC
7
*/
8
9
export function cache<T>(f: () => Promise<T>): () => Promise<T> {
10
let value: T | undefined = undefined;
11
return async () => {
12
if (value === undefined) {
13
value = await f();
14
}
15
return value;
16
};
17
}
18
19
export function cacheMap<K, V>(
20
f: (key: K) => Promise<V>,
21
): (key: K) => Promise<V> {
22
const map = new Map<K, V>();
23
return async (key: K) => {
24
if (!map.has(key)) {
25
map.set(key, await f(key));
26
}
27
return map.get(key)!;
28
};
29
}
30
31