Struct yrs::Doc

source · []
pub struct Doc {
    pub client_id: ClientID,
    /* private fields */
}
Expand description

A Yrs document type. Documents are most important units of collaborative resources management. All shared collections live within a scope of their corresponding documents. All updates are generated on per document basis (rather than individual shared type). All operations on shared collections happen via Transaction, which lifetime is also bound to a document.

Document manages so called root types, which are top-level shared types definitions (as opposed to recursively nested types).

A basic workflow sample:

use yrs::{Doc, StateVector, Update};
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;

let doc = Doc::new();
let mut txn = doc.transact(); // all Yrs operations happen in scope of a transaction
let root = txn.get_text("root-type-name");
root.push(&mut txn, "hello world"); // append text to our collaborative document

// in order to exchange data with other documents we first need to create a state vector
let remote_doc = Doc::new();
let mut remote_txn = remote_doc.transact();
let state_vector = remote_txn.state_vector().encode_v1();

// now compute a differential update based on remote document's state vector
let update = txn.encode_diff_v1(&StateVector::decode_v1(&state_vector));

// both update and state vector are serializable, we can pass the over the wire
// now apply update to a remote document
remote_txn.apply_update(Update::decode_v1(update.as_slice()));

Fields

client_id: ClientID

A unique client identifier, that’s also a unique identifier of current document replica.

Implementations

Creates a new document with a randomized client identifier.

Creates a new document with a specified client_id. It’s up to a caller to guarantee that this identifier is unique across all communicating replicas of that document.

Creates a transaction used for all kind of block store operations. Transaction cleanups & calling event handles happen when the transaction struct is dropped.

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v1 encoding and can be decoded using [Update::decode_v1] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Returns a subscription, which will unsubscribe function when dropped.

Manually unsubscribes from a callback used in Doc::observe_update_v1 method.

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v1 encoding and can be decoded using [Update::decode_v2] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Returns a subscription, which will unsubscribe function when dropped.

Manually unsubscribes from a callback used in Doc::observe_update_v1 method.

Subscribe callback function to updates on the Doc. The callback will receive state updates and deletions when a document transaction is committed.

Cancels the transaction cleanup callback associated with the subscription_id

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.