loro-crdt
v1.0.9
Published
Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.
Downloads
4,263
Maintainers
Readme
Loro is a CRDTs(Conflict-free Replicated Data Types) library that makes building local-first apps easier. It is currently available for JavaScript (via WASM) and Rust developers.
Features
Basic Features Provided by CRDTs
- P2P Synchronization
- Automatic Merging
- Local Availability
- Scalability
- Delta Updates
Supported CRDT Algorithms
- 📝 Text Editing with Fugue
- 📙 Peritext-like Rich Text CRDT
- 🌲 Moveable Tree
- 🚗 Moveable List
- 🗺️ Last-Write-Wins Map
- 🔄 Replayable Event Graph
Advanced Features in Loro
- ⏱️ Fast Time Travel Through History
- 🏛️ Version Control with Real-Time Collaboration
- 📦 Shallow Snapshot that Works like Git Shallow Clone
In this example, we demonstrate importing an entire Loro codebase into a Loro-powered version controller, preserving the complete Git DAG history while enabling fast version switching.
Example
import { expect, test } from 'vitest';
import { LoroDoc, LoroList } from 'loro-crdt';
test('sync example', () => {
/**
* Demonstrates synchronization of two documents with two rounds of exchanges.
*/
// Initialize document A
const docA = new LoroDoc();
const listA: LoroList = docA.getList('list');
listA.insert(0, 'A');
listA.insert(1, 'B');
listA.insert(2, 'C');
// Export the state of document A as a byte array
const bytes: Uint8Array = docA.export({ mode: 'update' });
// Simulate sending `bytes` across the network to another peer, B
const docB = new LoroDoc();
// Peer B imports the updates from A
docB.import(bytes);
// Verify that B's state matches A's state
expect(docB.toJSON()).toStrictEqual({
list: ['A', 'B', 'C'],
});
// Get the current operation log version of document B
const version = docB.oplogVersion();
// Simulate editing at B: delete item 'B'
const listB: LoroList = docB.getList('list');
listB.delete(1, 1);
// Export the updates from B since the last synchronization point
const bytesB: Uint8Array = docB.export({ mode: 'update', from: version });
// Simulate sending `bytesB` back across the network to A
// A imports the updates from B
docA.import(bytesB);
// Verify that the list at A now matches the list at B after merging
expect(docA.toJSON()).toStrictEqual({
list: ['A', 'C'],
});
});
Credits
Loro draws inspiration from the innovative work of the following projects and individuals:
- Ink & Switch: The principles of Local-first Software have greatly influenced this project. The Peritext project has also shaped our approach to rich text CRDTs.
- Diamond-types: The Event Graph Walker (Eg-walker) algorithm from @josephg has been adapted to reduce the computation and space usage of CRDTs.
- Automerge: Their use of columnar encoding for CRDTs has informed our strategies for efficient data encoding.
- Yjs: We have incorporated a similar algorithm for effectively merging collaborative editing operations, thanks to their pioneering work.
- Matthew Weidner: His work on the Fugue algorithm has been invaluable, enhancing our text editing capabilities.
- Martin Kleppmann: His work on CRDTs has significantly influenced our comprehension of the field.