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
@@ -5,6 +5,12 @@ export class TetherClient {
connect = (url: string) => {
this.websocketHandler.startConnection(url);
this.websocketHandler.onQuery = (location, data) => {
if (location) {
const callback = this.subscribedQueries.get(location);
callback?.(data);
}
};
};
disconnect = () => {
+5 -9
View File
@@ -1,9 +1,9 @@
export class WebSocketHandler {
private ws: WebSocket | null = null;
private url: string = '';
private subscribedQueries = new Map<string, (data: any) => void>();
private onOpen: () => void = () => {};
private onClose: () => void = () => {};
public onOpen: () => void = () => {};
public onQuery: (location: string | undefined, data: any) => void = () => {};
public onClose: () => void = () => {};
private reconnectAttempts: number = 0;
private maxReconnectAttempts: number = 5;
private reconnectInterval: number = 1000;
@@ -27,16 +27,12 @@ export class WebSocketHandler {
ws.onmessage = (event: MessageEvent) => {
const data = JSON.parse(String(event.data)) as {
type: string;
query?: string;
location?: string;
data?: unknown;
error?: string;
};
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);
}