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/patchLocalmutate the local peer and return aPresenceBroadcastto ship (the caller decides when/how to send it).applyRemoteintegrates an inbound update, ignoring anything stale byclock, and emitsjoin/update.remove/pruneretire peers and emitleave.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new PresenceTracker(self, options?): PresenceTracker;Defined in: collab-presence.ts:105
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
self | Peer |
options | PresenceTrackerOptions |
Returns
Section titled “Returns”PresenceTracker
Properties
Section titled “Properties”| Property | Modifier | Type | Defined in |
|---|---|---|---|
self | readonly | Peer | collab-presence.ts:97 |
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get local(): PeerPresence;Defined in: collab-presence.ts:112
The local peer’s current presence (clock + lastSeen reflect the last setLocal).
Returns
Section titled “Returns”Get Signature
Section titled “Get Signature”get size(): number;Defined in: collab-presence.ts:237
Returns
Section titled “Returns”number
Methods
Section titled “Methods”allPeers()
Section titled “allPeers()”allPeers(): PeerPresence[];Defined in: collab-presence.ts:233
Every peer including the local one.
Returns
Section titled “Returns”applyRemote()
Section titled “applyRemote()”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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
broadcast | PresenceBroadcast |
Returns
Section titled “Returns”boolean
clear()
Section titled “clear()”clear(): void;Defined in: collab-presence.ts:247
Forget all remote peers without emitting (e.g. on disconnect/teardown).
Returns
Section titled “Returns”void
get(peerId): | PeerPresence | undefined;Defined in: collab-presence.ts:223
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
peerId | string |
Returns
Section titled “Returns”| PeerPresence
| undefined
has(peerId): boolean;Defined in: collab-presence.ts:219
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
peerId | string |
Returns
Section titled “Returns”boolean
mapSelections()
Section titled “mapSelections()”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).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
mapper | (selection, peer) => Selection | null |
Returns
Section titled “Returns”boolean
onChange()
Section titled “onChange()”onChange(listener): () => void;Defined in: collab-presence.ts:241
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
listener | PresenceListener |
Returns
Section titled “Returns”() => void
patchLocal()
Section titled “patchLocal()”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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
patch | Partial<PresenceState> |
Returns
Section titled “Returns”prune()
Section titled “prune()”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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
at | number |
Returns
Section titled “Returns”string[]
remotePeers()
Section titled “remotePeers()”remotePeers(): PeerPresence[];Defined in: collab-presence.ts:228
Remote peers only (what you typically render as other people’s cursors).
Returns
Section titled “Returns”remove()
Section titled “remove()”remove(peerId): boolean;Defined in: collab-presence.ts:197
Retire a remote peer (e.g. it sent bye). Emits leave.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
peerId | string |
Returns
Section titled “Returns”boolean
setLocal()
Section titled “setLocal()”setLocal(state): PresenceBroadcast;Defined in: collab-presence.ts:117
Replace the local presence wholesale. Bumps the local clock. Returns the value to broadcast.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
state | PresenceState |
Returns
Section titled “Returns”setLocalSelection()
Section titled “setLocalSelection()”setLocalSelection(selection): PresenceBroadcast;Defined in: collab-presence.ts:140
Convenience: set just the local caret/selection.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
selection | Selection | null |
Returns
Section titled “Returns”touchLocal()
Section titled “touchLocal()”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.