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';
|
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user