From 38a3ac89eaa08d1d0bf05fc4a091fc51255e2fff Mon Sep 17 00:00:00 2001 From: Jordan Muir Date: Fri, 15 Apr 2022 18:55:18 -0400 Subject: [PATCH] Kill chain connection providers --- main/chains/index.js | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/main/chains/index.js b/main/chains/index.js index 1d01a32737..55db8ffddb 100644 --- a/main/chains/index.js +++ b/main/chains/index.js @@ -137,14 +137,25 @@ class ChainConnection extends EventEmitter { }) } - getNodeType (provider, cb) { provider.sendAsync({ jsonrpc: '2.0', method: 'web3_clientVersion', params: [], id: 1 }, cb) } + getNodeType (provider, cb) { p + rovider.sendAsync({ jsonrpc: '2.0', method: 'web3_clientVersion', params: [], id: 1 }, cb) + } + + killProvider (provider) { + if (provider) { + provider.removeAllListeners() + provider.close() + } + } connect (chain) { const connection = chain.connection log.info(this.type + ':' + this.chainId + '\'s connection has been updated') if (this.network !== connection.network) { - if (this.primary.provider) this.primary.provider.close() - if (this.secondary.provider) this.secondary.provider.close() + this.killProvider(this.primary.provider) + this.primary.provider = null + this.killProvider(this.secondary.provider) + this.secondary.provider = null this.primary = { status: 'loading', network: '', type: '', connected: false } this.secondary = { status: 'loading', network: '', type: '', connected: false } this.update('primary') @@ -159,7 +170,7 @@ class ChainConnection extends EventEmitter { if (connection.primary.on && connection.primary.status === 'connected') { // Connection is on Standby log.info(' Secondary connection on STANDBY', connection.secondary.status === 'standby') - if (this.secondary.provider) this.secondary.provider.close() + this.killProvider(this.secondary.provider) this.secondary.provider = null if (connection.secondary.status !== 'standby') { this.secondary.connected = false @@ -175,7 +186,7 @@ class ChainConnection extends EventEmitter { const target = secondary.current === 'custom' ? secondary.custom : currentPresets[secondary.current] if (!this.secondary.provider || this.secondary.currentSecondaryTarget !== target) { log.info(' Creating secondary connection because it didn\'t exist or the target changed') - if (this.secondary.provider) this.secondary.provider.close() + this.killProvider(this.secondary.provider) this.secondary.provider = null this.secondary.currentSecondaryTarget = target this.secondary.status = 'loading' @@ -235,7 +246,7 @@ class ChainConnection extends EventEmitter { // Secondary connection is set to OFF by the user } else { log.info(' Secondary connection: OFF') - if (this.secondary.provider) this.secondary.provider.close() + this.killProvider(this.secondary.provider) this.secondary.provider = null if (this.secondary.status !== 'off') { this.secondary.status = 'off' @@ -256,7 +267,7 @@ class ChainConnection extends EventEmitter { if (!this.primary.provider || this.primary.currentPrimaryTarget !== target) { log.info(' Creating primary connection because it didn\'t exist or the target changed') - if (this.primary.provider) this.primary.provider.close() + this.killProvider(this.primary.provider) this.primary.provider = null this.primary.currentPrimaryTarget = target this.primary.status = 'loading' @@ -315,7 +326,7 @@ class ChainConnection extends EventEmitter { } } else { log.info(' Primary connection: OFF') - if (this.primary.provider) this.primary.provider.close() + this.killProvider(this.primary.provider) this.primary.provider = null if (this.primary.status !== 'off') { this.primary.status = 'off' @@ -330,15 +341,11 @@ class ChainConnection extends EventEmitter { close (update = true) { if (this.observer) this.observer.remove() - if (this.primary.provider) { - this.primary.provider.removeAllListeners() - this.primary.provider.close() - } + this.killProvider(this.primary.provider) + this.primary.provider = null - if (this.secondary.provider) { - this.secondary.provider.removeAllListeners() - this.secondary.provider.close() - } + this.killProvider(this.secondary.provider) + this.secondary.provider = null if (update) { this.primary = { status: 'loading', network: '', type: '', connected: false }