mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
import { WebSocketHandler } from './utils/websocket.js';
|
|
export class TetherClient {
|
|
websocketHandler = new WebSocketHandler();
|
|
subscribedQueries = new Map();
|
|
connect = (url) => {
|
|
this.websocketHandler.startConnection(url);
|
|
this.websocketHandler.onQuery = (location, data) => {
|
|
if (location) {
|
|
const { callback } = this.subscribedQueries.get(location) || { callback: () => { } };
|
|
callback?.(data);
|
|
}
|
|
};
|
|
this.websocketHandler.onOpen = () => {
|
|
this.subscribedQueries.forEach(({ params }, queryName) => {
|
|
this.websocketHandler.send(JSON.stringify({
|
|
type: 'subscribe',
|
|
location: queryName,
|
|
params: params
|
|
}));
|
|
});
|
|
};
|
|
};
|
|
disconnect = () => {
|
|
this.websocketHandler.close();
|
|
};
|
|
subscribe = (queryName, params, callback) => {
|
|
this.subscribedQueries.set(queryName, { callback, params });
|
|
this.websocketHandler.send(JSON.stringify({
|
|
type: 'subscribe',
|
|
location: queryName,
|
|
params: params
|
|
}));
|
|
};
|
|
unsubscribe = (query) => {
|
|
this.subscribedQueries.delete(query);
|
|
this.websocketHandler.send(JSON.stringify({
|
|
type: 'unsubscribe',
|
|
query: query
|
|
}));
|
|
};
|
|
sendMutation = (mutationName, params) => {
|
|
const mutation_id = crypto.randomUUID();
|
|
this.websocketHandler.send(JSON.stringify({
|
|
type: 'mutation',
|
|
location: mutationName,
|
|
params: params,
|
|
mutation_id: mutation_id
|
|
}));
|
|
return new Promise((resolve, reject) => {
|
|
const timeoutId = setTimeout(() => {
|
|
reject(new Error('Mutation timeout'));
|
|
}, 10000);
|
|
this.websocketHandler.onMutation = (incoming_id, data) => {
|
|
if (incoming_id === mutation_id) {
|
|
clearTimeout(timeoutId);
|
|
resolve(data);
|
|
}
|
|
};
|
|
});
|
|
};
|
|
}
|