Lavash.Optimistic.Version (Lavash v0.4.0-rc.5)

Copy Markdown View Source

Layer-4 (optimism) version counter.

Tracks a monotonic version per LiveView socket that the client uses to reject stale DOM patches. The counter bumps on each event that may trigger an optimistic update; the rendered HTML carries the current version, and Lavash.Assigns.project/2 exposes it to child components as @__lavash_parent_version__ so they can cross-check parent identity when reconciling bindings.

Storage

The value lives in socket.private[:lavash].optimistic_version for historical compatibility — Lavash.Socket.init/2 still declares the key. This module owns the read/write API, which is the layering concern; the storage location is a refactor for another day.

Why this is its own module

Per the architecture audit (docs/ARCHITECTURE.md punchlist item #1), the version counter is purely a layer-4 (optimism) concept. Stale-patch rejection only makes sense when there are client-side optimistic patches in flight. Keeping the API in Lavash.Socket (layer 2) meant the layer-2 surface advertised an optimism primitive even when no optimistic state existed in the module.

Summary

Functions

Bumps the optimistic version counter.

Returns the socket's current optimistic version (0 if unset).

Projects the version into assigns as @__lavash_parent_version__.

Functions

bump(socket)

Bumps the optimistic version counter.

Called when processing an event that triggers optimistic updates. The version is included in the rendered HTML and compared by the client to detect and reject stale DOM patches.

get(socket)

Returns the socket's current optimistic version (0 if unset).

project(socket)

Projects the version into assigns as @__lavash_parent_version__.

Child components compare this against the parent identity they saw at mount; on mismatch they re-hydrate optimistic state from the parent's authoritative values. Called from Lavash.Assigns.project/2.