bindCrdt
testedWire a {@link CrdtProvider} to an {@link Editor}: local transactions flow into the CRDT, and remote ops are reflected back as a single history-bypassing `setDoc` transaction. The provider's `onLocalOps`/`applyUpdate` are connected to a transport by the caller.
Signature
export function bindCrdt(editor: Editor, provider: CrdtProvider): CrdtBinding{ ... }Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
editor | Editor | — | — |
provider | CrdtProvider | — | — |
Returns
CrdtBindingRelated Types
PointAnchor
A caret point anchored to the character op id it sits after (stable under remote edits).
interface PointAnchor| Property | Type | Default | Description |
|---|---|---|---|
blockIdreadonly | string | — | — |
afterCharIdreadonly | any | — | — |
AwarenessState
Ephemeral per-client presence (cursor + identity), sent over the awareness channel.
interface AwarenessState| Property | Type | Default | Description |
|---|---|---|---|
clientIdreadonly | string | — | — |
user?readonly | CursorUser | undefined | — | — |
anchorreadonly | SelectionAnchor | null | — | — |
RemoteCursor
A remote participant's cursor, resolved back into local model coordinates.
interface RemoteCursor| Property | Type | Default | Description |
|---|---|---|---|
clientIdreadonly | string | — | — |
user?readonly | CursorUser | undefined | — | — |
selectionreadonly | Selection | null | — | — |
CrdtProvider
A pluggable CRDT backend. The editor core stays CRDT-agnostic behind this interface; {@link bindCrdt} wires it to an {@link Editor}, and any transport (BroadcastChannel, WebSocket, …) is layered on via the op + awareness hooks.
interface CrdtProvider| Property | Type | Default | Description |
|---|---|---|---|
namereadonly | string | — | — |
load | () => EditorDocument | — | The current document materialized from CRDT state. |
applyLocal | (tr: Transaction) => void | — | Translate a local transaction's steps into CRDT ops and apply them. |
applyUpdate | (bytes: Uint8Array, origin?: unknown) => void | — | Merge a remote update (encoded ops) into the CRDT. |
encodeStateVector | () => Uint8Array | — | Encode this replica's version vector for a sync handshake. |
encodeDelta | (remoteStateVector?: Uint8Array) => Uint8Array | — | Encode ops a remote is missing (by its state vector); omit for a full snapshot. |
onLocalOps | (listener: (bytes: Uint8Array) => void) => () => void | — | Subscribe to locally-produced op batches (to broadcast). Returns unsubscribe. |
onRemoteApplied | (listener: () => void) => () => void | — | Subscribe to "remote ops were applied" (to reflect into editor state). Returns unsubscribe. |
setLocalSelection | (selection: Selection | null) => void | — | Publish the local selection as presence (anchored to char ids). |
onLocalAwareness | (listener: (bytes: Uint8Array) => void) => () => void | — | Subscribe to locally-produced awareness frames (to broadcast). Returns unsubscribe. |
applyAwareness | (bytes: Uint8Array) => void | — | Merge a remote awareness frame. |
onAwareness | (listener: (cursors: RemoteCursor[]) => void) => () => void | — | Subscribe to the resolved set of remote cursors. Returns unsubscribe. |
gc | (stableStateVector?: Uint8Array) => { blocks: number; chars: number; } | — | Compact tombstones and removed blocks covered by `stableStateVector` (or this replica's version when omitted). Safe only at quiescence — all peers fully synced, nothing in flight. Returns how much was dropped. |
destroy | () => void | — | — |
SelectionAnchor
A selection anchored to char ids rather than offsets, for awareness.
export type SelectionAnchor
= | { readonly kind: 'text'; readonly anchor: PointAnchor; readonly focus: PointAnchor }
| { readonly kind: 'node'; readonly ids: readonly string[] };