mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
update to be a class
This commit is contained in:
@@ -1,48 +1,53 @@
|
|||||||
const subscribedQueries = new Map<string, (data: any) => void>();
|
export class TetherClient {
|
||||||
let ws: WebSocket | null = null;
|
private ws: WebSocket | null = null;
|
||||||
|
private subscribedQueries = new Map<string, (data: any) => void>();
|
||||||
|
|
||||||
export const connect = async (url: string): Promise<WebSocket> => {
|
connect = (url: string) => {
|
||||||
ws = new WebSocket(url);
|
this.ws = new WebSocket(url);
|
||||||
ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
console.log('Connected to Tether');
|
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);
|
disconnect = () => {
|
||||||
if (data.type === 'query') {
|
if (!this.ws) {
|
||||||
subscribedQueries.forEach((callback, query) => {
|
throw new Error('Not connected to Tether');
|
||||||
if (data.query === query) {
|
|
||||||
callback(data.data);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (data.type === 'error') {
|
|
||||||
console.error(data.error);
|
|
||||||
}
|
}
|
||||||
|
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) => {
|
unsubscribe = (query: string) => {
|
||||||
ws.close();
|
this.subscribedQueries.delete(query);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const subscribe = (query: string, callback: (data: any) => void) => {
|
sendMutation = (mutationName: string, params: any) => {
|
||||||
subscribedQueries.set(query, callback);
|
if (!this.ws) {
|
||||||
};
|
throw new Error('Not connected to Tether');
|
||||||
|
}
|
||||||
export const unsubscribe = (query: string) => {
|
this.ws.send(JSON.stringify({
|
||||||
subscribedQueries.delete(query);
|
type: 'mutation',
|
||||||
};
|
name: mutationName,
|
||||||
|
payload: params,
|
||||||
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,
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tether-ts",
|
"name": "tether-ts",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"description": "TypeScript client for Tether",
|
"description": "TypeScript client for Tether",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"author": "wisplite",
|
"author": "wisplite",
|
||||||
|
|||||||
Reference in New Issue
Block a user