Skip to content

PresenceTracker

Defined in: collab-presence.ts:96

Tracks the local peer’s presence plus every remote peer’s last-known presence. Pure in-memory bookkeeping with a tiny event surface; transport is someone else’s job.

  • setLocal / patchLocal mutate the local peer and return a PresenceBroadcast to ship (the caller decides when/how to send it).
  • applyRemote integrates an inbound update, ignoring anything stale by clock, and emits join/update.
  • remove / prune retire peers and emit leave.
new PresenceTracker(self, options?): PresenceTracker;

Defined in: collab-presence.ts:105

ParameterType
selfPeer
optionsPresenceTrackerOptions

PresenceTracker

PropertyModifierTypeDefined in
selfreadonlyPeercollab-presence.ts:97
get local(): PeerPresence;

Defined in: collab-presence.ts:112

The local peer’s current presence (clock + lastSeen reflect the last setLocal).

PeerPresence


get size(): number;

Defined in: collab-presence.ts:237

number

allPeers(): PeerPresence[];

Defined in: collab-presence.ts:233

Every peer including the local one.

PeerPresence[]


applyRemote(broadcast): boolean;

Defined in: collab-presence.ts:152

Integrate a remote presence update. Updates with a clock at or below the one we already hold for that peer are ignored (stale / duplicate). The local peer’s own echoes are ignored. A newer clock carrying an unchanged state is treated as a liveness heartbeat: we refresh lastSeen (so TTL pruning keeps the peer) but report no change, so idle heartbeats never trigger a re-render. Returns true only when the visible state changed.

ParameterType
broadcastPresenceBroadcast

boolean


clear(): void;

Defined in: collab-presence.ts:247

Forget all remote peers without emitting (e.g. on disconnect/teardown).

void


get(peerId):
| PeerPresence
| undefined;

Defined in: collab-presence.ts:223

ParameterType
peerIdstring

| PeerPresence | undefined


has(peerId): boolean;

Defined in: collab-presence.ts:219

ParameterType
peerIdstring

boolean


mapSelections(mapper): boolean;

Defined in: collab-presence.ts:183

Remap every remote peer’s caret/selection through mapper. This keeps remote cursors tracking the text as the LOCAL document mutates beneath them — your own edits, or a third peer’s confirmed ops — in between that peer’s own presence broadcasts. A stored selection is an absolute {path, offset}; without remapping it silently drifts onto the wrong text after any concurrent edit, then visibly jumps when the peer next broadcasts.

mapper receives a peer’s current selection plus its Peer (so the caller can, say, skip ops that peer authored itself) and returns the transformed selection, or null to drop the caret (e.g. its block was deleted) until the peer broadcasts afresh. Peers with no selection are skipped.

The per-peer clock/lastSeen are deliberately left untouched: this is a local cosmetic transform, not a new update from the peer, so the peer’s next genuine broadcast (higher clock) still wins and re-syncs any drift. Stays ledger-agnostic — the caller supplies the position math. Returns true if any stored selection changed (so the caller can re-render).

ParameterType
mapper(selection, peer) => Selection | null

boolean


onChange(listener): () => void;

Defined in: collab-presence.ts:241

ParameterType
listenerPresenceListener

() => void


patchLocal(patch): PresenceBroadcast;

Defined in: collab-presence.ts:124

Merge a partial patch into the local presence. Bumps the local clock. Returns the value to broadcast.

ParameterType
patchPartial<PresenceState>

PresenceBroadcast


prune(at?): string[];

Defined in: collab-presence.ts:209

Drop remote peers whose last update is older than ttlMs. Returns the ids removed; emits leave for each. A no-op when ttlMs is 0.

ParameterType
atnumber

string[]


remotePeers(): PeerPresence[];

Defined in: collab-presence.ts:228

Remote peers only (what you typically render as other people’s cursors).

PeerPresence[]


remove(peerId): boolean;

Defined in: collab-presence.ts:197

Retire a remote peer (e.g. it sent bye). Emits leave.

ParameterType
peerIdstring

boolean


setLocal(state): PresenceBroadcast;

Defined in: collab-presence.ts:117

Replace the local presence wholesale. Bumps the local clock. Returns the value to broadcast.

ParameterType
statePresenceState

PresenceBroadcast


setLocalSelection(selection): PresenceBroadcast;

Defined in: collab-presence.ts:140

Convenience: set just the local caret/selection.

ParameterType
selectionSelection | null

PresenceBroadcast


touchLocal(): PresenceBroadcast;

Defined in: collab-presence.ts:134

Re-announce the current local presence with a fresh clock — a liveness heartbeat. Peers accept it (newer clock) and refresh their lastSeen for us, but since the state is unchanged it fires no update event on their side. Returns the value to broadcast.

PresenceBroadcast