Raxol.UI.State.Store (Raxol v2.0.1)
View SourceRefactored UI State Store with GenServer-based state management.
This module provides the same Redux-like state management as the original but uses supervised state instead of Process dictionary for debounce timers.
Migration Notes
Debounce timer management has been moved to the UI.State.Management.Server, eliminating Process dictionary usage while maintaining full functionality.
Summary
Functions
Dispatches an action to update the store.
Gets the current state or a value at a specific path.
Registers a reducer function for handling actions.
Starts the global state store.
Subscribes to state changes at a specific path.
Unsubscribes from state changes.
Updates a value in the store at the given path.
Updates state at a specific path directly (use sparingly - prefer dispatch).
Functions
Dispatches an action to update the store.
Examples
Store.dispatch({:user, :login, user_data})
Store.dispatch({:theme, :toggle})
Store.dispatch({:counter, :increment})
Gets the current state or a value at a specific path.
Examples
# Get entire state
state = Store.get_state()
# Get value at path
counter = Store.get_state([:counter])
user = Store.get_state([:user, :current])
Registers a reducer function for handling actions.
Examples
Store.register_reducer(fn
{:counter, :increment}, state ->
update_in(state, [:counter], &((&1 || 0) + 1))
{:counter, :decrement}, state ->
update_in(state, [:counter], &((&1 || 0) - 1))
_action, state ->
state
end)
Starts the global state store.
Subscribes to state changes at a specific path.
Examples
# Subscribe to counter changes
unsubscribe = Store.subscribe([:counter], fn new_value ->
Log.info("Counter: #{new_value}")
end)
# Subscribe with options
unsubscribe = Store.subscribe([:user], fn user ->
update_ui(user)
end, debounce: 100)
# Unsubscribe
unsubscribe.()
Unsubscribes from state changes.
Updates a value in the store at the given path.
This function supports multiple argument orders and function updates.
Examples
# Direct value update
Store.update(store, :counter, 42)
Store.update(store, [:user, :name], "John")
# Function update
Store.update(store, :counter, fn count -> count + 1 end)
Store.update(store, [:items], fn items -> [new_item | items] end)
Updates state at a specific path directly (use sparingly - prefer dispatch).
Examples
Store.update_state([:counter], 42)
Store.update_state([:user, :name], "John Doe")