diff --git a/package-lock.json b/package-lock.json index 22e39b1..86df290 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jackallabs/banshee", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@jackallabs/banshee", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "dependencies": { "@cosmjs/amino": "^0.32.3", diff --git a/package.json b/package.json index f436e68..9b59e20 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jackallabs/banshee", - "version": "1.0.0", + "version": "1.0.1", "description": "Modern problems require modern solutions", "keywords": [], "exports": { diff --git a/src/classes/websocketCore.ts b/src/classes/websocketCore.ts index 4405f7e..f3b3ca4 100644 --- a/src/classes/websocketCore.ts +++ b/src/classes/websocketCore.ts @@ -6,9 +6,11 @@ import { tidyString } from '@/utils/misc' export class WebsocketCore implements IWebsocketCore { protected readonly wsConnections: Record + protected readonly wsFeeds: Record constructor() { this.wsConnections = {} + this.wsFeeds = {} } monitor( @@ -30,45 +32,59 @@ export class WebsocketCore implements IWebsocketCore { protected setupMonitoring( conn: IIbcEngageBundle, ): void { - if ( - !conn.endpoint.startsWith('ws://') - && !conn.endpoint.startsWith('wss://') - ) { - throw new Error('invalid url') - } - const cleanEndpoint = tidyString(conn.endpoint, '/') - const finalEndpoint = cleanEndpoint.endsWith('websocket') - ? cleanEndpoint - : `${cleanEndpoint}/websocket` - if (!this.wsConnections[conn.chainId]) { - this.wsConnections[conn.chainId] = new WebSocket(finalEndpoint) - } - const client = this.wsConnections[conn.chainId] + try { + if ( + !conn.endpoint.startsWith('ws://') + && !conn.endpoint.startsWith('wss://') + ) { + throw new Error('invalid url') + } - const wsQuery = { - jsonrpc: '2.0', - method: 'subscribe', - id: Date.now().toString(), - params: { - query: conn.query - ? `tm.event = 'Tx' AND ${conn.query}` - : `tm.event = 'Tx'`, - }, - } - client.onopen = () => { - client.send(JSON.stringify(wsQuery)) - } - client.onmessage = (msg) => { - try { - const data = JSON.parse(msg.data) - if (!data.result.data) { - return + const cleanEndpoint = tidyString(conn.endpoint, '/') + const finalEndpoint = cleanEndpoint.endsWith('websocket') + ? cleanEndpoint + : `${cleanEndpoint}/websocket` + if (!this.wsConnections[conn.chainId]) { + this.wsConnections[conn.chainId] = new WebSocket(finalEndpoint) + } + const client = this.wsConnections[conn.chainId] + + const query = conn.query + ? `tm.event = 'Tx' AND ${conn.query}` + : `tm.event = 'Tx'` + const marker = Date.now().toString() + this.wsFeeds[`${marker}|${query}`] = conn.feed + const wsQuery = { + jsonrpc: '2.0', + method: 'subscribe', + id: marker, + params: { query }, + } + + if (client.readyState === 1) { + client.send(JSON.stringify(wsQuery)) + } else { + client.onopen = () => { + client.send(JSON.stringify(wsQuery)) + } + } + + client.onmessage = (msg) => { + try { + const data = JSON.parse(msg.data) + if (!data.result.data) { + return + } + + const ready = Responses.decodeTxEvent(data.result) as T + this.wsFeeds[`${marker}|${data.result.query}`].push(ready) + this.wsFeeds[`${marker}|${data.result.query}`] = [] + } catch (err) { + console.error(err) } - const ready = Responses.decodeTxEvent(data.result) as T - conn.feed.push(ready) - } catch (err) { - console.error(err) } + } catch (err) { + throw err } } }