mirror of
https://github.com/wisplite/tether-ts.git
synced 2026-05-01 06:22:41 -05:00
tons of bugfixes and additional logging
This commit is contained in:
Vendored
+18
-11
@@ -2,6 +2,7 @@ import { WebSocketHandler } from './utils/websocket.js';
|
||||
export class TetherClient {
|
||||
websocketHandler = new WebSocketHandler();
|
||||
subscribedQueries = new Map();
|
||||
pendingMutations = new Map();
|
||||
connect = (url) => {
|
||||
this.websocketHandler.startConnection(url);
|
||||
this.websocketHandler.onQuery = (location, data) => {
|
||||
@@ -10,6 +11,15 @@ export class TetherClient {
|
||||
callback?.(data);
|
||||
}
|
||||
};
|
||||
this.websocketHandler.onMutation = (incoming_id, data) => {
|
||||
const pending = this.pendingMutations.get(incoming_id);
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
clearTimeout(pending.timeoutId);
|
||||
this.pendingMutations.delete(incoming_id);
|
||||
pending.resolve(data);
|
||||
};
|
||||
this.websocketHandler.onOpen = () => {
|
||||
this.subscribedQueries.forEach(({ params }, queryName) => {
|
||||
this.websocketHandler.send(JSON.stringify({
|
||||
@@ -40,22 +50,19 @@ export class TetherClient {
|
||||
};
|
||||
sendMutation = (mutationName, params) => {
|
||||
const mutation_id = crypto.randomUUID();
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
this.pendingMutations.delete(mutation_id);
|
||||
reject(new Error('Mutation timeout'));
|
||||
}, 10000);
|
||||
this.pendingMutations.set(mutation_id, { resolve, reject, timeoutId });
|
||||
});
|
||||
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);
|
||||
}
|
||||
};
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user