@homeostate/persist-indexeddb
v0.1.1
Published
IndexedDB persistence adapter for @homeostate/core
Maintainers
Readme
@homeostate/persist-indexeddb
IndexedDB storage for createPersistence from
@homeostate/core. It keeps
a Yjs, Loro or Automerge document in the browser, so its state survives reloads, offline starts
and every peer leaving the room.
Install
npm install @homeostate/core @homeostate/persist-indexeddb @homeostate/crdt-yjs yjsUsage
import * as Y from "yjs";
import { createPersistence, createSyncEngine } from "@homeostate/core";
import { createYjsBackend, createYjsPersistable } from "@homeostate/crdt-yjs";
import { createIndexedDbAdapter } from "@homeostate/persist-indexeddb";
const doc = new Y.Doc();
const persistence = createPersistence(
createYjsPersistable(doc),
createIndexedDbAdapter(),
{ key: "todos" },
);
await persistence.whenLoaded;
createSyncEngine(createYjsBackend(doc, "shared"), adapter).connect();Connect the engine after whenLoaded, so it adopts the restored state instead of seeding the
store's defaults over it. Its result tells whether a stored document was found and restored in
full; see the persistence guide.
createIndexedDbAdapter({ name, indexedDB }) takes an optional database name (default
"homeostate") and IndexedDB implementation, such as fake-indexeddb in tests. Several tabs
can persist the same key safely.
License
MIT — see LICENSE.
