Skip to content

CRDTProvider

Defined in: packages/core/src/crdt-provider.ts:32

CRDTProvider is an interface with a set of methods for manipulating CRDT documents.

MessageType

The sync message that gets sent when changes are made to a document

DocType

The CRDT document type

ChangesType

A block of CRDT change(s)

ChangeFnType

A function for applying changes to a document

readonly optional actorBinding?: CRDTActorBinding<DocType, ChangesType>

Defined in: packages/core/src/crdt-provider.ts:39

Binds this provider’s actor identifiers to change signers. When present, ordinary documents sign every change, write under derived actors and reject a change that adds operations under an actor its signer does not own. Coordinator-backed documents do not use it.


readonly optional compactSnapshots?: boolean

Defined in: packages/core/src/crdt-provider.ts:197

Whether getSnapshot() encodes a document more compactly than getHistory(), as Automerge’s save() does. A local checkpoint then stores the document’s full state with getSnapshot() and restores it with applySnapshot().

optional applySnapshot(document, snapshot): DocType

Defined in: packages/core/src/crdt-provider.ts:189

Apply a full-state snapshot produced by getSnapshot().

Implement this when the snapshot format differs from incremental changes (e.g. Automerge’s save() output cannot be applied via applyChanges()). When not implemented, the caller falls back to remoteChange().

DocType

Current CRDT document.

ChangesType

Snapshot state from getSnapshot().

DocType

The updated document with the snapshot state merged in.


optional cloneDocument(document): DocType | undefined

Defined in: packages/core/src/crdt-provider.ts:70

Return a writable copy of document as it was when it was produced, even after a later localChange() or remoteChange() consumed it, or undefined when no faithful copy exists.

Implement this when those methods leave their input unusable, as Automerge marks it outdated. A local change that fails before it is stored and recorded in the change tree rolls back to this copy. The copy must make its next local change under a new replica identity, so the discarded change’s position is never reused. Return undefined when the copy would lose state, such as remote changes still waiting for their dependencies: the document instance is then retired instead. When not implemented, a failed change rolls back to document itself if localChange() returned a new document. A provider whose localChange() returns the document it was given edits in place and cannot roll back, so the instance is retired.

DocType

A document this provider returned before the failed change.

DocType | undefined


optional coversVersion(document, version): boolean

Defined in: packages/core/src/crdt-provider.ts:248

Report whether document holds all of the state that version, from encodeVersion() on another replica, describes. Every insertion and deletion counts, so a replica that lacks a deletion is not covered. Return false, without throwing, for a malformed version.

DocType

CRDT document to inspect.

Uint8Array

Bytes from another replica’s encodeVersion().

boolean


optional createDocumentSession(document): CRDTDocumentSession<DocType, ChangesType, ChangeFnType>

Defined in: packages/core/src/crdt-provider.ts:42

Required by coordinator-backed documents; never falls back to live preparation.

DocType

CRDTDocumentSession<DocType, ChangesType, ChangeFnType>


optional encodeVersion(document): Uint8Array

Defined in: packages/core/src/crdt-provider.ts:237

Encode a compact version of document’s state, such as Automerge heads, a Loro version vector or a Yjs state vector with its delete set. A writer whose catch-up reply does not fit a frame sends it, and the requester loads only when coversVersion() reports that it lacks some of that state. When not implemented, the requester always loads.

DocType

CRDT document to describe.

Uint8Array


optional getChangesSinceSnapshot(document, snapshot): ChangesType | undefined

Defined in: packages/core/src/crdt-provider.ts:212

Encode the changes document holds beyond snapshot, a state from getSnapshot() without an actor manifest, so that remoteChange() applies them to a new document that snapshot was applied to. Return undefined when document does not hold every change in snapshot, or when applying the changes would take longer than restoring the full state. A local checkpoint stores a document that its latest snapshot covers in part as that snapshot and these changes, so it holds the state once.

DocType

CRDT document to encode.

ChangesType

State that getSnapshot() returned for an earlier state.

ChangesType | undefined


getHistory(document): ChangesType

Defined in: packages/core/src/crdt-provider.ts:162

Gets all changes for the document.

DocType

CRDT document to inspect.

ChangesType


