Raxol.Core.StateManager (Raxol v2.0.1)
View SourceConsolidated 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: trueOr control per-call with options:
StateManager.put(state, :key, value, strategy: :ets)
StateManager.start_managed(:app_state, %{}, strategy: :process)Migration from Previous Modules
Replace:
Raxol.Core.StateManager.get_state(:key)
Raxol.Core.StateManager.Unified.update_managed(:id, fun)
Raxol.Core.StateManager.Default.put(state, :key, value)With:
Raxol.Core.StateManager.get_state(:key, strategy: :ets)
Raxol.Core.StateManager.update_managed(:id, fun, strategy: :process)
Raxol.Core.StateManager.put(state, :key, value, strategy: :functional)Examples
# Functional state (no processes, good for simple transformations)
state = %{count: 0}
{:ok, new_state} = StateManager.put(state, :count, 1)
# Process-based managed state (supervised processes)
{:ok, state_id} = StateManager.start_managed(:app_state, %{count: 0})
StateManager.update_managed(:app_state, fn s -> %{s | count: s.count + 1} end)
# ETS-backed state (high performance for large datasets)
StateManager.set_state(:global_config, %{theme: "dark"}, strategy: :ets)
config = StateManager.get_state(:global_config, strategy: :ets)
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.
Agent-based get and update operation.
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.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_call/3.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.
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 state agent with the given initial state.
Starts a new managed state with supervision. This is recommended for long-lived application state.
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.
Legacy support for existing code using Process dictionary.
Types
Functions
Creates a supervised state manager as part of a supervision tree.
Examples
children = [
{StateManager, name: MyApp.StateManager, initial_state: %{}}
]
Supervisor.start_link(children, strategy: :one_for_one)
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.
Options
strategy: atom()- Force specific strategy (:functional, :process, :ets)
Gets a value from functional state with default.
Agent-based get and update operation.
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.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_call/3.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.
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.
Options
strategy: atom()- Force specific strategy
Sets a state value atomically in ETS.
Starts a new state agent with the given initial state.
@deprecated "Use start_managed/3 for supervised state or functional operations for simple transformations"
Starts a new managed state with supervision. This is recommended for long-lived application state.
Options
strategy: :process | :ets- Choose the backing strategy
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.
Legacy support for existing code using Process dictionary.
@deprecated "Use start_managed/3 and update_managed/3 instead"