Raxol.UI.State.Store (Raxol v2.8.0)

View Source

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

Summary

Functions

batch_update(actions, store \\ nil)

create_selector(paths, compute_fn, store \\ nil)

delete_state(path, store \\ nil)

dispatch(action, store \\ nil)

Dispatches an action to update the store.

Examples

Store.dispatch({:user, :login, user_data})
Store.dispatch({:theme, :toggle})
Store.dispatch({:counter, :increment})

get_history(store \\ nil)

get_state(path \\ [], store \\ nil)

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])

pause_updates(paused \\ true, store \\ nil)

register_middleware(middleware_fn, store \\ nil)

register_reducer(reducer_fn, store \\ nil)

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)

set_time_travel(enabled, store \\ nil)

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

Starts the global state store.

subscribe(path, callback)

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.()

subscribe(path, callback, options)

subscribe(path, callback, options, store)

time_travel_back(steps \\ 1, store \\ nil)

time_travel_forward(steps \\ 1, store \\ nil)

unsubscribe(subscription_id, store \\ nil)

Unsubscribes from state changes.

update(store \\ nil, path, value_or_fun)

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)

update_state(path, value, store \\ nil)

Updates state at a specific path directly (use sparingly - prefer dispatch).

Examples

Store.update_state([:counter], 42)
Store.update_state([:user, :name], "John Doe")