Dstar (dstar v0.1.0)

Copy Markdown View Source

Datastar SSE helpers for Elixir/Phoenix.

A trivially small library: SSE connection management, signal patching, DOM element patching. That's it.

Quick Example

def increment(conn, _params) do
  signals = Dstar.read_signals(conn)
  count = signals["count"] || 0

  conn
  |> Dstar.start()
  |> Dstar.patch_signals(%{count: count + 1})
end

Modules

Summary

Functions

Checks if an SSE connection is still open.

Logs a message to the browser console via SSE.

Generates a @delete(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

Executes JavaScript on the client by appending a script tag via SSE.

Generates a @get(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

Generates a @patch(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

Patches a DOM element on the client via SSE.

Patches signals on the client via SSE.

Generates a @post(...) expression for Datastar attributes.

Generates a @put(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

Reads Datastar signals from the request.

Redirects the client to the given URL via JavaScript.

Removes DOM elements on the client via SSE.

Removes signals from the client by setting them to nil.

Starts an SSE connection on the given Plug conn.

Starts an SSE stream with per-tab deduplication.

Functions

check_connection(conn)

Checks if an SSE connection is still open.

Returns {:ok, conn} if open, {:error, conn} if closed. Useful in streaming loops to detect disconnections.

Example

case Dstar.check_connection(conn) do
  {:ok, conn} -> stream_loop(conn)
  {:error, _} -> :ok
end

console_log(conn, message, opts \\ [])

Logs a message to the browser console via SSE.

Example

conn |> Dstar.console_log("Debug info")

delete(module_or_name, name_or_opts \\ [])

Generates a @delete(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

delete(module, event_name, opts)

See Dstar.Actions.delete/3.

event(module_or_name, name_or_opts)

This function is deprecated. Use Dstar.post/2 (or get/put/patch/delete) instead.

See Dstar.Actions.event/2.

event(module, event_name, opts)

See Dstar.Actions.event/3.

execute_script(conn, script, opts \\ [])

Executes JavaScript on the client by appending a script tag via SSE.

Example

conn |> Dstar.execute_script("alert('Hello!')")

get(module_or_name, name_or_opts \\ [])

Generates a @get(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

get(module, event_name, opts)

See Dstar.Actions.get/3.

patch(module_or_name, name_or_opts \\ [])

Generates a @patch(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

patch(module, event_name, opts)

See Dstar.Actions.patch/3.

patch_elements(conn, html, opts)

Patches a DOM element on the client via SSE.

Requires a :selector option.

Example

conn |> Dstar.patch_elements("<span id=\"count\">42</span>", selector: "#count")

patch_signals(conn, signals, opts \\ [])

Patches signals on the client via SSE.

Example

conn |> Dstar.patch_signals(%{count: 42})

post(module_or_name, name_or_opts \\ [])

Generates a @post(...) expression for Datastar attributes.

Examples

Dstar.post(MyAppWeb.CounterHandler, "increment")
# => "@post('/ds/my_app_web-counter_handler/increment', {headers: ...})"

Dstar.post("increment")
# => "@post('/ds/' + $_dstar_module + '/increment', {headers: ...})"

post(module, event_name, opts)

See Dstar.Actions.post/3.

put(module_or_name, name_or_opts \\ [])

Generates a @put(...) expression for Datastar attributes. See Dstar.post/2 for usage — same API, different HTTP verb.

put(module, event_name, opts)

See Dstar.Actions.put/3.

read_signals(conn)

Reads Datastar signals from the request.

For GET requests, reads from query params. For POST/PUT/etc, reads from the JSON body.

Example

signals = Dstar.read_signals(conn)
count = signals["count"] || 0

redirect(conn, url, opts \\ [])

Redirects the client to the given URL via JavaScript.

Example

conn |> Dstar.redirect("/workspaces")

remove_elements(conn, selector, opts \\ [])

Removes DOM elements on the client via SSE.

Example

conn |> Dstar.remove_elements("#old-item")

remove_signals(conn, paths, opts \\ [])

Removes signals from the client by setting them to nil.

Accepts a single path string or list of dot-notated paths.

Examples

conn |> Dstar.remove_signals("user.profile.theme")
conn |> Dstar.remove_signals(["user.name", "user.email"])

start(conn)

Starts an SSE connection on the given Plug conn.

Sets content type to text/event-stream, disables caching, and initiates a chunked response. Returns the conn.

Example

conn = Dstar.start(conn)

start_stream(conn, scope_key)

Starts an SSE stream with per-tab deduplication.

Requires Dstar.Utility.StreamRegistry in your supervision tree and a tabId signal in your root layout. See Dstar.Utility.StreamRegistry module docs for setup.

Example

conn = Dstar.start_stream(conn, current_user.id)