PhoenixHtmxWs.Socket behaviour (phoenix_htmx_ws v0.1.0)

Copy Markdown View Source

Connection state and the use macro for an htmx WebSocket handler.

meta describes only the event currently being dispatched. It is reset to %{} before the transport stores the returned socket. private belongs to this library and application code must not modify it.

Summary

Functions

Merges a map or keyword list into the connection assigns.

Assigns one value for the life of this connection.

Updates an existing assign with a unary function.

Types

event_result()

@type event_result() ::
  {:noreply, t()}
  | {:reply, term(), t()}
  | {:reply, term(), options(), t()}
  | {:stop, term(), t()}

info_result()

@type info_result() ::
  {:noreply, t()}
  | {:push, term(), t()}
  | {:push, term(), options(), t()}
  | {:stop, term(), t()}

options()

@type options() :: keyword(binary())

t()

@type t() :: %PhoenixHtmxWs.Socket{
  assigns: map(),
  connect_info: map(),
  connected_at: DateTime.t(),
  endpoint: module(),
  id: binary(),
  meta: map(),
  params: %{optional(binary()) => term()},
  private: map(),
  session: map() | nil
}

Callbacks

connect(params, session, socket)

(optional)
@callback connect(params :: map(), session :: map() | nil, socket :: t()) ::
  {:ok, t()} | :error | {:error, term()}

handle_error(reason, payload, socket)

(optional)
@callback handle_error(reason :: atom(), payload :: term(), socket :: t()) ::
  event_result()

handle_event(event, params, socket)

@callback handle_event(event :: binary(), params :: map(), socket :: t()) ::
  event_result()

handle_info(message, socket)

(optional)
@callback handle_info(message :: term(), socket :: t()) :: info_result()

mount(params, socket)

(optional)
@callback mount(params :: map(), socket :: t()) :: {:ok, t()} | {:stop, term(), t()}

terminate(reason, socket)

(optional)
@callback terminate(reason :: term(), socket :: t()) :: term()

Functions

assign(socket, attrs)

@spec assign(t(), map() | keyword()) :: t()

Merges a map or keyword list into the connection assigns.

assign(socket, key, value)

@spec assign(t(), atom(), term()) :: t()

Assigns one value for the life of this connection.

update(socket, key, fun)

@spec update(t(), atom(), (term() -> term())) :: t()

Updates an existing assign with a unary function.