Struct ywasm::YTransaction[][src]

pub struct YTransaction(_);
Expand description

A transaction that serves as a proxy to document block store. 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'))

Implementations

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.

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.

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.

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.

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.

Triggers a post-update series of operations without freeing the transaction. This includes compaction and optimization of internal representation of updates, triggering events etc. ywasm transactions are auto-committed when they are freed.

Encodes a state vector of a given transaction document into its binary representation using lib0 v1 encoding. State vector is a compact representation of updates performed on a given document and can be used by encode_state_as_update on remote peer to generate a delta update payload to synchronize changes between peers.

Example:

import YDoc from 'ywasm'

/// document on machine A
const localDoc = new YDoc()
const localTxn = localDoc.beginTransaction()

// document on machine B
const remoteDoc = new YDoc()
const remoteTxn = localDoc.beginTransaction()

try {
    const localSV = localTxn.stateVectorV1()
    const remoteDelta = remoteTxn.diffV1(localSv)
    localTxn.applyV1(remoteDelta)
} finally {
    localTxn.free()
    remoteTxn.free()
}

Encodes all updates that have happened since a given version vector into a compact delta representation using lib0 v1 encoding. If vector parameter has not been provided, generated delta payload will contain all changes of a current ywasm document, working effectively as its state snapshot.

Example:

import YDoc from 'ywasm'

/// document on machine A
const localDoc = new YDoc()
const localTxn = localDoc.beginTransaction()

// document on machine B
const remoteDoc = new YDoc()
const remoteTxn = localDoc.beginTransaction()

try {
    const localSV = localTxn.stateVectorV1()
    const remoteDelta = remoteTxn.diffV1(localSv)
    localTxn.applyV1(remoteDelta)
} finally {
    localTxn.free()
    remoteTxn.free()
}

Applies delta update generated by the remote document replica to a current transaction’s document. This method assumes that a payload maintains lib0 v1 encoding format.

Example:

import YDoc from 'ywasm'

/// document on machine A
const localDoc = new YDoc()
const localTxn = localDoc.beginTransaction()

// document on machine B
const remoteDoc = new YDoc()
const remoteTxn = localDoc.beginTransaction()

try {
    const localSV = localTxn.stateVectorV1()
    const remoteDelta = remoteTxn.diffV1(localSv)
    localTxn.applyV1(remoteDelta)
} finally {
    localTxn.free()
    remoteTxn.free()
}

Methods from Deref<Target = Transaction<'static>>

Returns state vector describing current state of the updates.

Encodes the difference between remove peer state given its state_vector and the state of a current local peer

Returns a Text 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 not 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 Map 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 not 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 Array 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 not 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 XmlElement 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 not 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 XmlText 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 not 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).

Encodes the document state to a binary format.

Document updates are idempotent and commutative. Caveats:

  • It doesn’t matter in which order document updates are applied.
  • As long as all clients receive the same document updates, all clients end up with the same content.
  • Even if an update contains known information, the unknown information is extracted and integrated into the document structure.

Commits current transaction. This step involves cleaning up and optimizing changes performed during lifetime of a transaction. Such changes include squashing delete sets data or squashing blocks that have been appended one after another to preserve memory.

This step is performed automatically when a transaction is about to be dropped (its life scope comes to an end).

Trait Implementations

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Executes the destructor for this type. Read more

Performs the conversion.

The wasm ABI type that this converts from when coming back out from the ABI boundary. Read more

Recover a Self from Self::Abi. Read more

The wasm ABI type that this converts into when crossing the ABI boundary. Read more

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

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

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

The wasm ABI type references to Self are recovered from.

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

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

Same as RefFromWasmAbi::Abi

Same as RefFromWasmAbi::Anchor

Same as RefFromWasmAbi::ref_from_abi

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

Performs the conversion.

Performs the conversion.

Same as IntoWasmAbi::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err. 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.