@mocanvas/store · function

createComputedCache

declare function createComputedCache<Context, R extends UnknownRecord, Result>(name: string, derive: (context: Context, record: R) => Result, options?: CreateComputedCacheOpts<Result, R>): ComputedCache<Context, R, Result>

Declare a per-record memo, keyed by record id, recomputed only when that record changes.

``ts const bindingsCache = createComputedCache("connection bindings", (editor: Editor, shape: TLShape) => editor.getBindingsFromShape(shape.id, "connection"), ) bindingsCache.get(editor, shapeId) ``

SEMANTICS-ASSUMED: Context is unconstrained rather than bound to ComputedCacheContext. The consumer annotates the derivation's own parameter ((editor: Editor, shape: TLShape) => …) and that is what get must accept; constraining the type parameter as well would force every caller to prove Editor is structurally a store holder at each call site for no added safety. The store is resolved at get time instead, and a context that carries none throws immediately rather than silently returning undefined.

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.