optional getSnapshot(document): ChangesType

Defined in: packages/core/src/crdt-provider.ts:176

Get a compacted state snapshot of the document. Used for onboarding new members without replaying individual changes.

For Yjs: Y.encodeStateAsUpdateV2(doc) (directly applicable via remoteChange()) For Automerge: Automerge.save(doc) (requires applySnapshot() since save format differs from the incremental change format used by remoteChange())

DocType

CRDT document to snapshot.

ChangesType

A snapshot representation. If the snapshot format differs from the incremental change format, implement applySnapshot() as well.


optional hasDiscarded(document, changes): boolean

Defined in: packages/core/src/crdt-provider.ts:155

Report whether document no longer holds changes, which remoteChange() buffered while a dependency was missing, and will never integrate them. A provider that validates buffered changes once their dependencies arrive, or evicts them, implements this. Peerborne then stops waiting for the change, as when it evicts a waiting change itself, so a later delivery passes it to remoteChange() again. It runs for a waiting change when it is delivered again and, after a change integrates, for each change still waiting. When not implemented, a buffered change waits until it integrates or Peerborne evicts it.

DocType

CRDT document returned by remoteChange().

ChangesType

Changes previously passed to remoteChange().

boolean


optional hasIntegrated(document, changes): boolean

Defined in: packages/core/src/crdt-provider.ts:139

Report whether document has integrated every change in changes.

Implement this when remoteChange() can buffer changes until their causal dependencies arrive and buffered changes are missing from getHistory(). Peerborne records a remote change as applied only after this returns true, so a buffered change stays eligible for redelivery. It runs for every delivered change and, after a change integrates, for each change still waiting, of which Peerborne keeps at most 4,096 per document. Those checks pass the same changes object, so cache per-change decoding by it. When not implemented, every applied change counts as integrated.

DocType

CRDT document returned by remoteChange().

ChangesType

Changes previously passed to remoteChange().

boolean


optional hasPendingChanges(document): boolean

Defined in: packages/core/src/crdt-provider.ts:226

Report whether document holds remote changes that wait for missing causal dependencies, such as Automerge’s queued changes or Yjs pending structs and deletions. Automatic catch-up asks peers for missed updates while this is true. When not implemented, only failed block fetches and advertised heads reveal a gap.

DocType

CRDT document to inspect.

boolean


localChange(document, message, changeFn): [DocType, ChangesType]

Defined in: packages/core/src/crdt-provider.ts:84

Apply locally made changes to provided CRDT document as defined by changeFn.

If changeFn throws, leave document without its edits and rethrow the error. A provider that edits in place and cannot roll back throws PartialLocalChangeError instead when the callback edited anything.

DocType

CRDT document to change.

string

A description of the change made.

ChangeFnType

A function that modifies a CRDT document (provided as a parameter). This function will be run as a part of .localChange(...)

[DocType, ChangesType]


newDocument(): DocType

Defined in: packages/core/src/crdt-provider.ts:49

Create a new empty (contains the equivalent of {}) CRDT document.

DocType


remoteChange(document, changes): DocType

Defined in: packages/core/src/crdt-provider.ts:96

Apply CRDT document changes made by a remote peer.

DocType

CRDT document to update.

ChangesType

An object describing changes made by a peer. CRDT-specific.

DocType


optional remoteChangeBatch(document, batch): readonly [DocType, number] | undefined

Defined in: packages/core/src/crdt-provider.ts:118

Apply the remote changes of consecutive frames in one step. batch holds each frame’s change sets in delivery order. Apply the longest leading run of frames whose changes all integrate once the frames before them have, without letting any change buffered in document integrate, and return a new document with the number of frames applied. A frame without changes always applies, so [[]] copies document. The result must equal the document remoteChange() would produce from those sets one after another and keep the replica identity of document, which stays usable and unchanged. Return undefined when no frame applies or applying them fails.

Implement this when remoteChange() costs more than the changes it applies, as Automerge’s walks every object it edits, so a burst of frames costs about one walk. Peerborne verifies each frame before batching it and applies frames one by one when this is not implemented.

DocType

CRDT document to update.

readonly (readonly ChangesType[])[]

The change sets of each frame, in delivery order.

readonly [DocType, number] | undefined