import * as recordCore from '@okikio/record'; const requestCaches = new WeakMap>(); /** Compute one request-owned capability once, sharing a pending promise across consumers. */ export async function memoize( request: object, key: unknown, load: () => Value | Promise, options: { readonly cacheRejected?: boolean } = {}, ): Promise { recordCore.assert(options, 'memoize options'); if (options.cacheRejected !== undefined && typeof options.cacheRejected !== 'boolean') { throw new TypeError('cacheRejected must be a boolean when provided.'); } const cacheRejected = options.cacheRejected ?? false; let cache = requestCaches.get(request); if (!cache) { cache = new Map(); requestCaches.set(request, cache); } if (cache.has(key)) return await cache.get(key) as Value; const pending = Promise.resolve().then(load); cache.set(key, pending); try { const value = await pending; cache.set(key, value); return value; } catch (error) { if (!cacheRejected) cache.delete(key); throw error; } } /** Invalidate one memoized request capability or the complete request cache. */ export function invalidate(request: object, key?: unknown): void { if (key === undefined) requestCaches.delete(request); else requestCaches.get(request)?.delete(key); } /** Dispose memoized values exposing native disposal protocols, then clear the request cache. */ export async function dispose(request: object): Promise { const cache = requestCaches.get(request); requestCaches.delete(request); if (!cache) return; const values = new Set(cache.values()); for (const value of [...values].reverse()) { const resolved = await value; if (resolved && typeof resolved === 'object') { const asyncDispose = (resolved as { [Symbol.asyncDispose]?: () => PromiseLike })[Symbol.asyncDispose]; if (asyncDispose) await asyncDispose.call(resolved); else (resolved as { [Symbol.dispose]?: () => void })[Symbol.dispose]?.call(resolved); } } }