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
sourceimpl YDoc
impl YDoc
sourcepub fn new(id: Option<f64>) -> Self
pub fn new(id: Option<f64>) -> 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.
sourcepub fn begin_transaction(&mut self) -> YTransaction
pub fn begin_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.beginTransaction()
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'))
sourcepub fn get_text(&mut self, name: &str) -> YText
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.
sourcepub fn get_array(&mut self, name: &str) -> YArray
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.
sourcepub fn get_map(&mut self, name: &str) -> YMap
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.
sourcepub fn get_xml_element(&mut self, name: &str) -> YXmlElement
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.
sourcepub fn get_xml_text(&mut self, name: &str) -> YXmlText
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.
sourcepub fn on_update(&mut self, f: Function) -> YUpdateObserver
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.
sourcepub fn on_update_v2(&mut self, f: Function) -> YUpdateObserver
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.
sourcepub fn on_after_transaction(&mut self, f: Function) -> YAfterTransactionObserver
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.
Trait Implementations
sourceimpl FromWasmAbi for YDoc
impl FromWasmAbi for YDoc
sourceimpl IntoWasmAbi for YDoc
impl IntoWasmAbi for YDoc
sourceimpl OptionFromWasmAbi for YDoc
impl OptionFromWasmAbi for YDoc
sourceimpl OptionIntoWasmAbi for YDoc
impl OptionIntoWasmAbi for YDoc
sourceimpl RefFromWasmAbi for YDoc
impl RefFromWasmAbi for YDoc
type Anchor = Ref<'static, YDoc>
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. Read more
sourceunsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor
Recover a Self::Anchor
from Self::Abi
. Read more
sourceimpl RefMutFromWasmAbi for YDoc
impl RefMutFromWasmAbi 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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ReturnWasmAbi for T where
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for T where
T: IntoWasmAbi,
type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
Same as IntoWasmAbi::Abi
sourcefn return_abi(self) -> <T as ReturnWasmAbi>::Abi
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
. Read more