Raxol.Core.StateManager (Raxol v1.5.4)

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)

Migration from Previous Modules

Replace:

Raxol.Core.UnifiedStateManager.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.

Clears functional state.

Delegates to domain-specific state manager.

Deletes a key from functional state.

Deletes a state value (alias for compatibility with UnifiedStateManager).

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 the current state or a specific key from ETS.

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.

Updates a value in functional state using a function.

Updates managed state using a function.

Updates a state value with a function (alias for compatibility with UnifiedStateManager).

Validates functional state.

Legacy support for existing code using Process dictionary.

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.

Examples

children = [
  {StateManager, name: MyApp.StateManager, initial_state: %{}}
]

Supervisor.start_link(children, strategy: :one_for_one)

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 (alias for compatibility with UnifiedStateManager).

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_and_update(agent, fun)

Agent-based get and update operation.

get_managed(state_id, opts \\ [])

Gets the current managed state.

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

Gets the current state or a specific key from ETS.

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.

Options

  • strategy: atom() - Force specific strategy

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

Sets a state value atomically in ETS.

start_link(initial_state \\ %{}, opts \\ [])

Starts a new state agent with the given initial state.

@deprecated "Use start_managed/3 for supervised state or functional operations for simple transformations"

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

Starts a new managed state with supervision. This is recommended for long-lived application state.

Options

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

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 (alias for compatibility with UnifiedStateManager).

update_state(key, update_fn, opts)

validate(state, opts \\ [])

Validates functional state.

with_state(state_key, fun)

Legacy support for existing code using Process dictionary.

@deprecated "Use start_managed/3 and update_managed/3 instead"