mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
update to handle resubscribe on reconnect
This commit is contained in:
+12
-3
@@ -1,16 +1,25 @@
|
||||
import { WebSocketHandler } from './utils/websocket.js';
|
||||
export class TetherClient {
|
||||
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) => {
|
||||
this.websocketHandler.startConnection(url);
|
||||
this.websocketHandler.onQuery = (location, data) => {
|
||||
if (location) {
|
||||
const callback = this.subscribedQueries.get(location);
|
||||
const { callback } = this.subscribedQueries.get(location) || { callback: () => {} };
|
||||
callback?.(data);
|
||||
}
|
||||
};
|
||||
this.websocketHandler.onOpen = () => {
|
||||
this.subscribedQueries.forEach(({ params }, queryName) => {
|
||||
this.websocketHandler.send(JSON.stringify({
|
||||
type: 'subscribe',
|
||||
location: queryName,
|
||||
params: params
|
||||
}));
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
disconnect = () => {
|
||||
@@ -18,7 +27,7 @@ export class TetherClient {
|
||||
};
|
||||
|
||||
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({
|
||||
type: 'subscribe',
|
||||
location: queryName,
|
||||
|
||||
Reference in New Issue
Block a user