MCPcopy Create free account
hub / github.com/dailydotdev/apps / getAsyncCache

Function getAsyncCache

packages/shared/src/hooks/usePersistentContext.ts:4–22  ·  view source on GitHub ↗
(
  key: string,
  valueWhenCacheEmpty: T,
  validValues?: T[],
)

Source from the content-addressed store, hash-verified

2import { get as getCache, set as setCache } from 'idb-keyval';
3
4function getAsyncCache<T>(
5 key: string,
6 valueWhenCacheEmpty: T,
7 validValues?: T[],
8): Promise<T> {
9 return getCache<T>(key)
10 .then((cachedValue) => {
11 if (
12 cachedValue !== undefined &&
13 (validValues === undefined || validValues.includes(cachedValue))
14 ) {
15 return cachedValue;
16 }
17 return valueWhenCacheEmpty;
18 })
19 .catch(() => {
20 return valueWhenCacheEmpty;
21 });
22}
23
24export type UserPersistentContextType<T> = [
25 T,

Callers 1

usePersistentContextFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected