Expand description
§Store Module
Redux-like store for centralized state management.
§Features
- Thread-safe with
Arc<Mutex<T>> - Subscribe/unsubscribe to state changes
- Batch dispatch operations
- Dynamic reducer replacement
- Read-only state access
§Example
use zed::{Store, create_reducer};
#[derive(Clone, Debug, PartialEq)]
struct AppState {
count: i32,
}
#[derive(Clone)]
enum Action {
Increment,
Decrement,
}
fn reducer(state: &AppState, action: &Action) -> AppState {
match action {
Action::Increment => AppState { count: state.count + 1 },
Action::Decrement => AppState { count: state.count - 1 },
}
}
let store = Store::new(AppState { count: 0 }, Box::new(create_reducer(reducer)));
// Subscribe and get an ID for later unsubscription
let subscription_id = store.subscribe(|state: &AppState| {
println!("Count: {}", state.count);
});
store.dispatch(Action::Increment);
assert_eq!(store.get_state().count, 1);
// Unsubscribe when no longer needed
store.unsubscribe(subscription_id);Structs§
- Store
- Redux-like store for centralized state management.
Type Aliases§
- Subscription
Id - Type alias for subscription IDs