pub struct StateManager { /* private fields */ }Expand description
A helper for managing state and generating deltas.
StateManager tracks the current state and provides methods to update
it while automatically computing the JSON Patch delta between states.
This is useful for efficiently synchronizing state with frontends.
§Example
use ag_ui_core::state::StateManager;
use serde_json::json;
let mut manager = StateManager::new(json!({"count": 0, "items": []}));
// Update state - returns delta patch
let delta = manager.update(json!({"count": 1, "items": []}));
assert!(delta.is_some());
// No change - returns None
let delta = manager.update(json!({"count": 1, "items": []}));
assert!(delta.is_none());
// Check current state
assert_eq!(manager.current()["count"], 1);Implementations§
Source§impl StateManager
impl StateManager
Sourcepub fn new(initial: JsonValue) -> Self
pub fn new(initial: JsonValue) -> Self
Creates a new state manager with the given initial state.
Sourcepub fn update(&mut self, new_state: JsonValue) -> Option<Patch>
pub fn update(&mut self, new_state: JsonValue) -> Option<Patch>
Updates the state and returns the delta patch if there were changes.
Returns None if the new state is identical to the current state.
Sourcepub fn update_with<F>(&mut self, f: F) -> Option<Patch>
pub fn update_with<F>(&mut self, f: F) -> Option<Patch>
Updates the state using a closure and returns the delta patch.
The closure receives a mutable reference to the current state. After the closure completes, the delta is computed.
§Example
use ag_ui_core::state::StateManager;
use serde_json::json;
let mut manager = StateManager::new(json!({"count": 0}));
let delta = manager.update_with(|state| {
state["count"] = json!(10);
});
assert!(delta.is_some());
assert_eq!(manager.current()["count"], 10);Trait Implementations§
Source§impl Clone for StateManager
impl Clone for StateManager
Source§fn clone(&self) -> StateManager
fn clone(&self) -> StateManager
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for StateManager
impl Debug for StateManager
Auto Trait Implementations§
impl Freeze for StateManager
impl RefUnwindSafe for StateManager
impl Send for StateManager
impl Sync for StateManager
impl Unpin for StateManager
impl UnsafeUnpin for StateManager
impl UnwindSafe for StateManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more