pub struct Document {
pub local_actor: ActorId,
pub version: VectorClock,
pub simplify_epsilon: f32,
/* private fields */
}Expand description
The document root. Contains the full CRDT state.
Fields§
§local_actor: ActorIdActor ID of this client. Assigned by server on first connect.
version: VectorClockVector clock: tracks what we’ve seen from each peer.
pub — read by callers for delta-sync state vector encoding.
simplify_epsilon: f32RDP epsilon applied automatically to every inserted stroke.
Set to 0.0 to disable auto-simplification.
Default: 0.5 (sub-pixel accuracy on high-DPI displays).
Implementations§
Source§impl Document
impl Document
pub fn new(actor: ActorId) -> Self
Sourcepub fn take_pending_ops(&mut self) -> Vec<Operation>
pub fn take_pending_ops(&mut self) -> Vec<Operation>
Drain and return all pending operations accumulated since the last flush. Call this to get the ops to encode and send to the server.
Sourcepub fn insert_stroke(
&mut self,
data: StrokeData,
properties: StrokeProperties,
) -> StrokeId
pub fn insert_stroke( &mut self, data: StrokeData, properties: StrokeProperties, ) -> StrokeId
Insert a stroke at the top of the z-order (after all existing strokes).
If simplify_epsilon > 0, automatically applies Ramer-Douglas-Peucker
before storing and broadcasting — reducing point count and wire size.
Sourcepub fn delete_stroke(&mut self, target: StrokeId) -> bool
pub fn delete_stroke(&mut self, target: StrokeId) -> bool
Delete a stroke by ID.
Sourcepub fn update_color(&mut self, target: StrokeId, color: u32) -> bool
pub fn update_color(&mut self, target: StrokeId, color: u32) -> bool
Update stroke color.
Sourcepub fn update_stroke_width(&mut self, target: StrokeId, width: f32) -> bool
pub fn update_stroke_width(&mut self, target: StrokeId, width: f32) -> bool
Update stroke width.
Sourcepub fn update_opacity(&mut self, target: StrokeId, opacity: f32) -> bool
pub fn update_opacity(&mut self, target: StrokeId, opacity: f32) -> bool
Update stroke opacity.
Sourcepub fn update_transform(
&mut self,
target: StrokeId,
transform: Transform2D,
) -> bool
pub fn update_transform( &mut self, target: StrokeId, transform: Transform2D, ) -> bool
Update stroke transform.
Sourcepub fn set_metadata(&mut self, key: MetadataKey, value: MetadataValue)
pub fn set_metadata(&mut self, key: MetadataKey, value: MetadataValue)
Set a metadata key.
Sourcepub fn apply_remote(&mut self, op: Operation) -> Option<StrokeId>
pub fn apply_remote(&mut self, op: Operation) -> Option<StrokeId>
Apply a remote operation. Core of the merge. Returns the affected StrokeId (if any) for incremental re-render.
Sourcepub fn simplify_stroke(&mut self, target: StrokeId, epsilon: f32) -> usize
pub fn simplify_stroke(&mut self, target: StrokeId, epsilon: f32) -> usize
Simplify an existing stroke’s points using Ramer-Douglas-Peucker. Returns the number of points removed, or 0 if not found.
Does NOT generate a pending op — simplification is a local optimization that reduces memory. The simplified version is only sent if the stroke is subsequently re-inserted. For existing strokes already synced to peers, use this only on your local replica.
Sourcepub fn undo_last_stroke(&mut self) -> Option<StrokeId>
pub fn undo_last_stroke(&mut self) -> Option<StrokeId>
Undo the last stroke drawn by the local actor.
Pops the undo stack and deletes the stroke — generating a DeleteStroke
operation that will be broadcast to peers. If the stroke was already
deleted remotely, skips it and tries the next one.
Returns the StrokeId that was deleted, or None if the stack is empty.
Sourcepub fn undo_depth(&self) -> usize
pub fn undo_depth(&self) -> usize
Number of strokes currently available to undo.
Sourcepub fn visible_stroke_ids(&self) -> Vec<StrokeId> ⓘ
pub fn visible_stroke_ids(&self) -> Vec<StrokeId> ⓘ
Returns visible stroke IDs in z-order (bottom to top).
Sourcepub fn get_stroke(
&self,
id: &StrokeId,
) -> Option<(&StrokeData, &StrokeProperties)>
pub fn get_stroke( &self, id: &StrokeId, ) -> Option<(&StrokeData, &StrokeProperties)>
Returns the stroke data + properties for a given ID.
pub fn stats(&self) -> DocumentStats
Source§impl Document
impl Document
Sourcepub fn should_gc(&self, config: &GcConfig) -> bool
pub fn should_gc(&self, config: &GcConfig) -> bool
Returns true if GC should run based on current thresholds.
Sourcepub fn run_gc(&mut self, config: &GcConfig) -> GcResult
pub fn run_gc(&mut self, config: &GcConfig) -> GcResult
Runs an incremental GC cycle.
SAFETY: Only removes tombstones that are causally stable — i.e., ALL known peers have seen the delete operation. This guarantees no future peer will need the tombstone to resolve conflicts.
§Re-parenting of origin references
Before erasing GC’d tombstones, this method re-parents any surviving
item whose origin_left (or origin_right) pointed to a GC’d ID.
Re-parenting walks the chain to the nearest kept ancestor, so that
encoded snapshots remain fully self-contained — no dangling references.
Because re-parenting is deterministic (same MVV → same GC set across all peers), convergence is preserved: two peers that GC the same set produce identical re-parented states.
§Offline peers
Peers offline longer than gc_grace_period must perform a full state
sync on reconnect; their deltas may reference items already GC’d.
Sourcepub fn update_min_version(
&mut self,
mvv: VectorClock,
config: &GcConfig,
) -> Option<GcResult>
pub fn update_min_version( &mut self, mvv: VectorClock, config: &GcConfig, ) -> Option<GcResult>
Update the Minimum Version Vector (MVV) and run GC if needed.
Source§impl Document
impl Document
Sourcepub fn apply_remote_buffered(
&mut self,
op: Operation,
buffer: &mut CausalBuffer,
) -> VectisResult<Vec<StrokeId>>
pub fn apply_remote_buffered( &mut self, op: Operation, buffer: &mut CausalBuffer, ) -> VectisResult<Vec<StrokeId>>
Apply a remote operation through the causal buffer.
Pushes op into the buffer, then repeatedly drains + applies
ready operations until no more can be unblocked.
Returns the StrokeIds that were changed (for incremental re-render).