Jido.Signal.Bus.State (Jido Signal v1.0.0)

View Source

Defines 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

t()

@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

add_route(state, route)

@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 state
  • route: The route to add to the router

Returns

  • {:ok, new_state} if successful
  • {:error, reason} if the route addition fails

add_subscription(state, subscription_id, subscription)

@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 state
  • subscription_id: The unique ID for the subscription
  • subscription: The subscription struct to add

Returns

  • {:ok, new_state} if successful
  • {:error, :subscription_exists} if a subscription with this ID already exists

append_signals(state, signals)

@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

clear_log(state)

@spec clear_log(t()) :: {:ok, t()}

Clears all signals from the log.

dbug(_, _ \\ [])

(macro)

error(_, _ \\ [])

(macro)

get_subscription(state, subscription_id)

@spec get_subscription(t(), String.t()) :: Jido.Signal.Bus.Subscriber.t() | nil

Retrieves a subscription from the bus state.

Parameters

  • state: The current bus state
  • subscription_id: The ID of the subscription to retrieve

Returns

The subscription struct if found, nil otherwise

has_subscription?(state, subscription_id)

@spec has_subscription?(t(), String.t()) :: boolean()

Checks if a subscription exists in the bus state.

Parameters

  • state: The current bus state
  • subscription_id: The ID of the subscription to check

Returns

true if the subscription exists, false otherwise

log_to_list(state)

@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"]

remove_route(state, route)

@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 state
  • route: 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

remove_subscription(state, subscription_id, opts \\ [])

@spec remove_subscription(t(), String.t(), keyword()) :: {:ok, t()} | {:error, atom()}

Removes a subscription from the bus state and its corresponding route.

Parameters

  • state: The current bus state
  • subscription_id: The ID of the subscription to remove
  • opts: 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

truncate_log(state, max_size)

@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.