Phoenix controller helpers for StarView.
Use use StarView from your web module after
use Phoenix.Controller has been applied:
def star_view do
quote do
use Phoenix.Controller, formats: [:html, :json]
use StarView
use Phoenix.Component
use Gettext, backend: MyAppWeb.Gettext
import Phoenix.Component, except: [assign: 3]
import Plug.Conn
alias MyAppWeb.Components.StarView.Layout
plug :put_root_layout, false
unquote(verified_routes())
end
endControllers implement StarView:
@impl StarView
def handle_event("increment", signals, conn), do: ...assign/3 vs signal/3
assign/3sets a Plug connection assign. Function components can read it via@key, but it is never sent to the browser.signal/3doesassign/3and sends the value to the browser when the connection is already an SSE response. Before SSE starts, it records the value forinit_signals/1.
Summary
Functions
Extracts tracked signal keys, values, and per-signal options.
Flushes tracked pre-start signals as Datastar signal patch events.
Returns the tracked signal map as JSON for data-signals.
Patches a rendered component or HTML value against current assigns.
Assigns a value and exposes it as a Datastar signal.
Functions
@spec extract_signals(Plug.Conn.t()) :: [{atom(), term(), keyword()}]
Extracts tracked signal keys, values, and per-signal options.
@spec flush_signals(Plug.Conn.t()) :: Plug.Conn.t()
Flushes tracked pre-start signals as Datastar signal patch events.
This is retained for manual flows that call signal/4 before starting SSE.
Controllers dispatched through StarView.Dispatch patch signals immediately
and do not need a final flush.
@spec init_signals(Plug.Conn.t()) :: String.t() | nil
Returns the tracked signal map as JSON for data-signals.
@spec patch_element(Plug.Conn.t(), (map() -> term()) | term(), keyword()) :: Plug.Conn.t()
Patches a rendered component or HTML value against current assigns.
@spec signal(Plug.Conn.t(), atom(), term(), keyword()) :: Plug.Conn.t()
Assigns a value and exposes it as a Datastar signal.
Before the SSE response starts, the signal is recorded for init_signals/1.
During an SSE response, the signal is patched immediately so the browser sees
updates in the same order as the server pipeline.