UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

28 lines 847 B
import { Trie } from "@wry/trie"; import { AutoCleanedWeakCache } from "./caches.js"; /** * Naive alternative to `wrap` without any dependency tracking, potentially avoiding resulting memory leaks. */ export function memoize(fn, { max }) { const keys = new Trie(true); const cache = new AutoCleanedWeakCache(max); return (...args) => { const cacheKey = keys.lookupArray(args); const cached = cache.get(cacheKey); if (cached) { if (cached.error) { throw cached.error; } return cached.result; } const entry = cache.set(cacheKey, {}); try { return (entry.result = fn(...args)); } catch (error) { entry.error = error; throw error; } }; } //# sourceMappingURL=memoize.js.map