fix(kilo-sessions): cache undefined values in in-flight cache

Track entry presence separately from the cached value so `undefined`
results are treated as valid cache hits within the TTL.
This commit is contained in:
Igor Šćekić
2026-02-02 13:22:19 +01:00
parent 2f1c9fe6ea
commit 9e64e10d80
@@ -1,6 +1,7 @@
type Entry<T> = {
at: number
value: T | undefined
has: boolean
inflight: Promise<T> | undefined
}
@@ -11,7 +12,8 @@ export function withInFlightCache<T>(key: string, ttlMs: number, cb: () => Promi
const existing = store.get(key) as Entry<T> | undefined
if (existing) {
if (existing.value !== undefined && now - existing.at < ttlMs) return Promise.resolve(existing.value)
// Allow caching `undefined` by tracking presence separately.
if (existing.has && now - existing.at < ttlMs) return Promise.resolve(existing.value as T)
if (existing.inflight && now - existing.at < ttlMs) return existing.inflight
}
@@ -19,16 +21,19 @@ export function withInFlightCache<T>(key: string, ttlMs: number, cb: () => Promi
? {
at: now,
value: existing.value,
has: existing.has,
inflight: undefined,
}
: {
at: now,
value: undefined,
has: false,
inflight: undefined,
}
const task = cb().then((value) => {
next.value = value
next.has = true
return value
})