Skip to content

Commit

Permalink
fix: unsubscribe from RpcBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jan 16, 2024
1 parent 40aaf8c commit d55d9bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/frontend/src/stores/playground-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { MSG_NEW_PLAYGROUND_QUERIES_STATE } from '@shared/Messages';
import { studioClient } from '/@/utils/client';

export const playgroundQueries: Readable<QueryState[]> = readable<QueryState[]>([], (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();
}
});
12 changes: 10 additions & 2 deletions packages/shared/MessageProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class RpcExtension {
}
}

export interface Subscriber {
unsubscribe(): void;
}

export class RpcBrowser {
counter: number = 0;
promises: Map<number, {resolve: (value: unknown) => any, reject: (value: unknown) => void}> = new Map();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d55d9bd

Please sign in to comment.