Skip to content

Commit

Permalink
Update WebSocketClient.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlantisPleb committed Feb 22, 2025
1 parent aa61e55 commit ea704d7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions frontend/app/lib/agentsync/hooks/WebSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface WebSocketMessage {

export class WebSocketClient {
private ws: WebSocket | null = null;
private messageHandlers: ((msg: WebSocketMessage) => void)[] = [];
private messageHandlers: Set<(msg: WebSocketMessage) => void> = new Set();
private reconnectAttempts = 0;
private maxReconnectAttempts = 5;
private reconnectTimeout: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -128,13 +128,18 @@ export class WebSocketClient {
this.ws = null;
}

this.messageHandlers = [];
this.messageHandlers.clear();
this.messageQueue = [];
this.isConnecting = false;
this.reconnectAttempts = 0;
console.debug("WebSocket client disconnected");
}

clearHandlers() {
console.debug("Clearing message handlers");
this.messageHandlers.clear();
}

send(msg: any) {
if (this.isClosed) {
console.debug("Cannot send message - client is closed");
Expand All @@ -159,9 +164,9 @@ export class WebSocketClient {
}

onMessage(handler: (msg: WebSocketMessage) => void) {
this.messageHandlers.push(handler);
this.messageHandlers.add(handler);
return () => {
this.messageHandlers = this.messageHandlers.filter((h) => h !== handler);
this.messageHandlers.delete(handler);
};
}
}

0 comments on commit ea704d7

Please sign in to comment.