createCachedResolver
Creates a lazy, cached resolver: resolve(key) computes and caches
compute(key) the first time a given key is seen, and returns the cached
value on every later call for that same key — until clear() wipes the
whole cache.
Backed by a Map by default (works with any key type). Pass a factory
that returns a WeakMap instead when keys (objects, functions, or
symbols — see isWeakMapKey) should be allowed to be
garbage-collected once nothing else references them anymore — the whole
point of WeakMap over Map.
A factory function is accepted (not a cache instance) so clear() can
reliably produce a fresh, empty cache regardless of its kind: WeakMap
deliberately has no .clear() method (no way to enumerate its entries, by
design), so the only way to empty one is to replace it outright — the same
factory used to create the initial cache is called again to do that.
Available since v3.0.7
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
compute | function | Computes the value for a key not yet in the cache. |
createCache | function | Produces a fresh, empty cache. Defaults to `() => new Map()`. Its *initial* call must return an instance not already claimed by another `createCachedResolver()` call sharing the same factory — otherwise the two would silently share cache state, so this throws instead. |
Returns
Section titled “Returns”CachedResolver<K, V> — A CachedResolver.
Examples
Section titled “Examples”Compute once, reuse on every later call for the same key
Section titled “Compute once, reuse on every later call for the same key”compute() only runs on a cache miss — a repeated call for the same key returns the cached value.
clear() wipes the cache — the next resolve() recomputes
Section titled “clear() wipes the cache — the next resolve() recomputes”Useful for a “refresh” action that must pick up changes since the cache was last filled.
WeakMap-backed cache for object keys
Section titled “WeakMap-backed cache for object keys”Pass a factory returning a WeakMap so entries can be garbage-collected once the key object is no longer referenced elsewhere.
Related Types
Section titled “Related Types”CachedResolver
Section titled “CachedResolver”A cached resolver created by createCachedResolver.
