( key: string, valueWhenCacheEmpty: T, validValues?: T[], )
| 2 | import { get as getCache, set as setCache } from 'idb-keyval'; |
| 3 | |
| 4 | function 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 | |
| 24 | export type UserPersistentContextType<T> = [ |
| 25 | T, |
no outgoing calls
no test coverage detected