Skip to content

Commit

Permalink
fix: network switch event broadcast twice
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 26, 2019
1 parent 89d1b1d commit f1b0f72
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions packages/neuron-wallet/src/services/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ import { Validate, Required } from 'decorators'
import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions'
import { NetworkListSubject, CurrentNetworkIDSubject } from 'models/subjects/networks'
import { MAINNET_GENESIS_HASH, EMPTY_GENESIS_HASH, NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
import { remote } from 'electron'
import NetworkSwitchSubject from 'models/subjects/network-switch-subject'

const isRenderer = process && process.type === 'renderer'
const networkSwitchSubject = isRenderer
? remote.require('./models/subjects/network-switch-subject').default.getSubject()
: NetworkSwitchSubject.getSubject()

const currentNetworkIDSubject = isRenderer
? remote.require('./models/subjects/networks').default.CurrentNetworkIDSubject
: CurrentNetworkIDSubject

const networkListSubject = isRenderer
? remote.require('./models/subjects/networks').default.NetworkListSubject
: NetworkListSubject
const isMainProcess = process && process.type === 'browser'

const presetNetworks: { selected: string, networks: NetworkWithID[] } = {
selected: 'mainnet',
Expand Down Expand Up @@ -52,20 +40,26 @@ export default class NetworksService extends Store {
super('networks', 'index.json', JSON.stringify(presetNetworks))

const currentNetworkList = this.getAll()
networkListSubject.next({ currentNetworkList })
if (isMainProcess) {
NetworkListSubject.next({ currentNetworkList })
}

const currentNetwork = this.getCurrent()
if (currentNetwork) {
if (currentNetwork.type !== NetworkType.Default) {
this.update(currentNetwork.id, {}) // Update to trigger chain/genesis hash refresh
}

currentNetworkIDSubject.next({ currentNetworkID: currentNetwork.id })
networkSwitchSubject.next(currentNetwork)
if (isMainProcess) {
CurrentNetworkIDSubject.next({ currentNetworkID: currentNetwork.id })
NetworkSwitchSubject.getSubject().next(currentNetwork)
}
}

this.on(NetworksKey.List, async (_, currentNetworkList: NetworkWithID[] = []) => {
networkListSubject.next({ currentNetworkList })
if (isMainProcess) {
NetworkListSubject.next({ currentNetworkList })
}

const currentID = this.getCurrentID()
if (currentNetworkList.find(network => network.id === currentID)) {
Expand All @@ -84,8 +78,10 @@ export default class NetworksService extends Store {
if (!currentNetwork) {
throw new NetworkNotFound(currentNetworkID)
}
currentNetworkIDSubject.next({ currentNetworkID })
networkSwitchSubject.next(currentNetwork)
if (isMainProcess) {
CurrentNetworkIDSubject.next({ currentNetworkID })
NetworkSwitchSubject.getSubject().next(currentNetwork)
}
})
}

Expand Down Expand Up @@ -169,9 +165,9 @@ export default class NetworksService extends Store {

this.updateAll(list)

if (this.getCurrentID() === id) {
currentNetworkIDSubject.next({ currentNetworkID: id })
networkSwitchSubject.next(network)
if (this.getCurrentID() === id && isMainProcess) {
CurrentNetworkIDSubject.next({ currentNetworkID: id })
NetworkSwitchSubject.getSubject().next(network)
}
}

Expand Down

0 comments on commit f1b0f72

Please sign in to comment.