diff --git a/dist/index.d.ts b/dist/index.d.ts index 00cc7a8..222dfc3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -5,5 +5,5 @@ export declare class TetherClient { disconnect: () => void; subscribe: (queryName: string, params: any, callback: (data: any) => void) => void; unsubscribe: (query: string) => void; - sendMutation: (mutationName: string, params: any) => void; + sendMutation: (mutationName: string, params: any) => Promise; } diff --git a/dist/index.js b/dist/index.js index eb97bd6..a12e368 100644 --- a/dist/index.js +++ b/dist/index.js @@ -39,10 +39,22 @@ export class TetherClient { })); }; 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) => { + this.websocketHandler.onMutation = (data) => { + if (data.mutation_id === mutation_id) { + resolve(data); + } + }; + setTimeout(() => { + reject(new Error('Mutation timeout')); + }, 10000); + }); }; } diff --git a/dist/utils/websocket.d.ts b/dist/utils/websocket.d.ts index c439029..4d1bb25 100644 --- a/dist/utils/websocket.d.ts +++ b/dist/utils/websocket.d.ts @@ -8,6 +8,7 @@ export declare class WebSocketHandler { private maxReconnectAttempts; private reconnectInterval; private sendQueue; + onMutation: (data: any) => void; startConnection: (url: string) => void; attemptReconnect: () => void; close: () => void; diff --git a/dist/utils/websocket.js b/dist/utils/websocket.js index d463454..62f30ab 100644 --- a/dist/utils/websocket.js +++ b/dist/utils/websocket.js @@ -8,6 +8,7 @@ export class WebSocketHandler { maxReconnectAttempts = 5; reconnectInterval = 1000; sendQueue = []; + onMutation = () => { }; startConnection = (url) => { this.url = url; this.ws = new WebSocket(url); @@ -26,6 +27,9 @@ 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); }