Elixir SDK helpers for Datastar.
The facade functions in this module delegate to small protocol modules while keeping a pipeline-friendly Plug API:
conn
|> StarView.start()
|> StarView.patch_signals(%{count: 1})
|> StarView.patch_elements(~s(<div id="count">1</div>))Controller Behaviour
Use use StarView in your AppWeb.star_view/0 macro, then
implement callbacks with @impl StarView:
Lifecycle
mount/2— Sets up initial signals and assigns for the page load.render/1— Renders the HEEx template. The generatedLayout.app/1wrapper usesinit_signals/1to emit thedata-signalsattribute for the initial client state.handle_event/3— Called byStarView.Dispatchwhen a Datastar action fires. The dispatcher starts the SSE response before this callback, sosignal/3patches browser signals immediately.
Example
@impl StarView
def mount(conn, _params) do
conn
|> signal(:count, 0)
|> signal(:step, 1)
end
@impl StarView
def render(assigns) do
~H"""
<Layout.app conn={@conn}>
<button data-on:click={post("increment")}>+</button>
<span data-text="$count">{@count}</span>
</Layout.app>
"""
end
@impl StarView
def handle_event("increment", signals, conn) do
conn
|> signal(:count, Map.get(signals, "count", 0) + 1)
end
Summary
Functions
Provides Phoenix controller helpers.
Checks whether a chunked SSE connection still accepts writes.
Logs a value to the browser console.
Executes JavaScript in the browser by appending a script element.
Patches one or more complete HTML elements into the DOM.
Patches client-side Datastar signals using RFC 7386 JSON Merge Patch semantics.
Patches client-side Datastar signals from a pre-encoded JSON string.
Reads Datastar signals from a Plug connection.
Reads Datastar signals from a Plug connection, raising on invalid JSON.
Redirects the browser by executing a tiny client-side script.
Removes elements from the DOM by CSS selector.
Removes signals by setting one or more dot-notated signal paths to null.
Sends a raw Datastar SSE event and returns the updated connection.
Starts a Server-Sent Events response on a Plug connection.
Starts an SSE stream with per-tab deduplication.
Callbacks
@callback handle_event(String.t(), map(), Plug.Conn.t()) :: Plug.Conn.t()
@callback mount(Plug.Conn.t(), map()) :: Plug.Conn.t()
Functions
Provides Phoenix controller helpers.
Use use StarView in your AppWeb.star_view/0 macro
instead of use StarView.Controller directly:
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
end
@spec check_connection(Plug.Conn.t()) :: {:ok, Plug.Conn.t()} | {:error, Plug.Conn.t()}
Checks whether a chunked SSE connection still accepts writes.
@spec console_log(Plug.Conn.t(), term(), keyword()) :: Plug.Conn.t()
Logs a value to the browser console.
@spec execute_script(Plug.Conn.t(), String.t(), keyword()) :: Plug.Conn.t()
Executes JavaScript in the browser by appending a script element.
@spec patch_elements(Plug.Conn.t(), iodata() | tuple() | nil, keyword()) :: Plug.Conn.t()
Patches one or more complete HTML elements into the DOM.
@spec patch_signals(Plug.Conn.t(), map(), keyword()) :: Plug.Conn.t()
Patches client-side Datastar signals using RFC 7386 JSON Merge Patch semantics.
@spec patch_signals_raw(Plug.Conn.t(), String.t(), keyword()) :: Plug.Conn.t()
Patches client-side Datastar signals from a pre-encoded JSON string.
@spec read_signals(Plug.Conn.t()) :: map()
Reads Datastar signals from a Plug connection.
Returns a signal map. Raises StarView.Signals.ReadError when the
payload cannot be decoded. Plugs that need {:ok, map()} | {:error, term()}
should call StarView.Signals.read/1 instead.
@spec read_signals!(Plug.Conn.t()) :: map()
Reads Datastar signals from a Plug connection, raising on invalid JSON.
@spec redirect(Plug.Conn.t(), String.t(), keyword()) :: Plug.Conn.t()
Redirects the browser by executing a tiny client-side script.
@spec remove_elements(Plug.Conn.t(), String.t(), keyword()) :: Plug.Conn.t()
Removes elements from the DOM by CSS selector.
@spec remove_signals(Plug.Conn.t(), String.t() | [String.t()], keyword()) :: Plug.Conn.t()
Removes signals by setting one or more dot-notated signal paths to null.
@spec send(Plug.Conn.t(), String.t(), [String.t()] | String.t(), keyword()) :: Plug.Conn.t()
Sends a raw Datastar SSE event and returns the updated connection.
@spec start(Plug.Conn.t()) :: Plug.Conn.t()
Starts a Server-Sent Events response on a Plug connection.
@spec start_stream(Plug.Conn.t(), term()) :: Plug.Conn.t()
Starts an SSE stream with per-tab deduplication.
Requires StarView.StreamRegistry in your supervision tree
and a tabId signal in your root layout. See
StarView.StreamRegistry for setup.