From 55ee4898e40fe0e4dee1da78b32f123d98f46fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Nov=C3=A1k?= Date: Tue, 14 Jan 2025 11:10:48 +0100 Subject: [PATCH] fix: Proper unsubscribing from websockets (#2838) --- webapp/src/websocket-client/WebsocketClient.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/webapp/src/websocket-client/WebsocketClient.ts b/webapp/src/websocket-client/WebsocketClient.ts index f1a2bde14b..d438eb0697 100644 --- a/webapp/src/websocket-client/WebsocketClient.ts +++ b/webapp/src/websocket-client/WebsocketClient.ts @@ -33,7 +33,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => { let _client: CompatClient | undefined; let connected = false; - const subscriptions: Subscription[] = []; + let subscriptions: Subscription[] = []; const resubscribe = () => { if (_client) { @@ -79,6 +79,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => { subscriptions.forEach((s) => { s.unsubscribe = undefined; s.id = undefined; + removeSubscription(s); }); options.onConnectionClose?.(); }; @@ -120,6 +121,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => { return () => { subscription.unsubscribe?.(); + removeSubscription(subscription); }; } @@ -129,6 +131,10 @@ export const WebsocketClient = (options: TranslationsClientOptions) => { } } + function removeSubscription(subscription: Subscription) { + subscriptions = subscriptions.filter((it) => it !== subscription); + } + return Object.freeze({ subscribe, disconnect }); };