Jido.Signal.Bus.State (Jido Signal v1.0.0)
View SourceDefines the state structure for the signal bus.
This module contains the type definitions and operations for managing the internal state of the signal bus, including signal logs, subscriptions, snapshots, and router configuration. It provides functions for manipulating and querying this state.
Summary
Functions
Adds a route to the router in the bus state.
Adds a subscription to the bus state and creates a corresponding route.
Merges a list of recorded signals into the existing log. Signals are added to the log keyed by their IDs. If a signal with the same ID already exists, it will be overwritten.
Clears all signals from the log.
Retrieves a subscription from the bus state.
Checks if a subscription exists in the bus state.
Converts the signal log from a map to a sorted list.
Removes a route from the router in the bus state.
Removes a subscription from the bus state and its corresponding route.
Truncates the signal log to the specified maximum size. Keeps the most recent signals and discards older ones.
Types
@type t() :: %Jido.Signal.Bus.State{ child_supervisor: pid() | nil, log: %{required(String.t()) => Jido.Signal.t()}, middleware: [Jido.Signal.Bus.MiddlewarePipeline.middleware_config()], name: atom(), router: Jido.Signal.Router.Router.t(), snapshots: %{required(String.t()) => Jido.Signal.t()}, subscriptions: %{required(String.t()) => Jido.Signal.Bus.Subscriber.t()} }
Functions
@spec add_route(t(), Jido.Signal.Router.Route.t()) :: {:ok, t()} | {:error, term()}
Adds a route to the router in the bus state.
Parameters
state
: The current bus stateroute
: The route to add to the router
Returns
{:ok, new_state}
if successful{:error, reason}
if the route addition fails
@spec add_subscription(t(), String.t(), Jido.Signal.Bus.Subscriber.t()) :: {:ok, t()} | {:error, atom()}
Adds a subscription to the bus state and creates a corresponding route.
Parameters
state
: The current bus statesubscription_id
: The unique ID for the subscriptionsubscription
: The subscription struct to add
Returns
{:ok, new_state}
if successful{:error, :subscription_exists}
if a subscription with this ID already exists
@spec append_signals(t(), [Jido.Signal.t() | {:ok, Jido.Signal.t()} | map()]) :: {:ok, t(), [Jido.Signal.t()]} | {:error, term()}
Merges a list of recorded signals into the existing log. Signals are added to the log keyed by their IDs. If a signal with the same ID already exists, it will be overwritten.
Parameters
- state: The current bus state
- signals: List of recorded signals to merge
Returns
{:ok, new_state, recorded_signals}
with signals merged into log{:error, reason}
if there was an error processing the signals
Clears all signals from the log.
@spec get_subscription(t(), String.t()) :: Jido.Signal.Bus.Subscriber.t() | nil
Retrieves a subscription from the bus state.
Parameters
state
: The current bus statesubscription_id
: The ID of the subscription to retrieve
Returns
The subscription struct if found, nil
otherwise
Checks if a subscription exists in the bus state.
Parameters
state
: The current bus statesubscription_id
: The ID of the subscription to check
Returns
true
if the subscription exists, false
otherwise
@spec log_to_list(t()) :: [Jido.Signal.t()]
Converts the signal log from a map to a sorted list.
Parameters
state
: The current bus state
Returns
A list of signals sorted by their IDs
Examples
iex> state = %Jido.Signal.Bus.State{log: %{"2" => signal2, "1" => signal1}}
iex> signals = Jido.Signal.Bus.State.log_to_list(state)
iex> length(signals)
2
iex> Enum.map(signals, & &1.id)
["1", "2"]
@spec remove_route(t(), Jido.Signal.Router.Route.t() | String.t()) :: {:ok, t()} | {:error, atom()}
Removes a route from the router in the bus state.
Parameters
state
: The current bus stateroute
: The route to remove (can be a Route struct or path string)
Returns
{:ok, new_state}
if successful{:error, :route_not_found}
if the route doesn't exist
Removes a subscription from the bus state and its corresponding route.
Parameters
state
: The current bus statesubscription_id
: The ID of the subscription to removeopts
: Options including::delete_persistence
- Whether to delete persistence (default: true)
Returns
{:ok, new_state}
if successful{:error, :subscription_not_found}
if the subscription doesn't exist
@spec truncate_log(t(), non_neg_integer()) :: {:ok, t()}
Truncates the signal log to the specified maximum size. Keeps the most recent signals and discards older ones.