-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
websocket overhaul
Showing
13 changed files
with
69 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,70 @@ | ||
import type { CometClient } from '@cosmjs/tendermint-rpc' | ||
import { connectComet } from '@cosmjs/tendermint-rpc' | ||
import type { Stream } from 'xstream' | ||
import type { TPossibleTxEvents } from '@/types' | ||
import { | ||
IIbcDeafenBundle, | ||
IIbcDisengageBundle, | ||
IIbcEngageBundle, | ||
IWebsocketCore, | ||
} from '@/interfaces' | ||
|
||
import { Responses } from '@cosmjs/tendermint-rpc/build/tendermint34/adaptor' | ||
import { tidyString } from '@/utils/misc' | ||
|
||
export class WebsocketCore implements IWebsocketCore { | ||
protected readonly wsConnections: Record<string, CometClient> | ||
protected readonly activeStreams: Record<string, Stream<TPossibleTxEvents>> | ||
protected readonly wsConnections: Record<string, WebSocket> | ||
|
||
constructor() { | ||
this.wsConnections = {} | ||
this.activeStreams = {} | ||
} | ||
|
||
async monitor<T extends TPossibleTxEvents>( | ||
monitor<T extends TPossibleTxEvents>( | ||
connections: IIbcEngageBundle<T> | IIbcEngageBundle<T>[], | ||
): Promise<void> { | ||
): void { | ||
try { | ||
if (connections instanceof Array) { | ||
for (let conn of connections) { | ||
await this.setupMonitoring<T>(conn) | ||
this.setupMonitoring<T>(conn) | ||
} | ||
} else { | ||
await this.setupMonitoring<T>(connections) | ||
this.setupMonitoring<T>(connections) | ||
} | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
disengage(connections: IIbcDisengageBundle | IIbcDisengageBundle[]): void { | ||
if (connections instanceof Array) { | ||
for (let conn of connections) { | ||
delete this.activeStreams[`${conn.chainId}|${conn.query || 'all'}`] | ||
} | ||
} else { | ||
delete this.activeStreams[ | ||
`${connections.chainId}|${connections.query || 'all'}` | ||
] | ||
} | ||
} | ||
|
||
deafen(connection: IIbcDeafenBundle): void { | ||
this.activeStreams[ | ||
`${connection.chainId}|${connection.query || 'all'}` | ||
].removeListener(connection.listener) | ||
} | ||
|
||
debug(): void { | ||
console.log('wsConnections:', this.wsConnections) | ||
console.log('activeStreams:', this.activeStreams) | ||
} | ||
|
||
protected async setupMonitoring<T extends TPossibleTxEvents>( | ||
protected setupMonitoring<T extends TPossibleTxEvents>( | ||
conn: IIbcEngageBundle<T>, | ||
) { | ||
): 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] = await connectComet(conn.endpoint) | ||
this.wsConnections[conn.chainId] = new WebSocket(finalEndpoint) | ||
} | ||
const client = this.wsConnections[conn.chainId] | ||
const streamId = `${conn.chainId}|${conn.query || 'all'}` | ||
if (!this.activeStreams[streamId]) { | ||
if (conn.query) { | ||
this.activeStreams[streamId] = client.subscribeTx( | ||
conn.query, | ||
) as Stream<TPossibleTxEvents> | ||
} else { | ||
this.activeStreams[streamId] = | ||
client.subscribeTx() as Stream<TPossibleTxEvents> | ||
|
||
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 postProcess = Responses.decodeTxEvent(data.result) | ||
conn.feed.push(postProcess as T) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
this.activeStreams[streamId].addListener(conn.listener) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export * from '@/classes' | ||
export * from '@/interfaces' | ||
export * from '@/types' | ||
|
||
export { makeListener } from '@/utils/misc' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,15 @@ | ||
import type { TCurrentTxEvent, TPossibleTxEvents } from '@/types' | ||
import type { IListener } from '@/interfaces/IListener' | ||
|
||
/** | ||
* @interface IIbcEngageBundle | ||
* @property {string} chainId | ||
* @property {string} endpoint | ||
* @property {string} [query] | ||
* @property {TCurrentTxEvent<T>[]} feed | ||
* @property {IListener<TPossibleTxEvents>} listener | ||
*/ | ||
export interface IIbcEngageBundle<T extends TPossibleTxEvents> { | ||
chainId: string | ||
endpoint: string | ||
query?: string | ||
feed: TCurrentTxEvent<T>[] | ||
listener: IListener<TPossibleTxEvents> | ||
} | ||
|
||
/** | ||
* @interface IIbcDisengageBundle | ||
* @property {string} chainId | ||
* @property {string} [query] | ||
*/ | ||
export interface IIbcDisengageBundle { | ||
chainId: string | ||
query?: string | ||
} | ||
|
||
/** | ||
* @interface IIbcDeafenBundle | ||
* @property {string} chainId | ||
* @property {string} [query] | ||
* @property {IListener<TPossibleTxEvents>} listener | ||
*/ | ||
export interface IIbcDeafenBundle { | ||
chainId: string | ||
query?: string | ||
listener: IListener<TPossibleTxEvents> | ||
} | ||
|
||
/** | ||
* @interface IIbcMakeListenerBundle | ||
* @property {string} chainId | ||
* @property {TCurrentTxEvent<T>[]} feed | ||
*/ | ||
export interface IIbcMakeListenerBundle<T extends TPossibleTxEvents> { | ||
chainId: string | ||
feed: TCurrentTxEvent<T>[] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TTidyStringModes = 'start' | 'end' | 'both' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from '@/types/stockCosmos' | ||
export * from '@/types/TClientDefs' | ||
export * from '@/types/TMisc' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters