Skip to main content

Document

Struct Document 

Source
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: ActorId

Actor ID of this client. Assigned by server on first connect.

§version: VectorClock

Vector clock: tracks what we’ve seen from each peer. pub — read by callers for delta-sync state vector encoding.

§simplify_epsilon: f32

RDP 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

Source

pub fn new(actor: ActorId) -> Self

Source

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.

Source

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.

Source

pub fn delete_stroke(&mut self, target: StrokeId) -> bool

Delete a stroke by ID.

Source

pub fn update_color(&mut self, target: StrokeId, color: u32) -> bool

Update stroke color.

Source

pub fn update_stroke_width(&mut self, target: StrokeId, width: f32) -> bool

Update stroke width.

Source

pub fn update_opacity(&mut self, target: StrokeId, opacity: f32) -> bool

Update stroke opacity.

Source

pub fn update_transform( &mut self, target: StrokeId, transform: Transform2D, ) -> bool

Update stroke transform.

Source

pub fn set_metadata(&mut self, key: MetadataKey, value: MetadataValue)

Set a metadata key.

Source

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.

Source

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.

Source

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.

Source

pub fn undo_depth(&self) -> usize

Number of strokes currently available to undo.

Source

pub fn visible_stroke_ids(&self) -> Vec<StrokeId>

Returns visible stroke IDs in z-order (bottom to top).

Source

pub fn get_stroke( &self, id: &StrokeId, ) -> Option<(&StrokeData, &StrokeProperties)>

Returns the stroke data + properties for a given ID.

Source

pub fn stats(&self) -> DocumentStats

Source§

impl Document

Source

pub fn should_gc(&self, config: &GcConfig) -> bool

Returns true if GC should run based on current thresholds.

Source

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.

Source

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

Source

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).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.