js-bao-wss-client / YjsPersistenceFactory
Type Alias: YjsPersistenceFactory
YjsPersistenceFactory = (
storeName,ydoc,context) =>YjsPersistenceProvider|null
Factory function for creating Yjs persistence providers.
Parameters
storeName
string
The name to persist this Yjs state UNDER. For an ordinary document, and for a large document (documentFormat: 2) that has not rotated yet, this is the document id. Once a large document rotates, its Yjs state is an epoch OVERLAY rather than the document, and each epoch gets its own name ({documentId}@{epoch}) — a rotation attaches a provider for the fresh epoch and then clears the sealed one, so two epochs sharing one name would lose the fresh epoch's data to that clear. context.documentId is the plain id when you need it.
ydoc
Doc
The Yjs document instance
context
Additional context: appId, userId, and the documentId, documentFormat and epoch the store name is for
Returns
YjsPersistenceProvider | null
A persistence provider instance, or null to disable persistence
Example
// Node.js with y-sqlite3
import { SqlitePersistence } from 'y-sqlite3';
// `storeName` already carries the epoch for a rotated large document, so a
// factory that keys its storage on it needs nothing else to be correct.
const factory: YjsPersistenceFactory = (storeName, ydoc, { appId, userId }) => {
return new SqlitePersistence(storeName, ydoc, {
dbPath: `~/.my-app/${appId}/${userId}/yjs.sqlite`
});
};