Skip to content

lspeasy / client/src / ConnectionHealthTracker

Class: ConnectionHealthTracker

Defined in: packages/client/src/connection/health.ts:35

Tracks connection state transitions and message activity timestamps.

Remarks

ConnectionHealthTracker is created internally by LSPClient and exposed via client.connectionHealth. Subscribe to stateChanged and healthChanged events to react to disconnections or transport failures.

Use When

You need to monitor connection liveness — for example, to show a status indicator, trigger reconnection logic, or surface transport errors to users.

See

HeartbeatMonitor for detecting silent transport failures with periodic ping/pong checks.

Never

NEVER mutate the object returned by getHealth() — it is a defensive copy but consumers that store a reference and then modify it will see stale state.

NEVER call setState from outside LSPClient internals. External callers have no knowledge of the full state-transition graph; setting state directly can desync the client's internal bookkeeping from the tracker's reported state.

Extends

Constructors

Constructor

ts
new ConnectionHealthTracker(): ConnectionHealthTracker;

Returns

ConnectionHealthTracker

Inherited from

ts
DisposableEventEmitter<HealthEventMap>.constructor

Methods

getHealth()

ts
getHealth(): ConnectionHealth;

Defined in: packages/client/src/connection/health.ts:48

Returns a defensive copy of the current health snapshot.

Returns

ConnectionHealth

A shallow copy of the current ConnectionHealth object, including a nested copy of heartbeat when present.


markMessageReceived()

ts
markMessageReceived(): void;

Defined in: packages/client/src/connection/health.ts:107

Records inbound message activity.

Returns

void


markMessageSent()

ts
markMessageSent(): void;

Defined in: packages/client/src/connection/health.ts:96

Records outbound message activity.

Returns

void


onHealthChange()

ts
onHealthChange(handler): () => void;

Defined in: packages/client/src/connection/health.ts:157

Subscribes to health snapshot updates.

Parameters

ParameterTypeDescription
handler(health) => voidCallback invoked with the latest ConnectionHealth snapshot after any state or activity change.

Returns

An unsubscribe function — call it to remove the listener.

() => void


onStateChange()

ts
onStateChange(handler): () => void;

Defined in: packages/client/src/connection/health.ts:145

Subscribes to connection state transitions.

Parameters

ParameterTypeDescription
handler(event) => voidCallback invoked with a StateChangeEvent each time the connection state changes.

Returns

An unsubscribe function — call it to remove the listener.

() => void


setHeartbeat()

ts
setHeartbeat(status): void;

Defined in: packages/client/src/connection/health.ts:130

Updates the heartbeat subsection of the current health snapshot.

Parameters

ParameterType
statusHeartbeatStatus

Returns

void


setState()

ts
setState(next, reason?): void;

Defined in: packages/client/src/connection/health.ts:73

Updates connection state and emits state/health change events.

Parameters

ParameterTypeDescription
nextConnectionStateThe new ConnectionState to transition to.
reason?stringOptional human-readable reason for the state transition (e.g. 'Transport error'). Included in the emitted StateChangeEvent.

Returns

void

See

  • ConnectionState for valid state values.
  • ConnectionHealthTracker for the full class contract.

Released under the MIT License.