Fix load bypassing freshness

This commit is contained in:
Jon Staab
2024-09-02 15:57:40 -07:00
parent 9a5a423b3e
commit b910569fc0
8 changed files with 80 additions and 61 deletions
+6 -3
View File
@@ -19,8 +19,11 @@ export const collection = <T, LoadArgs extends any[]>({
const pending = new Map<string, Promise<Maybe<T>>>()
const loadItem = async (key: string, ...args: LoadArgs) => {
if (getFreshness(name, key) > now() - 3600) {
return indexStore.get().get(key)
const item = indexStore.get().get(key)
const delta = item ? 3600 : 300
if (getFreshness(name, key) > now() - delta) {
return item
}
if (pending.has(key)) {
@@ -47,7 +50,7 @@ export const collection = <T, LoadArgs extends any[]>({
// If we don't yet have the item, or it's stale, trigger a request for it. The derived
// store will update when it arrives
load(key, ...args)
loadItem(key, ...args)
return derived(indexStore, $index => $index.get(key))
}