Struct ywasm::YDoc

source ·
pub struct YDoc(_);
Expand description

A ywasm 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 YTransaction, 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:

import YDoc from 'ywasm'

const doc = new YDoc()
const txn = doc.beginTransaction()
try {
    const text = txn.getText('name')
    text.push(txn, 'hello world')
    const output = text.toString(txn)
    console.log(output)
} finally {
    txn.free()
}

Implementations§

source§

impl YDoc

source

pub fn new(options: &JsValue) -> Self

Creates a new ywasm document. If id parameter was passed it will be used as this document globally unique identifier (it’s up to caller to ensure that requirement). Otherwise it will be assigned a randomly generated number.

source

pub fn parent_doc(&self) -> Option<YDoc>

Returns a parent document of this document or null if current document is not sub-document.

source

pub fn id(&self) -> f64

Gets unique peer identifier of this YDoc instance.

source

pub fn guid(&self) -> String

Gets globally unique identifier of this YDoc instance.

source

pub fn should_load(&self) -> bool

source

pub fn auto_load(&self) -> bool

source

pub fn read_transaction(&mut self) -> YTransaction

Returns a new transaction for this document. Ywasm shared data types execute their operations in a context of a given transaction. Each document can have only one active transaction at the time - subsequent attempts will cause exception to be thrown.

Transactions started with doc.beginTransaction can be released using transaction.free method.

Example:

import YDoc from 'ywasm'

// helper function used to simplify transaction
// create/release cycle
YDoc.prototype.transact = callback => {
    const txn = this.readTransaction()
    try {
        return callback(txn)
    } finally {
        txn.free()
    }
}

const doc = new YDoc()
const text = doc.getText('name')
doc.transact(txn => text.insert(txn, 0, 'hello world'))
source

pub fn write_transaction(&mut self, origin: JsValue) -> YTransaction

Returns a new transaction for this document. Ywasm shared data types execute their operations in a context of a given transaction. Each document can have only one active transaction at the time - subsequent attempts will cause exception to be thrown.

Transactions started with doc.beginTransaction can be released using transaction.free method.

Example:

import YDoc from 'ywasm'

// helper function used to simplify transaction
// create/release cycle
YDoc.prototype.transact = callback => {
    const txn = this.writeTransaction()
    try {
        return callback(txn)
    } finally {
        txn.free()
    }
}

const doc = new YDoc()
const text = doc.getText('name')
doc.transact(txn => text.insert(txn, 0, 'hello world'))
source

pub fn get_text(&mut self, name: &str) -> YText

Returns a YText shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YText instance.

source

pub fn get_array(&mut self, name: &str) -> YArray

Returns a YArray shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YArray instance.

source

pub fn get_map(&mut self, name: &str) -> YMap

Returns a YMap shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YMap instance.

source

pub fn get_xml_fragment(&mut self, name: &str) -> YXmlFragment

Returns a YXmlFragment shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YXmlFragment instance.

source

pub fn get_xml_element(&mut self, name: &str) -> YXmlElement

Returns a YXmlElement shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YXmlElement instance.

source

pub fn get_xml_text(&mut self, name: &str) -> YXmlText

Returns a YXmlText shared data type, that’s accessible for subsequent accesses using given name.

If there was no instance with this name before, it will be created and then returned.

If there was an instance with this name, but it was of different type, it will be projected onto YXmlText instance.

source

pub fn on_update(&mut self, f: Function) -> YUpdateObserver

Subscribes given function to be called any time, a remote update is being applied to this document. Function takes an Uint8Array as a parameter which contains a lib0 v1 encoded update.

Returns an observer, which can be freed in order to unsubscribe this callback.

source

pub fn on_update_v2(&mut self, f: Function) -> YUpdateObserver

Subscribes given function to be called any time, a remote update is being applied to this document. Function takes an Uint8Array as a parameter which contains a lib0 v2 encoded update.

Returns an observer, which can be freed in order to unsubscribe this callback.

source

pub fn on_after_transaction(&mut self, f: Function) -> YAfterTransactionObserver

Subscribes given function to be called, whenever a transaction created by this document is being committed.

Returns an observer, which can be freed in order to unsubscribe this callback.

source

pub fn on_subdocs(&mut self, f: Function) -> YSubdocsObserver

Subscribes given function to be called, whenever a subdocuments are being added, removed or loaded as children of a current document.

Returns an observer, which can be freed in order to unsubscribe this callback.

source

pub fn on_destroy(&mut self, f: Function) -> YDestroyObserver

Subscribes given function to be called, whenever current document is being destroyed.

Returns an observer, which can be freed in order to unsubscribe this callback.

source

pub fn load(&self, parent_txn: &ImplicitTransaction)

Notify the parent document that you request to load data into this subdocument (if it is a subdocument).

source

pub fn destroy(&mut self, parent_txn: &ImplicitTransaction)

Emit onDestroy event and unregister all event handlers.

source

pub fn subdocs(&self, txn: &ImplicitTransaction) -> Array

Returns a list of sub-documents existings within the scope of this document.

source

pub fn subdoc_guids(&self, txn: &ImplicitTransaction) -> Set

Returns a list of unique identifiers of the sub-documents existings within the scope of this document.

Trait Implementations§

source§

impl AsRef<Doc> for YDoc

source§

fn as_ref(&self) -> &Doc

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl From<Doc> for YDoc

source§

fn from(doc: Doc) -> Self

Converts to this type from the input type.
source§

impl From<YDoc> for JsValue

source§

fn from(value: YDoc) -> Self

Converts to this type from the input type.
source§

impl FromWasmAbi for YDoc

§

type Abi = u32

The wasm ABI type that this converts from when coming back out from the ABI boundary.
source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
source§

impl IntoWasmAbi for YDoc

§

type Abi = u32

The wasm ABI type that this converts into when crossing the ABI boundary.
source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
source§

impl LongRefFromWasmAbi for YDoc

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, YDoc>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl OptionFromWasmAbi for YDoc

source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
source§

impl OptionIntoWasmAbi for YDoc

source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
source§

impl RefFromWasmAbi for YDoc

§

type Abi = u32

The wasm ABI type references to Self are recovered from.
§

type Anchor = Ref<'static, YDoc>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
source§

impl RefMutFromWasmAbi for YDoc

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, YDoc>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl WasmDescribe for YDoc

Auto Trait Implementations§

§

impl !RefUnwindSafe for YDoc

§

impl Send for YDoc

§

impl Sync for YDoc

§

impl Unpin for YDoc

§

impl !UnwindSafe for YDoc

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ReturnWasmAbi for Twhere T: IntoWasmAbi,

§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
source§

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

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V