Lavash.Reactive.GraphMacro (Lavash v0.3.0-rc.5)

Copy Markdown View Source

Macro for defining reactive graphs with client-side JS generation.

defgraph provides a declarative block syntax that:

  1. Defines a reactive graph (states + derives) for server-side use
  2. Transpiles rx() expressions to JavaScript for client-side optimistic updates
  3. Generates a colocated JS hook that Phoenix bundles automatically

Usage

defmodule MyAppWeb.CounterLive do
  use Phoenix.LiveView
  import Lavash.Rx
  import Lavash.Reactive.GraphMacro

  defgraph do
    state :count, 0
    state :step, 1
    calculate :doubled, rx(@count * @step)
    calculate :quad, rx(@doubled * 2)
  end

  def mount(_params, _session, socket) do
    {:ok, Lavash.Reactive.init(socket, __reactive_graph__())}
  end
end

What it generates

  • __reactive_graph__/0 — returns a cached %Lavash.Rx.Graph{} (via persistent_term)
  • __phoenix_macro_components__/0 — registers the colocated JS hook with Phoenix
  • A colocated JS file with compute functions and dependency graph metadata

Async calculations

Calculations marked async: true are server-only — they appear in the graph metadata but have no JS compute function. The client skips them during recomputation.

calculate :results, rx(search(@query)), async: true

Summary

Functions

defgraph(list)

(macro)