pub struct StateManager { /* private fields */ }Available on crate feature
server only.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§impl Debug for StateManager
impl Debug for StateManager
Auto Trait Implementations§
impl Forget for StateManager
impl Freeze for StateManager
impl Move 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