PhoenixDatastar.Signals (PhoenixDatastar v0.1.14)

Copy Markdown View Source

Functions for reading and patching Datastar signals via SSE.

signals = PhoenixDatastar.Signals.read(conn)
sse |> patch(%{count: 42, message: "Hello"})
sse |> patch(%{count: 42}, only_if_missing: true)

Summary

Functions

Patches signals on the client by sending an SSE event.

Patches signals using a raw JSON string.

Reads signals from a Plug connection.

Functions

patch(sse, signals, opts \\ [])

Patches signals on the client by sending an SSE event.

Options

  • :only_if_missing - Only patch signals that don't exist on the client (default: false)
  • :event_id - Event ID for client tracking
  • :retry - Retry duration in milliseconds

Example

sse
|> PhoenixDatastar.Signals.patch(%{count: 42})
|> PhoenixDatastar.Signals.patch(%{message: "Hello"}, only_if_missing: true)

patch_raw(sse, json, opts \\ [])

Patches signals using a raw JSON string.

Example

sse
|> PhoenixDatastar.Signals.patch_raw(~s({"count": 42}))

read(conn)

@spec read(Plug.Conn.t()) :: map()

Reads signals from a Plug connection.

For GET requests, reads from query parameters under the "datastar" key. For other methods, reads from the JSON request body.

Returns a map of signals or an empty map if no signals are present.

Example

signals = PhoenixDatastar.Signals.read(conn)
# => %{"count" => 10, "message" => "Hello"}