@homeostate/crdt-loro
v0.2.0
Published
Loro CRDT backend for @homeostate/core
Maintainers
Readme
@homeostate/crdt-loro
Loro backend for
@homeostate/core.
It maps the synced state onto a LoroMap (objects become LoroMap, arrays LoroList, and
strings plain values or, where you choose, LoroText), so a toggle or a keystroke produces one
small update.
Install
npm install @homeostate/core @homeostate/crdt-loro loro-crdtUsage
import { LoroDoc } from "loro-crdt";
import { createSyncEngine } from "@homeostate/core";
import { createLoroBackend } from "@homeostate/crdt-loro";
const doc = new LoroDoc();
const engine = createSyncEngine(createLoroBackend(doc, "shared"), adapter);
engine.connect();The synced state lives in doc.getMap("shared"). Replication is yours to wire, for example
with doc.subscribeLocalUpdates on one side and doc.import on the other.
undefined and functions are not JSON, so the backend leaves out object entries holding
either and stores such array items as null, as JSON.stringify does.
Strings are plain values, so concurrent writes keep one of them. Pass text to store chosen
strings as LoroText, whose concurrent edits merge character by character:
createLoroBackend(doc, "shared", {
text: (path) => path[0] === "todos" && path[2] === "title",
});To persist the document, pass createLoroPersistable(doc) to createPersistence; see the
persistence guide.
License
MIT — see LICENSE. Includes code derived from zustand-middleware-yjs; see THIRD_PARTY_NOTICES.md.
