pub struct StateManager { /* private fields */ }Expand description
StateManager manages immutable state with patch history.
§Design
- State is immutable - all changes go through commits
- Full history is maintained for replay
- Supports batch commits and preview operations
§API Philosophy
commit/commit_batch: Mutating operations that modify state and record historypreview_patch: Pure operation that computes result without modifying statereplay_to: Pure operation that reconstructs historical state
§Example
use tirea_state::{StateManager, StateContext};
use serde_json::json;
let manager = StateManager::new(json!({}));
// Get snapshot and create state context
let snapshot = manager.snapshot().await;
let ctx = StateContext::new(&snapshot);
// ... modify state through ctx ...
// Commit patch (modifies state and records history)
manager.commit(ctx.take_tracked_patch("tool:example")).await?;
// Replay to a specific point
let old_state = manager.replay_to(5).await?;Implementations§
Source§impl StateManager
impl StateManager
Sourcepub async fn register_lattice<T>(&self, path: impl Into<Path>)
pub async fn register_lattice<T>(&self, path: impl Into<Path>)
Register a lattice type at the given path for automatic merge during apply.
Sourcepub async fn commit(
&self,
patch: TrackedPatch,
) -> Result<ApplyResult, StateError>
pub async fn commit( &self, patch: TrackedPatch, ) -> Result<ApplyResult, StateError>
Commit a single patch, modifying state and recording to history.
This is a mutating operation that:
- Applies the patch to current state
- Updates the internal state
- Records the patch in history for replay
Sourcepub async fn commit_batch(
&self,
patches: Vec<TrackedPatch>,
) -> Result<ApplyResult, StateError>
pub async fn commit_batch( &self, patches: Vec<TrackedPatch>, ) -> Result<ApplyResult, StateError>
Commit multiple patches in batch.
Patches are applied in order atomically. If any patch fails, no changes are persisted to state or history.
Sourcepub async fn preview_patch(&self, patch: &Patch) -> Result<Value, StateError>
pub async fn preview_patch(&self, patch: &Patch) -> Result<Value, StateError>
Preview applying a patch without modifying state (pure operation).
This computes what the state would look like after applying the patch, but does not modify the actual state or record to history.
Useful for validation, dry-runs, or showing users what changes would occur.
Sourcepub async fn apply(
&self,
patch: TrackedPatch,
) -> Result<ApplyResult, StateError>
👎Deprecated since 0.2.0: Use commit instead
pub async fn apply( &self, patch: TrackedPatch, ) -> Result<ApplyResult, StateError>
commit insteadDeprecated: Use commit instead.
Sourcepub async fn apply_batch(
&self,
patches: Vec<TrackedPatch>,
) -> Result<ApplyResult, StateError>
👎Deprecated since 0.2.0: Use commit_batch instead
pub async fn apply_batch( &self, patches: Vec<TrackedPatch>, ) -> Result<ApplyResult, StateError>
commit_batch insteadDeprecated: Use commit_batch instead.
Sourcepub async fn replay_to(&self, index: usize) -> Result<Value, StateError>
pub async fn replay_to(&self, index: usize) -> Result<Value, StateError>
Replay state from the beginning up to (and including) the specified index.
Returns the state as it was after applying patches [0..=index] to the initial state.
Sourcepub async fn history(&self) -> Vec<TrackedPatch>
pub async fn history(&self) -> Vec<TrackedPatch>
Get the full patch history.
Sourcepub async fn history_len(&self) -> usize
pub async fn history_len(&self) -> usize
Get the number of patches in history.
Sourcepub async fn clear_history(&self)
pub async fn clear_history(&self)
Clear history (keeps current state).
Use with caution - this removes the ability to replay.
Sourcepub async fn prune_history(&self, keep_last: usize) -> Result<usize, StateError>
pub async fn prune_history(&self, keep_last: usize) -> Result<usize, StateError>
Prune history, keeping only the last keep_last patches.
This is useful for long-running systems to prevent unbounded memory growth.
The initial state is updated to the state before the remaining patches,
so replay_to will continue to work correctly with the remaining patches.
§Arguments
keep_last: Number of recent patches to keep. If 0, all patches are removed.
§Returns
The number of patches that were removed.