Phoenix.SessionProcess.Action (phoenix_session_process v1.0.1)

Copy Markdown View Source

Internal action structure for fast pattern matching.

Actions are normalized to this struct internally for consistent handling and optimized pattern matching in the BEAM VM.

Fields

  • type - Action type identifier (must be a binary string)
  • payload - Action data (any term)
  • meta - Action metadata:
    • async: true - Route to handle_async/3 instead of handle_action/2
    • reducers: [:user, :cart] - List of reducer names (atoms) to target explicitly.
      • Bypasses normal prefix routing
      • Only specified reducers are called
      • Action type passed WITHOUT prefix stripping
      • Warning logged if reducer doesn't exist
    • reducer_prefix: "user" - Only call reducers with this prefix (when reducers not specified)
    • Custom metadata for middleware/logging

Usage

Actions are dispatched using the dispatch/4 function:

# Basic action
dispatch(session_id, "user.reload")

# With payload
dispatch(session_id, "user.set", %{id: 123})

# With meta (async) - note: meta is a keyword list
dispatch(session_id, "user.fetch", %{page: 1}, async: true)

# Target specific reducers (bypasses prefix routing, no prefix stripping)
dispatch(session_id, "user.reload", nil, reducers: [:user, :cart])
# → Only :user and :cart called, both receive "user.reload" unchanged

Reducers pattern match on the normalized Action struct for fast, consistent matching.

Summary

Functions

Check if action should be routed to handle_async/3.

Create an Action struct from type, payload, and meta.

Get reducer prefix filter if specified.

Get list of reducer names this action should be routed to.

Types

t()

@type t() :: %Phoenix.SessionProcess.Action{
  meta: map(),
  payload: term(),
  type: String.t()
}

Functions

async?(arg1)

@spec async?(t()) :: boolean()

Check if action should be routed to handle_async/3.

Returns true if meta contains async: true.

new(type, payload \\ nil, meta \\ %{})

@spec new(String.t(), term(), map()) :: t()

Create an Action struct from type, payload, and meta.

Type must be a binary string. Payload and meta can be any term.

Examples

iex> Action.new("user.reload", nil, %{})
%Action{type: "user.reload", payload: nil, meta: %{}}

iex> Action.new("user.set", %{id: 123}, %{})
%Action{type: "user.set", payload: %{id: 123}, meta: %{}}

iex> Action.new("fetch", %{page: 1}, %{async: true})
%Action{type: "fetch", payload: %{page: 1}, meta: %{async: true}}

reducer_prefix(arg1)

@spec reducer_prefix(t()) :: String.t() | nil

Get reducer prefix filter if specified.

Returns nil if no prefix filter. Returns string prefix if meta contains reducer_prefix: "user".

target_reducers(arg1)

@spec target_reducers(t()) :: [atom()] | nil

Get list of reducer names this action should be routed to.

Returns nil if action should be routed to all reducers. Returns list of reducer names if meta contains reducers: [...].