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 17, 2024
1 parent f71ddd3 commit 48870fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/frontend/src/pages/ModelPlayground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
studioClient.startPlayground(model.id);
playgroundQueries.subscribe((queries: QueryState[]) => {
const unsubscribe = playgroundQueries.subscribe((queries: QueryState[]) => {
if (queryId === -1) {
return;
}
Expand All @@ -32,6 +32,10 @@
}
displayQuery(myQuery);
});
return () => {
unsubscribe();
};
});
function displayQuery(query: QueryState) {
Expand Down
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();
}
});
14 changes: 12 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,16 @@ 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 {
console.log('add subscriber');
this.subscribers.set(msgId, f);
return {
unsubscribe: () => {
console.log('delete subscriber');
this.subscribers.delete(msgId);
}
}
}

isSubscribedMessage(content: any): content is ISubscribedMessage {
Expand Down

0 comments on commit 48870fa

Please sign in to comment.