import { type Heads } from '@automerge/automerge'; import type { DocumentId, PeerId } from '@automerge/automerge-repo'; import { Event } from '@dxos/async'; import { type Context, Resource } from '@dxos/context'; /** Whether the local replica of a document contains the change with the given hash; `false` marks it `advertised`. */ export type HasLocalChange = (documentId: DocumentId, changeHash: string) => boolean; export type CollectionSynchronizerProps = { sendCollectionState: (collectionId: string, peerId: PeerId, state: CollectionState) => void; queryCollectionState: (collectionId: string, peerId: PeerId) => void; shouldSyncCollection: (collectionId: string, peerId: PeerId) => boolean; /** Tells an ancestor head from a change worth a sync round. */ hasLocalChange?: HasLocalChange; }; /** * Implements collection sync protocol. */ export declare class CollectionSynchronizer extends Resource { private readonly _sendCollectionState; private readonly _queryCollectionState; private readonly _shouldSyncCollection; private readonly _hasLocalChange; /** * CollectionId -> State. */ private readonly _perCollectionStates; private readonly _activeCollections; private readonly _connectedPeers; /** Open sync span ids, by collection then peer. */ private readonly _syncSpans; /** Open sync spans with EDGE, by collection alone: each reconnect reaches EDGE under a new peer id. */ private readonly _edgeSyncSpans; /** The manual span registry is global, so ids must differ across synchronizers and across a pair's spans. */ private readonly _spanIdPrefix; private _syncSpanCount; readonly peerCollectionStateUpdated: Event<{ collectionId: string; peerId: PeerId; newDocsAppeared: boolean; }>; constructor(params: CollectionSynchronizerProps); protected _open(ctx: Context): Promise; protected _close(_ctx: Context): Promise; getRegisteredCollectionIds(): string[]; getLocalCollectionState(collectionId: string): CollectionState | undefined; setLocalCollectionState(collectionId: string, state: CollectionState): void; /** * Coalesce bursts of `setLocalCollectionState` (e.g. from `_onHeadsChanged` during an * import) into one broadcast microtask per collection. If another call arrives while * the microtask is pending, we just flag the collection dirty — the in-flight run * already reads the latest `localState`. If it arrives during the run itself, we * schedule exactly one follow-up. */ private _scheduleBroadcast; clearLocalCollectionState(collectionId: string): void; getRemoteCollectionStates(collectionId: string): ReadonlyMap; refreshCollection(collectionId: string): void; /** * Callback when a connection to a peer is established. */ onConnectionOpen(peerId: PeerId): void; /** * Callback when a connection to a peer is closed. */ onConnectionClosed(peerId: PeerId): void; /** * Callback when a peer queries the state of a collection. * * If we have no local state yet we silently drop the query; the peer will receive our * state via {@link _broadcastLocalState} once `setLocalCollectionState` is called. */ onCollectionStateQueried(collectionId: string, peerId: PeerId): void; /** * Callback when a peer sends the state of a collection. */ onRemoteStateReceived(collectionId: string, peerId: PeerId, state: CollectionState): void; /** True when the last recorded state for `peerId` has no outstanding work against our local state. */ private _isSyncedWithPeer; private _diffCollectionState; /** * Opens a span when a collection diverges from a peer; see {@link SYNC_SPAN_METHOD} for its dashboard. * Spans with EDGE are keyed by the collection, so one span covers a catch-up however many connections * it takes; spans with other peers, whose ids survive a reconnect, are keyed by the peer as well. */ private _startSyncSpan; private _endEdgeSyncSpan; private _endEdgeSyncSpans; private _endSyncSpan; private _endSyncSpans; private _getOrCreatePerCollectionState; /** * Push local state to interested peers whose last-known remote state differs from local * (or is unknown), then pull from peers via {@link refreshCollection}. * * Diff-gating avoids spamming peers that are already in sync; this matters because * {@link setLocalCollectionState} is called on every local heads change. */ private _broadcastLocalState; private _refreshInterestedPeers; } export type CollectionState = { /** * DocumentId -> Heads. */ readonly documents: Readonly>; }; export type CollectionStateDiff = { missingOnRemote: DocumentId[]; missingOnLocal: DocumentId[]; different: DocumentId[]; /** * In sync, since the heads overlap, but the remote advertises a change the local replica lacks: worth one sync * round in case it is new, never a divergence. */ advertised: DocumentId[]; }; export declare const isCollectionStateEqual: (left: CollectionState, right: CollectionState) => boolean; /** * Strip entries whose heads array is empty before sending a CollectionState * over the wire. Empty-heads entries are inert (they don't participate in the * diff thanks to `Record.filter` in {@link diffCollectionState}) but they * inflate the on-wire payload AND end up advertised as "I know about this * document with no commits" to the receiver — leaking sedimentree ids that * leaked into the local store from other spaces via the subduction * fingerprint exchange. Filtering at the send site is the cheapest place to * keep collection-state semantics aligned with `diffCollectionState`. */ export declare const withoutEmptyHeads: (state: CollectionState) => CollectionState; /** * Restrict a remote {@link CollectionState} to the document keys present in `local`. * * Used when diffing against an edge peer: edge subduction storage retains every * sedimentree id it has ever seen (old roots, partial creations, cross-space * fingerprint leaks), so a raw symmetric diff would surface those as * `missingOnLocal` forever. The local root's `links` are authoritative for * "what docs are in this space"; intersecting the remote view with the local * key set drops the noise without changing wire format. */ export declare const subsetRemoteToLocal: (local: CollectionState, remote: CollectionState) => CollectionState; /** * Peer-aware wrapper around {@link diffCollectionState}. * * For mesh peers the diff is symmetric (every doc on either side participates). * For edge peers we first intersect the remote state with the local key set — * see {@link subsetRemoteToLocal} for why. */ export declare const diffCollectionStateForPeer: (local: CollectionState, remote: CollectionState, { isEdgePeer, hasLocalChange }: { isEdgePeer: boolean; hasLocalChange?: HasLocalChange; }) => CollectionStateDiff; export type DiffCollectionStateOptions = { hasLocalChange?: HasLocalChange; /** Compares head sets exactly instead of applying the overlap rule. */ exact?: boolean; }; export declare const diffCollectionState: (local: CollectionState, remote: CollectionState, { hasLocalChange, exact }?: DiffCollectionStateOptions) => CollectionStateDiff; //# sourceMappingURL=collection-synchronizer.d.ts.map