pass callbacks to client

This commit is contained in:
2026-04-21 22:37:35 -05:00
parent 89d2b414f9
commit 5202a7f844
5 changed files with 22 additions and 18 deletions
+6
View File
@@ -4,6 +4,12 @@ export class TetherClient {
subscribedQueries = new Map();
connect = (url) => {
this.websocketHandler.startConnection(url);
this.websocketHandler.onQuery = (location, data) => {
if (location) {
const callback = this.subscribedQueries.get(location);
callback?.(data);
}
};
};
disconnect = () => {
this.websocketHandler.close();
+3 -3
View File
@@ -1,9 +1,9 @@
export declare class WebSocketHandler {
private ws;
private url;
private subscribedQueries;
private onOpen;
private onClose;
onOpen: () => void;
onQuery: (location: string | undefined, data: any) => void;
onClose: () => void;
private reconnectAttempts;
private maxReconnectAttempts;
private reconnectInterval;
+2 -6
View File
@@ -1,8 +1,8 @@
export class WebSocketHandler {
ws = null;
url = '';
subscribedQueries = new Map();
onOpen = () => { };
onQuery = () => { };
onClose = () => { };
reconnectAttempts = 0;
maxReconnectAttempts = 5;
@@ -24,11 +24,7 @@ export class WebSocketHandler {
ws.onmessage = (event) => {
const data = JSON.parse(String(event.data));
if (data.type === 'query') {
this.subscribedQueries.forEach((callback, query) => {
if (data.query === query) {
callback(data.data);
}
});
this.onQuery(data.location, data.data);
}
else if (data.type === 'error') {
console.error(data.error);