01 · Before and afterThe pattern
const gen = new Map<string, number>();
export function invalidate(id: string) { gen.set(id, (gen.get(id) ?? 0) + 1); cache.delete(id); notify(); }
async function load(id: string) {
const started = gen.get(id) ?? 0;
try { const data = await fetchAll(id); if ((gen.get(id) ?? 0) === started) commit(id, data); }
catch (e) { if ((gen.get(id) ?? 0) === started) markFailed(id); }
}
// subscribers: when gen changes and a view is open, call load(id) again02 · Run it on your appThe check
If a websocket event clears a cached value while a fetch for it is running, can the old response overwrite the new value?
If the honest answer is no or not sure, this is worth a look before it reaches production.