/** Share only concurrent work. Completed values and failures are never cached. */ export function singleFlight(load: (key: Key) => PromiseLike) { const pending = new Map>(); return (key: Key): Promise => { const existing = pending.get(key); if (existing) return existing; const request = Promise.resolve().then(() => load(key)).finally(() => { if (pending.get(key) === request) pending.delete(key); }); pending.set(key, request); return request; }; }