Struct yrs::Doc

source ·
pub struct Doc { /* 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, ReadTxn, StateVector, Text, Transact, Update};
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;

let doc = Doc::new();
let root = doc.get_or_insert_text("root-type-name");
let mut txn = doc.transact_mut(); // all Yrs operations happen in scope of a transaction
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_mut();
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).unwrap());

// 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()).unwrap());

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.

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

A globally unique identifier, that’s also a unique identifier of current document replica, and unlike Doc::client_id it’s not shared with its subdocuments.

Returns config options of this Doc instance.

Returns a TextRef data structure stored under a given name. Text structures are used for collaborative text editing: they expose operations to append and remove chunks of text, which are free to execute concurrently by multiple peers over remote boundaries.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a text (in such case a sequence component of complex data type will be interpreted as a list of text chunks).

Returns a MapRef data structure stored under a given name. Maps are used to store key-value pairs associated together. These values can be primitive data (similar but not limited to a JavaScript Object Notation) as well as other shared types (Yrs maps, arrays, text structures etc.), enabling to construct a complex recursive tree structures.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a map (in such case a map component of complex data type will be interpreted as native map).

Returns an ArrayRef data structure stored under a given name. Array structures are used for storing a sequences of elements in ordered manner, positioning given element accordingly to its index.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as an array (in such case a sequence component of complex data type will be interpreted as a list of inserted values).

Returns a XmlFragmentRef data structure stored under a given name. XML elements represent nodes of XML document. They can contain attributes (key-value pairs, both of string type) as well as other nested XML elements or text values, which are stored in their insertion order.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a XML element (in such case a map component of complex data type will be interpreted as map of its attributes, while a sequence component - as a list of its child XML nodes).

Returns a XmlElementRef data structure stored under a given name. XML elements represent nodes of XML document. They can contain attributes (key-value pairs, both of string type) as well as other nested XML elements or text values, which are stored in their insertion order.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a XML element (in such case a map component of complex data type will be interpreted as map of its attributes, while a sequence component - as a list of its child XML nodes).

Returns a XmlTextRef data structure stored under a given name. Text structures are used for collaborative text editing: they expose operations to append and remove chunks of text, which are free to execute concurrently by multiple peers over remote boundaries.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a text (in such case a sequence component of complex data type will be interpreted as a list of text chunks).

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

Subscribe callback function, that will be called whenever a subdocuments inserted in this Doc will request a load.

Cancels the subscription created previously using Doc::observe_subdocs.

Subscribe callback function, that will be called whenever a [DocRef::destroy] has been called.

Cancels the subscription created previously using Doc::observe_subdocs.

Sends a load request to a parent document. Works only if current document is a sub-document of an another document.

Starts destroy procedure for a current document, triggering an “destroy” callback and invalidating all event callback subscriptions.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method is used to create initial content required in order to create a block item. A supplied ptr can be used to identify block that is about to be created to store the returned content. Read more
Method called once an original item filled with content from Self::into_content has been added to block store. This method is used by complex types such as maps or arrays to append the original contents of prelim struct into YMap, YArray etc. Read more
Converts all contents of a current type into a JSON-like representation.
Creates a transaction used for all kind of block store operations. Transaction cleanups & calling event handles happen when the transaction struct is dropped. Read more
Creates a transaction used for all kind of block store operations. Transaction cleanups & calling event handles happen when the transaction struct is dropped. Read more
Creates a transaction used for all kind of block store operations. Transaction cleanups & calling event handles happen when the transaction struct is dropped. Read more
Creates a transaction used for all kind of block store operations. Transaction cleanups & calling event handles happen when the transaction struct is dropped. 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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
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.