update to handle resubscribe on reconnect

This commit is contained in:
2026-04-23 08:48:39 -05:00
parent 8945de00ec
commit 3178bc63eb
+12 -3
View File
@@ -1,16 +1,25 @@
import { WebSocketHandler } from './utils/websocket.js'; import { WebSocketHandler } from './utils/websocket.js';
export class TetherClient { export class TetherClient {
private websocketHandler: WebSocketHandler = new WebSocketHandler(); private websocketHandler: WebSocketHandler = new WebSocketHandler();
private subscribedQueries = new Map<string, (data: any) => void>(); private subscribedQueries = new Map<string, { callback: (data: any) => void, params: any }>();
connect = (url: string) => { connect = (url: string) => {
this.websocketHandler.startConnection(url); this.websocketHandler.startConnection(url);
this.websocketHandler.onQuery = (location, data) => { this.websocketHandler.onQuery = (location, data) => {
if (location) { if (location) {
const callback = this.subscribedQueries.get(location); const { callback } = this.subscribedQueries.get(location) || { callback: () => {} };
callback?.(data); callback?.(data);
} }
}; };
this.websocketHandler.onOpen = () => {
this.subscribedQueries.forEach(({ params }, queryName) => {
this.websocketHandler.send(JSON.stringify({
type: 'subscribe',
location: queryName,
params: params
}));
});
};
}; };
disconnect = () => { disconnect = () => {
@@ -18,7 +27,7 @@ export class TetherClient {
}; };
subscribe = (queryName: string, params: any, callback: (data: any) => void) => { subscribe = (queryName: string, params: any, callback: (data: any) => void) => {
this.subscribedQueries.set(queryName, callback); this.subscribedQueries.set(queryName, { callback, params });
this.websocketHandler.send(JSON.stringify({ this.websocketHandler.send(JSON.stringify({
type: 'subscribe', type: 'subscribe',
location: queryName, location: queryName,