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
impl YDoc
sourcepub fn new(options: &JsValue) -> Self
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.
sourcepub fn parent_doc(&self) -> Option<YDoc>
pub fn parent_doc(&self) -> Option<YDoc>
Returns a parent document of this document or null if current document is not sub-document.
pub fn should_load(&self) -> bool
pub fn auto_load(&self) -> bool
sourcepub fn read_transaction(&mut self) -> YTransaction
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'))
sourcepub fn write_transaction(&mut self, origin: JsValue) -> YTransaction
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'))
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_fragment(&mut self, name: &str) -> YXmlFragment
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.
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.
sourcepub fn on_subdocs(&mut self, f: Function) -> YSubdocsObserver
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.
sourcepub fn on_destroy(&mut self, f: Function) -> YDestroyObserver
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.
sourcepub fn load(&self, parent_txn: &ImplicitTransaction)
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).
sourcepub fn destroy(&mut self, parent_txn: &ImplicitTransaction)
pub fn destroy(&mut self, parent_txn: &ImplicitTransaction)
Emit onDestroy
event and unregister all event handlers.
sourcepub fn subdocs(&self, txn: &ImplicitTransaction) -> Array
pub fn subdocs(&self, txn: &ImplicitTransaction) -> Array
Returns a list of sub-documents existings within the scope of this document.
sourcepub fn subdoc_guids(&self, txn: &ImplicitTransaction) -> Set
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 FromWasmAbi for YDoc
impl FromWasmAbi for YDoc
source§impl IntoWasmAbi for YDoc
impl IntoWasmAbi for YDoc
source§impl LongRefFromWasmAbi for YDoc
impl LongRefFromWasmAbi for YDoc
source§impl OptionFromWasmAbi for YDoc
impl OptionFromWasmAbi for YDoc
source§impl OptionIntoWasmAbi for YDoc
impl OptionIntoWasmAbi for YDoc
source§impl RefFromWasmAbi for YDoc
impl RefFromWasmAbi for YDoc
source§impl 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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere T: IntoWasmAbi,
§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::Abi
source§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi
, except that it may throw and never
return in the case of Err
.