mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
pass callbacks to client
This commit is contained in:
Vendored
+6
@@ -4,6 +4,12 @@ export class TetherClient {
|
|||||||
subscribedQueries = new Map();
|
subscribedQueries = new Map();
|
||||||
connect = (url) => {
|
connect = (url) => {
|
||||||
this.websocketHandler.startConnection(url);
|
this.websocketHandler.startConnection(url);
|
||||||
|
this.websocketHandler.onQuery = (location, data) => {
|
||||||
|
if (location) {
|
||||||
|
const callback = this.subscribedQueries.get(location);
|
||||||
|
callback?.(data);
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
disconnect = () => {
|
disconnect = () => {
|
||||||
this.websocketHandler.close();
|
this.websocketHandler.close();
|
||||||
|
|||||||
Vendored
+3
-3
@@ -1,9 +1,9 @@
|
|||||||
export declare class WebSocketHandler {
|
export declare class WebSocketHandler {
|
||||||
private ws;
|
private ws;
|
||||||
private url;
|
private url;
|
||||||
private subscribedQueries;
|
onOpen: () => void;
|
||||||
private onOpen;
|
onQuery: (location: string | undefined, data: any) => void;
|
||||||
private onClose;
|
onClose: () => void;
|
||||||
private reconnectAttempts;
|
private reconnectAttempts;
|
||||||
private maxReconnectAttempts;
|
private maxReconnectAttempts;
|
||||||
private reconnectInterval;
|
private reconnectInterval;
|
||||||
|
|||||||
Vendored
+2
-6
@@ -1,8 +1,8 @@
|
|||||||
export class WebSocketHandler {
|
export class WebSocketHandler {
|
||||||
ws = null;
|
ws = null;
|
||||||
url = '';
|
url = '';
|
||||||
subscribedQueries = new Map();
|
|
||||||
onOpen = () => { };
|
onOpen = () => { };
|
||||||
|
onQuery = () => { };
|
||||||
onClose = () => { };
|
onClose = () => { };
|
||||||
reconnectAttempts = 0;
|
reconnectAttempts = 0;
|
||||||
maxReconnectAttempts = 5;
|
maxReconnectAttempts = 5;
|
||||||
@@ -24,11 +24,7 @@ export class WebSocketHandler {
|
|||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
const data = JSON.parse(String(event.data));
|
const data = JSON.parse(String(event.data));
|
||||||
if (data.type === 'query') {
|
if (data.type === 'query') {
|
||||||
this.subscribedQueries.forEach((callback, query) => {
|
this.onQuery(data.location, data.data);
|
||||||
if (data.query === query) {
|
|
||||||
callback(data.data);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else if (data.type === 'error') {
|
else if (data.type === 'error') {
|
||||||
console.error(data.error);
|
console.error(data.error);
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ export class TetherClient {
|
|||||||
|
|
||||||
connect = (url: string) => {
|
connect = (url: string) => {
|
||||||
this.websocketHandler.startConnection(url);
|
this.websocketHandler.startConnection(url);
|
||||||
|
this.websocketHandler.onQuery = (location, data) => {
|
||||||
|
if (location) {
|
||||||
|
const callback = this.subscribedQueries.get(location);
|
||||||
|
callback?.(data);
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
disconnect = () => {
|
disconnect = () => {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
export class WebSocketHandler {
|
export class WebSocketHandler {
|
||||||
private ws: WebSocket | null = null;
|
private ws: WebSocket | null = null;
|
||||||
private url: string = '';
|
private url: string = '';
|
||||||
private subscribedQueries = new Map<string, (data: any) => void>();
|
public onOpen: () => void = () => {};
|
||||||
private onOpen: () => void = () => {};
|
public onQuery: (location: string | undefined, data: any) => void = () => {};
|
||||||
private onClose: () => void = () => {};
|
public onClose: () => void = () => {};
|
||||||
private reconnectAttempts: number = 0;
|
private reconnectAttempts: number = 0;
|
||||||
private maxReconnectAttempts: number = 5;
|
private maxReconnectAttempts: number = 5;
|
||||||
private reconnectInterval: number = 1000;
|
private reconnectInterval: number = 1000;
|
||||||
@@ -27,16 +27,12 @@ export class WebSocketHandler {
|
|||||||
ws.onmessage = (event: MessageEvent) => {
|
ws.onmessage = (event: MessageEvent) => {
|
||||||
const data = JSON.parse(String(event.data)) as {
|
const data = JSON.parse(String(event.data)) as {
|
||||||
type: string;
|
type: string;
|
||||||
query?: string;
|
location?: string;
|
||||||
data?: unknown;
|
data?: unknown;
|
||||||
error?: string;
|
error?: string;
|
||||||
};
|
};
|
||||||
if (data.type === 'query') {
|
if (data.type === 'query') {
|
||||||
this.subscribedQueries.forEach((callback, query) => {
|
this.onQuery(data.location, data.data);
|
||||||
if (data.query === query) {
|
|
||||||
callback(data.data);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (data.type === 'error') {
|
} else if (data.type === 'error') {
|
||||||
console.error(data.error);
|
console.error(data.error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user