Namespace module for Lavash's optimistic update infrastructure.
This module serves as documentation and namespace for the optimistic update system. The actual functionality is provided by submodules:
Active Submodules
Lavash.Optimistic.Macros- Provides theoptimistic_action/3macro for LiveViewsLavash.Optimistic.ActionMacro- Providesoptimistic_actionfor componentsLavash.Optimistic.Transformers.ExtractColocatedJs- Extracts generated JS to colocated filesLavash.Optimistic.Transformers.ExpandAnimatedStates- Expands animated state DSLLavash.Optimistic.Transformers.ExpandDefrx- Expands defrx function calls
How Optimistic Updates Work
When you mark state fields and derives with optimistic: true, Lavash:
- Generates JavaScript from your DSL action declarations at compile time
- Extracts to colocated files via
Lavash.Optimistic.Transformers.ExtractColocatedJs - Injects state into the page via
Lavash.LiveView.Runtime.wrap_render/3 - Updates DOM via the
LavashOptimistichook (inpriv/static/lavash_optimistic.js)
Usage Example
defmodule MyApp.CounterLive do
use Lavash.LiveView
state :count, :integer, from: :url, default: 0, optimistic: true
optimistic_action :increment, :count,
run: fn count, _params -> count + 1 end
render fn assigns ->
~H"""
<div>
<span>{@count}</span>
<button phx-click="increment">+</button>
</div>
"""
end
endThe LavashOptimistic hook automatically intercepts the button click, updates the DOM
optimistically, then confirms with the server.