Skip to content

Commit

Permalink
Kill chain connection providers
Browse files Browse the repository at this point in the history
  • Loading branch information
floating committed Apr 15, 2022
1 parent d3009b1 commit 38a3ac8
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions main/chains/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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 }
Expand Down

0 comments on commit 38a3ac8

Please sign in to comment.