From 555fa1e220d840fde8bf0fc684287a61f91da977 Mon Sep 17 00:00:00 2001 From: wisplite Date: Wed, 8 Apr 2026 14:53:48 -0500 Subject: [PATCH] update to be a class --- index.ts | 91 +++++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/index.ts b/index.ts index 52311fc..494e3d1 100644 --- a/index.ts +++ b/index.ts @@ -1,48 +1,53 @@ -const subscribedQueries = new Map void>(); -let ws: WebSocket | null = null; +export class TetherClient { + private ws: WebSocket | null = null; + private subscribedQueries = new Map void>(); -export const connect = async (url: string): Promise => { - ws = new WebSocket(url); - ws.onopen = () => { - console.log('Connected to Tether'); + connect = (url: string) => { + 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'); + }; }; - ws.onmessage = (event) => { - const data = JSON.parse(event.data); - if (data.type === 'query') { - subscribedQueries.forEach((callback, query) => { - if (data.query === query) { - callback(data.data); - } - }); - } else if (data.type === 'error') { - console.error(data.error); + + disconnect = () => { + if (!this.ws) { + throw new Error('Not connected to Tether'); } + this.ws.close(); + this.ws = null; }; - ws.onclose = () => { - console.log('Disconnected from Tether'); + + subscribe = (query: string, callback: (data: any) => void) => { + this.subscribedQueries.set(query, callback); }; - return ws; -}; - -export const disconnect = (ws: WebSocket) => { - ws.close(); -}; - -export const subscribe = (query: string, callback: (data: any) => void) => { - subscribedQueries.set(query, callback); -}; - -export const unsubscribe = (query: string) => { - subscribedQueries.delete(query); -}; - -export const sendMutation = (mutationName: string, params: any) => { - if (!ws) { - throw new Error('Not connected to Tether'); - } - ws.send(JSON.stringify({ - type: 'mutation', - name: mutationName, - payload: params, - })); -}; \ No newline at end of file + + unsubscribe = (query: string) => { + this.subscribedQueries.delete(query); + }; + + sendMutation = (mutationName: string, params: any) => { + if (!this.ws) { + throw new Error('Not connected to Tether'); + } + this.ws.send(JSON.stringify({ + type: 'mutation', + name: mutationName, + payload: params, + })); + }; +} \ No newline at end of file diff --git a/package.json b/package.json index 2277bb9..bd323e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tether-ts", - "version": "1.0.0", + "version": "1.0.1", "description": "TypeScript client for Tether", "license": "ISC", "author": "wisplite",