@@ -143,7 +143,12 @@ export class ElectrumExplorer implements Explorer {
143143 * Implements {@link Explorer#connect}.
144144 */
145145 async connect ( ) : Promise < void > {
146- if ( await this . isConnected ( ) ) throw new Error ( 'Client already connected.' ) ;
146+ if ( this . #pingInterval)
147+ throw new Error (
148+ 'Client was not successfully closed. Prev connection is still pinging.'
149+ ) ;
150+ if ( ! this . isClosed ( ) )
151+ throw new Error ( 'Client previously connected and never closed.' ) ;
147152 this . #client = new ElectrumClient (
148153 netModule ,
149154 this . #protocol === 'ssl' ? tlsModule : false ,
@@ -179,20 +184,22 @@ export class ElectrumExplorer implements Explorer {
179184 //socket has been init. Here we get an error if electrum server cannot
180185 //be found in that port, so close the socket
181186 try {
182- await this . close ( ) ; //hide possible socket errors
187+ if ( ! this . isClosed ( ) ) this . close ( ) ;
183188 } catch ( err ) {
184189 console . warn ( 'Error while closing connection:' , getErrorMsg ( error ) ) ;
185190 }
186191 throw new Error (
187192 `ElectrumClient failed to connect: ${ getErrorMsg ( error ) } `
188193 ) ;
189194 }
195+ if ( ! this . #client) throw new Error ( 'Client should exist at this point' ) ;
190196
191197 // Ping every few seconds to keep connection alive.
192198 // This function will never throw since it cannot be handled
193199 // In case of connection errors, users will get them on any next function
194200 // call
195201 this . #pingInterval = setInterval ( async ( ) => {
202+ const pingInterval = this . #pingInterval;
196203 this . #getClientOrThrow( ) ;
197204 let shouldReconnect = false ;
198205 try {
@@ -204,11 +211,15 @@ export class ElectrumExplorer implements Explorer {
204211 getErrorMsg ( error )
205212 ) ;
206213 }
207- if ( shouldReconnect ) {
214+ //Dont allow 2 instances of #pingInterval. #pingInterval is set on connection
215+ if ( shouldReconnect && pingInterval === this . #pingInterval) {
208216 try {
209- if ( await this . isConnected ( false ) ) await this . close ( ) ; //hide possible socket errors
217+ if ( this . isClosed ( ) ) throw new Error ( 'Pinging a closed connection' ) ;
218+ this . close ( ) ;
210219 await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
211- await this . connect ( ) ;
220+ //connect may have been set externally while sleeping, check first
221+ if ( this . isClosed ( ) && pingInterval === this . #pingInterval)
222+ await this . connect ( ) ;
212223 } catch ( error ) {
213224 console . warn (
214225 'Error while reconnecting during interval pinging:' ,
@@ -242,31 +253,32 @@ export class ElectrumExplorer implements Explorer {
242253 * Checks server connectivity by sending a ping. Returns `true` if the ping
243254 * is successful, otherwise `false`.
244255 */
245- async isConnected (
246- requestNetworkConfirmation : boolean = true
247- ) : Promise < boolean > {
248- if ( this . #client === undefined ) return false ;
256+ async isConnected ( ) : Promise < boolean > {
257+ if ( ! this . #client) return false ;
249258 else {
250- if ( requestNetworkConfirmation ) {
251- try {
252- await this . #client. server_ping ( ) ;
253- return true ;
254- } catch { }
255- return false ;
256- } else return true ;
259+ try {
260+ await this . #client. server_ping ( ) ;
261+ return true ;
262+ } catch { }
263+ return false ;
257264 }
258265 }
266+ isClosed ( ) : boolean {
267+ return ! this . #client;
268+ }
259269
260270 /**
261271 * Implements {@link Explorer#close}.
262272 */
263- async close ( ) : Promise < void > {
273+ close ( ) : void {
264274 if ( this . #pingInterval) {
265275 clearInterval ( this . #pingInterval) ;
266276 this . #pingInterval = undefined ;
267277 }
268278 if ( ! this . #client) console . warn ( 'Client was already closed' ) ;
269- else this . #client. close ( ) ;
279+ else {
280+ this . #client. close ( ) ;
281+ }
270282 this . #client = undefined ;
271283 }
272284
0 commit comments