#crdt #wasm #yrs

ywasm

High performance implementation of the Yjs CRDT

17 releases (7 breaking)

new 0.10.2 Jul 6, 2022
0.9.3 Jun 23, 2022
0.5.0 Mar 18, 2022
0.1.0 Oct 14, 2021

#78 in WebAssembly

Download history 28/week @ 2022-03-16 10/week @ 2022-03-23 2/week @ 2022-03-30 14/week @ 2022-04-06 41/week @ 2022-04-13 41/week @ 2022-04-20 86/week @ 2022-04-27 100/week @ 2022-05-04 121/week @ 2022-05-11 67/week @ 2022-05-18 95/week @ 2022-05-25 106/week @ 2022-06-01 51/week @ 2022-06-08 18/week @ 2022-06-15 27/week @ 2022-06-22 38/week @ 2022-06-29

134 downloads per month

MIT license

110KB
2K SLoC

Ywasm

This project is a wrapper around Yrs and targets Web Assembly bindings.

It's a library used on collaborative document editing using Conflict-free Replicated Data Types. This enables to provide a shared document editing experience on a client devices without explicit requirement for hosting a single server - CRDTs can resolve potential update conflicts on their own with no central authority - as well as provide first-class offline editing capabilities, where document replicas are modified without having connection to each other, and then synchronize automatically once such connection is enabled.

Documentation

Example

import {YDoc, encodeStateVector, encodeStateAsUpdate, applyUpdate} from 'ywasm';

YDoc.prototype.transact = callback => {
    const txn = this.beginTransaction()
    try {
        return callback(txn)
    } finally {
        txn.free()
    }
}

const doc = new YDoc()
const text = doc.getText('name')

// append text to our collaborative document
doc.transact(txn => text.insert(txn, 0, 'hello world'))

// simulate update with remote peer
const remoteDoc = new YDoc()
const remoteText = remoteDoc.getText('name')

// in order to exchange data with other documents 
// we first need to create a state vector
const remoteSV = encodeStateVector(remoteDoc)
// now compute a differential update based on remote document's state vector
const update = encodeStateAsUpdate(doc, remoteSV)
// both update and state vector are serializable, we can pass them over the wire
// now apply update to a remote document
applyUpdate(remoteDoc, update)

const str = remoteDoc.transact(txn => remoteText.toString(txn))
console.log(str)

Dependencies

~10MB
~50K SLoC