diff --git a/src/index.ts b/src/index.ts index e790543..43a6d84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,10 +44,22 @@ export class TetherClient { }; sendMutation = (mutationName: string, params: any) => { + 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) => { + this.websocketHandler.onMutation = (data) => { + if (data.mutation_id === mutation_id) { + resolve(data); + } + }; + setTimeout(() => { + reject(new Error('Mutation timeout')); + }, 10000); + }); }; } \ No newline at end of file diff --git a/src/utils/websocket.ts b/src/utils/websocket.ts index 20e1def..6208db0 100644 --- a/src/utils/websocket.ts +++ b/src/utils/websocket.ts @@ -8,7 +8,7 @@ export class WebSocketHandler { private maxReconnectAttempts: number = 5; private reconnectInterval: number = 1000; private sendQueue: string[] = []; - + public onMutation: (data: any) => void = () => {}; startConnection = (url: string) => { this.url = url; this.ws = new WebSocket(url); @@ -33,6 +33,8 @@ export class WebSocketHandler { }; if (data.type === 'query') { this.onQuery(data.location, data.data); + } else if (data.type === 'mutation') { + this.onMutation(data.data); } else if (data.type === 'error') { console.error(data.error); } @@ -68,4 +70,5 @@ export class WebSocketHandler { } this.ws?.send(message); }; + }