feat(companion): add disconnect metadata to connection snapshots

This commit is contained in:
William Valentin
2026-02-16 23:28:01 -08:00
parent 44916fc9b2
commit 44b686da9c
6 changed files with 104 additions and 2 deletions
+12 -1
View File
@@ -66,6 +66,8 @@ export interface ConnectionSnapshot {
pendingEventWaitCount: number;
hasPendingWork: boolean;
idle: boolean;
lastDisconnectCode?: number;
lastDisconnectReason?: string;
}
export interface ClearEventSubscriptionsResult {
@@ -313,6 +315,8 @@ export class CompanionRuntimeClient {
private pending = new Map<number, PendingRequest>();
private readonly eventHandlers = new Set<CompanionEventHandler>();
private readonly pendingEventWaits = new Set<(error: Error) => void>();
private lastDisconnectCode: number | undefined;
private lastDisconnectReason: string | undefined;
constructor(options: CompanionRuntimeClientOptions) {
const requestTimeoutMs = options.requestTimeoutMs ?? 15_000;
@@ -374,6 +378,8 @@ export class CompanionRuntimeClient {
pendingEventWaitCount: this.pendingEventWaitCount,
hasPendingWork: this.hasPendingWork,
idle: this.idle,
lastDisconnectCode: this.lastDisconnectCode,
lastDisconnectReason: this.lastDisconnectReason,
};
}
@@ -405,7 +411,10 @@ export class CompanionRuntimeClient {
settled = true;
this.ws = ws;
this.ws.on('message', (raw) => this.handleMessage(raw.toString()));
this.ws.on('close', () => {
this.ws.on('close', (code, reason) => {
this.lastDisconnectCode = code;
const reasonText = reason?.toString().trim();
this.lastDisconnectReason = reasonText ? reasonText : undefined;
if (this.ws === ws) {
this.ws = null;
}
@@ -445,6 +454,8 @@ export class CompanionRuntimeClient {
}
disconnect(code?: number, reason?: string): void {
this.lastDisconnectCode = code;
this.lastDisconnectReason = reason;
if (!this.ws) {
this.rejectEventWaits(new Error('Disconnected'));
return;