This commit is contained in:
2026-04-23 08:51:36 -05:00
parent 44fe4c63c4
commit 6ee7b5ac34
4 changed files with 18 additions and 1 deletions
+1 -1
View File
@@ -5,5 +5,5 @@ export declare class TetherClient {
disconnect: () => void; disconnect: () => void;
subscribe: (queryName: string, params: any, callback: (data: any) => void) => void; subscribe: (queryName: string, params: any, callback: (data: any) => void) => void;
unsubscribe: (query: string) => void; unsubscribe: (query: string) => void;
sendMutation: (mutationName: string, params: any) => void; sendMutation: (mutationName: string, params: any) => Promise<unknown>;
} }
+12
View File
@@ -39,10 +39,22 @@ export class TetherClient {
})); }));
}; };
sendMutation = (mutationName, params) => { sendMutation = (mutationName, params) => {
const mutation_id = crypto.randomUUID();
this.websocketHandler.send(JSON.stringify({ this.websocketHandler.send(JSON.stringify({
type: 'mutation', type: 'mutation',
location: mutationName, location: mutationName,
params: params, params: params,
mutation_id: mutation_id
})); }));
return new Promise((resolve, reject) => {
this.websocketHandler.onMutation = (data) => {
if (data.mutation_id === mutation_id) {
resolve(data);
}
};
setTimeout(() => {
reject(new Error('Mutation timeout'));
}, 10000);
});
}; };
} }
+1
View File
@@ -8,6 +8,7 @@ export declare class WebSocketHandler {
private maxReconnectAttempts; private maxReconnectAttempts;
private reconnectInterval; private reconnectInterval;
private sendQueue; private sendQueue;
onMutation: (data: any) => void;
startConnection: (url: string) => void; startConnection: (url: string) => void;
attemptReconnect: () => void; attemptReconnect: () => void;
close: () => void; close: () => void;
+4
View File
@@ -8,6 +8,7 @@ export class WebSocketHandler {
maxReconnectAttempts = 5; maxReconnectAttempts = 5;
reconnectInterval = 1000; reconnectInterval = 1000;
sendQueue = []; sendQueue = [];
onMutation = () => { };
startConnection = (url) => { startConnection = (url) => {
this.url = url; this.url = url;
this.ws = new WebSocket(url); this.ws = new WebSocket(url);
@@ -26,6 +27,9 @@ export class WebSocketHandler {
if (data.type === 'query') { if (data.type === 'query') {
this.onQuery(data.location, data.data); this.onQuery(data.location, data.data);
} }
else if (data.type === 'mutation') {
this.onMutation(data.data);
}
else if (data.type === 'error') { else if (data.type === 'error') {
console.error(data.error); console.error(data.error);
} }