Raxol.Core.StateManager (Raxol v2.3.0)

View Source

Consolidated state management module providing functional, process-based, and ETS-backed state handling.

This module provides multiple state management strategies with automatic selection:

  • Functional: Simple map-based transformations (no processes)
  • Process-based: Supervised GenServer state with Agent or GenServer backing
  • ETS-backed: High-performance state with ETS storage for large datasets
  • Domain-specific: Delegation to specialized domain managers

Configuration

Set the default strategy in application config:

config :raxol, :state_manager,
  default_strategy: :functional,  # :functional, :process, :ets
  ets_enabled: true,
  process_supervision: true

Or control per-call with options:

StateManager.put(state, :key, value, strategy: :ets)
StateManager.start_managed(:app_state, %{}, strategy: :process)

Summary

Functions

Creates a supervised state manager as part of a supervision tree.

Cleans up state resources.

Clears functional state.

Delegates to domain-specific state manager.

Deletes a key from functional state.

Deletes a state value.

Gets a value from functional state.

Gets a value from functional state with default.

Gets the current managed state.

Gets memory usage statistics.

Gets the current state or a specific key from ETS. When called without arguments, returns the entire state as a map.

Gets the current version number.

Increments the version number.

Initializes state manager with default empty state.

Initializes state manager with options.

Lists all registered state domains.

Merges two functional states.

Puts a value into functional state.

Sets a state value atomically in ETS.

Starts a new managed state with supervision.

Executes a function within a transaction.

Updates a value in functional state using a function.

Updates managed state using a function.

Updates a state value with a function.

Validates functional state.

Types

state_key()

@type state_key() :: atom() | String.t() | [atom() | String.t()]

state_tree()

@type state_tree() :: map()

state_value()

@type state_value() :: term()

strategy()

@type strategy() :: :functional | :process | :ets

version()

@type version() :: non_neg_integer()

Functions

child_spec(arg)

Creates a supervised state manager as part of a supervision tree.

cleanup(state)

Cleans up state resources.

clear(state, opts \\ [])

Clears functional state.

delegate_to_domain(domain, function, args)

Delegates to domain-specific state manager.

delete(state, key, opts \\ [])

Deletes a key from functional state.

delete_state(key, opts \\ [])

Deletes a state value.

get(state, key, opts \\ [])

Gets a value from functional state.

Options

  • strategy: atom() - Force specific strategy (:functional, :process, :ets)

get(state, key, default, opts)

Gets a value from functional state with default.

get_managed(state_id, opts \\ [])

Gets the current managed state.

get_memory_usage(opts \\ [])

Gets memory usage statistics.

get_state(key \\ nil, opts \\ [])

Gets the current state or a specific key from ETS. When called without arguments, returns the entire state as a map.

get_version(opts \\ [])

Gets the current version number.

handle_manager_call(request, from, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_call/3.

handle_manager_cast(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.

handle_manager_info(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.

increment_version(opts \\ [])

Increments the version number.

initialize()

Initializes state manager with default empty state.

initialize(opts)

Initializes state manager with options.

list_domains()

Lists all registered state domains.

merge(state1, state2, opts \\ [])

Merges two functional states.

put(state, key, value, opts \\ [])

Puts a value into functional state.

set_state(key, value, opts \\ [])

Sets a state value atomically in ETS.

start_link(init_opts \\ [])

start_managed(state_id, initial_state, opts \\ [])

Starts a new managed state with supervision.

Options

  • strategy: :process | :ets - Choose the backing strategy

transaction(func, opts \\ [])

Executes a function within a transaction.

update(state, key, func, opts \\ [])

Updates a value in functional state using a function.

update_managed(state_id, update_fun, opts \\ [])

Updates managed state using a function.

update_state(key, update_fn)

Updates a state value with a function.

update_state(key, update_fn, opts)

validate(state, opts \\ [])

Validates functional state.