mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
do all the boring ts stuff to make this actually work
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
export class TetherClient {
|
||||
private ws: WebSocket | null = null;
|
||||
private subscribedQueries = new Map<string, (data: any) => void>();
|
||||
|
||||
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');
|
||||
};
|
||||
};
|
||||
|
||||
disconnect = () => {
|
||||
if (!this.ws) {
|
||||
throw new Error('Not connected to Tether');
|
||||
}
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
};
|
||||
|
||||
subscribe = (query: string, callback: (data: any) => void) => {
|
||||
this.subscribedQueries.set(query, callback);
|
||||
};
|
||||
|
||||
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,
|
||||
}));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user