import { WebSocketRxJs } from '@dripjs/common' ; import { getWSAuthQuery } from '../../../common' ; import { Config } from '../../../types' ; import { endpoints } from '../../types' ; param T param U export abstract class WebsocketInsiderBase < T , U > { private ws : WebSocketRxJs < U > | null = null ; protected constructor ( private readonly config ) { } abstract handleMessage ( response ) ; abstract onDestroy ( ) ; send ( request ) { if ( ! this . ws ) { this . init ( ) ; } if ( this . ws ) { this . ws . send ( JSON . stringify ( request ) ) ; } } destroy ( ) { if ( this . ws ) { this . ws . close ( ) ; this . ws = null ; } this . onDestroy ( ) ; } private init ( ) { const requestUrl = this . makeRequestUrl ( ) ; this . ws = new WebSocketRxJs ( requestUrl ) ; this . ws . message$ . subscribe ( ( response ) => { this . handleMessage ( response ) ; } ) ; } private makeRequestUrl ( ) { let endpoint = this . config . testnet ? endpoints . testnet : endpoints . production ; if ( this . config . apiKey && this . config . apiSecret ) { endpoint += `?${ getWSAuthQuery ( this . config . apiKey , this . config . apiSecret ) }` ; } return endpoint ; } }