separation of concerns

This commit is contained in:
2026-04-21 22:26:47 -05:00
parent 00eeff1282
commit 89d2b414f9
6 changed files with 169 additions and 76 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
export declare class TetherClient {
private ws;
private websocketHandler;
private subscribedQueries;
connect: (url: string) => void;
disconnect: () => void;
+7 -38
View File
@@ -1,60 +1,29 @@
import { WebSocketHandler } from './utils/websocket.js';
export class TetherClient {
ws = null;
websocketHandler = new WebSocketHandler();
subscribedQueries = new Map();
connect = (url) => {
this.ws = new WebSocket(url);
this.ws.onopen = () => {
console.log('Connected to Tether');
};
this.ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'query') {
this.subscribedQueries.forEach((callback, query) => {
if (data.query === query) {
callback(data.data);
}
});
}
else if (data.type === 'error') {
console.error(data.error);
}
};
this.ws.onclose = () => {
console.log('Disconnected from Tether');
};
this.websocketHandler.startConnection(url);
};
disconnect = () => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Not connected to Tether');
}
this.ws.close();
this.ws = null;
this.websocketHandler.close();
};
subscribe = (query, callback) => {
this.subscribedQueries.set(query, callback);
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Not connected to Tether');
}
this.ws.send(JSON.stringify({
this.websocketHandler.send(JSON.stringify({
type: 'subscribe',
query: query
}));
};
unsubscribe = (query) => {
this.subscribedQueries.delete(query);
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Not connected to Tether');
}
this.ws.send(JSON.stringify({
this.websocketHandler.send(JSON.stringify({
type: 'unsubscribe',
query: query
}));
};
sendMutation = (mutationName, params) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Not connected to Tether');
}
this.ws.send(JSON.stringify({
this.websocketHandler.send(JSON.stringify({
type: 'mutation',
name: mutationName,
payload: params,
+15
View File
@@ -0,0 +1,15 @@
export declare class WebSocketHandler {
private ws;
private url;
private subscribedQueries;
private onOpen;
private onClose;
private reconnectAttempts;
private maxReconnectAttempts;
private reconnectInterval;
private sendQueue;
startConnection: (url: string) => void;
attemptReconnect: () => void;
close: () => void;
send: (message: string) => void;
}
+64
View File
@@ -0,0 +1,64 @@
export class WebSocketHandler {
ws = null;
url = '';
subscribedQueries = new Map();
onOpen = () => { };
onClose = () => { };
reconnectAttempts = 0;
maxReconnectAttempts = 5;
reconnectInterval = 1000;
sendQueue = [];
startConnection = (url) => {
this.url = url;
this.ws = new WebSocket(url);
const ws = this.ws;
ws.onopen = () => {
console.log('Connected to Tether');
this.onOpen();
if (this.sendQueue.length > 0) {
this.sendQueue.forEach(message => this.ws?.send(message));
this.sendQueue = [];
}
this.reconnectAttempts = 0;
};
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);
}
});
}
else if (data.type === 'error') {
console.error(data.error);
}
};
ws.onclose = () => {
console.log('Disconnected from Tether');
this.attemptReconnect();
};
};
attemptReconnect = () => {
this.ws?.close();
this.reconnectAttempts++;
if (this.reconnectAttempts > this.maxReconnectAttempts) {
console.error('Max reconnect attempts reached');
return;
}
setTimeout(() => {
this.startConnection(this.url);
}, this.reconnectInterval);
};
close = () => {
this.ws?.close();
this.ws = null;
};
send = (message) => {
if (this.ws?.readyState !== WebSocket.OPEN) {
this.sendQueue.push(message);
return;
}
this.ws?.send(message);
};
}