Skip to content

Commit

Permalink
fix: Proper unsubscribing from websockets (#2838)
Browse files Browse the repository at this point in the history
  • Loading branch information
StaNov authored Jan 14, 2025
1 parent 45ff869 commit 55ee489
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion webapp/src/websocket-client/WebsocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => {

let _client: CompatClient | undefined;
let connected = false;
const subscriptions: Subscription<any>[] = [];
let subscriptions: Subscription<any>[] = [];

const resubscribe = () => {
if (_client) {
Expand Down Expand Up @@ -79,6 +79,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => {
subscriptions.forEach((s) => {
s.unsubscribe = undefined;
s.id = undefined;
removeSubscription(s);
});
options.onConnectionClose?.();
};
Expand Down Expand Up @@ -120,6 +121,7 @@ export const WebsocketClient = (options: TranslationsClientOptions) => {

return () => {
subscription.unsubscribe?.();
removeSubscription(subscription);
};
}

Expand All @@ -129,6 +131,10 @@ export const WebsocketClient = (options: TranslationsClientOptions) => {
}
}

function removeSubscription(subscription: Subscription<any>) {
subscriptions = subscriptions.filter((it) => it !== subscription);
}

return Object.freeze({ subscribe, disconnect });
};

Expand Down

0 comments on commit 55ee489

Please sign in to comment.