pub struct StateManager { /* private fields */ }Expand description
Tracks the last published state so changes can go out as patches.
let mut states = StateManager::new();
let notes = "the document the user is editing, at some length";
// First publish: a snapshot, whatever the size.
let first = states.publish(json!({"step": 1, "notes": notes}))?;
assert!(matches!(first, StatePublish::Snapshot(_)));
// One field of a large document: a patch, because it is smaller.
let second = states.publish(json!({"step": 2, "notes": notes}))?;
assert_eq!(
second,
StatePublish::Delta(vec![PatchOperation::replace("/step", 2)])
);
// A small document changing wholesale: back to a snapshot, because the
// patch would be bigger than the state it describes.
let mut small = StateManager::new();
small.publish(json!({"a": 1}))?;
assert_eq!(
small.publish(json!({"b": 2}))?,
StatePublish::Snapshot(json!({"b": 2}))
);Implementations§
Source§impl StateManager
impl StateManager
Sourcepub fn published(&self) -> Option<&Value>
pub fn published(&self) -> Option<&Value>
The last published state, or None before the first publish.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Forgets the last publish, so the next one is a snapshot again.
Call this after emitting a STATE_SNAPSHOT by hand, or after a
reconnect where the client’s copy is no longer known.
Sourcepub fn publish(&mut self, next: Value) -> Result<StatePublish>
pub fn publish(&mut self, next: Value) -> Result<StatePublish>
Decides how to publish next and records it as the new baseline.
Returns StatePublish::Unchanged when nothing moved, so callers can
skip emitting entirely.
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 (const: unstable) · 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
Source§impl Default for StateManager
impl Default for StateManager
Source§fn default() -> StateManager
fn default() -> StateManager
Returns the “default value” for a type. Read more
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