createLayoutCache
function createLayoutCache(params?): LayoutCache;Defined in: src/layout/layoutCache.ts:217
Create an opt-in layout cache.
Pass the result to a render function to memoise dagre layout across calls that do not change geometry, such as switching between themes that share a font size. On a large definition the layout dominates render cost, so a hit turns a re-render into little more than SVG serialisation.
The cache is caller-owned and bounded. Nothing is cached unless a cache is passed,
which keeps the core stateless by default: no global state to make tests
order-dependent, and no unbounded growth in a long-lived host such as the VS Code
extension, which should clear() when a document closes.
Parameters
Section titled “Parameters”params?
Section titled “params?”Optional maxEntries bound
Returns
Section titled “Returns”A cache to hand to a render function
Example
Section titled “Example”import { createLayoutCache } from 'sfn-diagram';import { generateViewerUpdate } from 'sfn-diagram/html';
const cache = createLayoutCache();// Second call reuses the layout: only the theme changed.generateViewerUpdate({ aslDefinition, cache, theme: 'light' });generateViewerUpdate({ aslDefinition, cache, theme: 'dark' });cache.stats(); // { entries: 1, hits: 1, misses: 1 }Those counts are for a definition with no Parallel or Map. One with a container
renders an expanded and a collapsed view per call, so it takes two entries and the
same two calls report { entries: 2, hits: 2, misses: 2 } - worth knowing when
sizing maxEntries, since containers are what the cache is most aimed at.
Remarks
Section titled “Remarks”Keys are the serialised graph and options, not a digest. A 32-bit digest would make
a collision return someone else’s layout — a silently wrong diagram, the one failure
mode no test announces — and an exact key removes that class of bug for a cost
bounded by maxEntries.