/** * @file `memoizeWeakAsync` — async memoizer keyed by an object reference via * `WeakMap`. The async counterpart of `memoizeWeak`, and the object-keyed * counterpart of `memoizeAsync`. * WHY IT IS NOT `memoizeWeak` WRAPPING AN ASYNC FUNCTION. A rejected promise * is a perfectly good return value, so `memoizeWeak` would store it and hand * the same failure to every later caller with no way to retry. This evicts * on rejection instead, so the next call recomputes. * WHY IT IS NOT `memoizeAsync`. That one is keyed by a generated string, so * it needs `maxSize` and LRU eviction to stay bounded. Here the key is the * object itself: the entry is collectable as soon as the caller drops the * key, so there is no size to cap and no eviction policy to tune. * Caching the in-flight promise is also what deduplicates concurrent callers * — two calls with the same key before the first settles share one * computation, which is why the promise is stored before it resolves rather * than after. */ /** * Memoize an async function keyed by an object reference. * * @example * import { memoizeWeakAsync } from '@socketsecurity/lib/memo/weak-async' * * const loadManifest = memoizeWeakAsync(async (pkg: Package) => { * return await readManifest(pkg) * }) * * await loadManifest(pkg) // Computed * await loadManifest(pkg) // Cached * // Concurrent calls share one read; a rejected read is not cached. * * @param fn - Async function to memoize. Takes a single object key. * * @returns Memoized version backed by a WeakMap. */ export declare function memoizeWeakAsync(fn: (key: K) => Promise | Result): (key: K) => Promise;