From d55d9bdd3c8b695e9ccb1f28f2d15b9427f632d1 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 16 Jan 2024 18:56:14 +0100 Subject: [PATCH] fix: unsubscribe from RpcBrowser --- packages/frontend/src/stores/playground-queries.ts | 5 ++++- packages/shared/MessageProxy.ts | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/frontend/src/stores/playground-queries.ts b/packages/frontend/src/stores/playground-queries.ts index 0a1d0fb7c..f2a0dd33f 100644 --- a/packages/frontend/src/stores/playground-queries.ts +++ b/packages/frontend/src/stores/playground-queries.ts @@ -6,8 +6,11 @@ import { MSG_NEW_PLAYGROUND_QUERIES_STATE } from '@shared/Messages'; import { studioClient } from '/@/utils/client'; export const playgroundQueries: Readable = readable([], (set) => { - rpcBrowser.subscribe(MSG_NEW_PLAYGROUND_QUERIES_STATE, (msg) => { + const sub = rpcBrowser.subscribe(MSG_NEW_PLAYGROUND_QUERIES_STATE, (msg) => { set(msg); }); studioClient.askPlaygroundQueries(); + return () => { + sub.unsubscribe(); + } }); diff --git a/packages/shared/MessageProxy.ts b/packages/shared/MessageProxy.ts index 5fb5dcf37..b6374f9cc 100644 --- a/packages/shared/MessageProxy.ts +++ b/packages/shared/MessageProxy.ts @@ -83,6 +83,10 @@ export class RpcExtension { } } +export interface Subscriber { + unsubscribe(): void; +} + export class RpcBrowser { counter: number = 0; promises: Map any, reject: (value: unknown) => void}> = new Map(); @@ -166,10 +170,14 @@ export class RpcBrowser { }) } - // TODO(feloy) need to unsubscribe // TODO(feloy) need to subscribe several times? - subscribe(msgId: string, f: (msg: any) => void) { + subscribe(msgId: string, f: (msg: any) => void): Subscriber { this.subscribers.set(msgId, f); + return { + unsubscribe: () => { + this.subscribers.delete(msgId); + } + } } isSubscribedMessage(content: any): content is ISubscribedMessage {