Skip to content

Commit

Permalink
fix: initialize NetworksService in renderer process
Browse files Browse the repository at this point in the history
Reduce about `1100 ~ 1200` `TIME_WAIT` connections to about `20 ~ 30`
  • Loading branch information
classicalliu committed Nov 23, 2019
1 parent 6558669 commit 73f1bf0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BehaviorSubject } from 'rxjs'
import { NetworkWithID } from 'types/network'

export default class NetworkSwitchSubject {
static subject = new BehaviorSubject<undefined | NetworkWithID>(undefined)

static getSubject() {
return this.subject
}
}
28 changes: 20 additions & 8 deletions packages/neuron-wallet/src/services/networks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { v4 as uuid } from 'uuid'
import { BehaviorSubject } from 'rxjs'
import { LackOfDefaultNetwork, DefaultNetworkUnremovable } from 'exceptions/network'
import { DefaultNetworkUnremovable, LackOfDefaultNetwork } from 'exceptions/network'

import Store from 'models/store'

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'

export const networkSwitchSubject = new BehaviorSubject<undefined | NetworkWithID>(undefined)
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 presetNetworks: { selected: string, networks: NetworkWithID[] } = {
selected: 'mainnet',
Expand Down Expand Up @@ -40,20 +52,20 @@ export default class NetworksService extends Store {
super('networks', 'index.json', JSON.stringify(presetNetworks))

const currentNetworkList = this.getAll()
NetworkListSubject.next({ currentNetworkList })
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 })
currentNetworkIDSubject.next({ currentNetworkID: currentNetwork.id })
networkSwitchSubject.next(currentNetwork)
}

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

const currentID = this.getCurrentID()
if (currentNetworkList.find(network => network.id === currentID)) {
Expand All @@ -72,7 +84,7 @@ export default class NetworksService extends Store {
if (!currentNetwork) {
throw new NetworkNotFound(currentNetworkID)
}
CurrentNetworkIDSubject.next({ currentNetworkID })
currentNetworkIDSubject.next({ currentNetworkID })
networkSwitchSubject.next(currentNetwork)
})
}
Expand Down Expand Up @@ -158,7 +170,7 @@ export default class NetworksService extends Store {
this.updateAll(list)

if (this.getCurrentID() === id) {
CurrentNetworkIDSubject.next({ currentNetworkID: id })
currentNetworkIDSubject.next({ currentNetworkID: id })
networkSwitchSubject.next(network)
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/neuron-wallet/src/startup/sync-block-task/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BrowserWindow } from 'electron'
import { ReplaySubject } from 'rxjs'
import path from 'path'
import { networkSwitchSubject } from 'services/networks'
import { NetworkWithID } from 'types/network'
import env from 'env'
import AddressService from 'services/addresses'
Expand All @@ -14,6 +13,7 @@ import NetworksService from 'services/networks'
import { distinctUntilChanged } from 'rxjs/operators'
import LockUtils from 'models/lock-utils'
import DaoUtils from 'models/dao-utils'
import NetworkSwitchSubject from 'models/subjects/network-switch-subject'

export { genesisBlockHash }

Expand Down Expand Up @@ -55,7 +55,7 @@ const networkChange = async (network: NetworkWithID) => {

export const databaseInitSubject = new ReplaySubject<DatabaseInitParams>(1)

networkSwitchSubject.subscribe(async (network: NetworkWithID | undefined) => {
NetworkSwitchSubject.getSubject().subscribe(async (network: NetworkWithID | undefined) => {
if (network) {
await networkChange(network)
}
Expand All @@ -77,8 +77,6 @@ NodeService

const loadURL = `file://${path.join(__dirname, 'index.html')}`

export { networkSwitchSubject }

let syncBlockBackgroundWindow: BrowserWindow | null

// create a background task to sync transactions
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/startup/sync-block-task/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { AddressesUsedSubject } from 'models/subjects/addresses-used-subject'
import AddressDbChangedSubject from 'models/subjects/address-db-changed-subject'
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
import AddressCreatedSubject from 'models/subjects/address-created-subject'

export { networkSwitchSubject } from 'services/networks'
import NetworkSwitchSubject from 'models/subjects/network-switch-subject'

export { genesisBlockHash } from './create'
export { databaseInitSubject } from './create'
Expand All @@ -14,3 +13,4 @@ export const addressesUsedSubject = AddressesUsedSubject.getSubject()
export const addressDbChangedSubject = AddressDbChangedSubject.getSubject()
export const walletCreatedSubject = WalletCreatedSubject.getSubject()
export const addressCreatedSubject = AddressCreatedSubject.getSubject()
export const networkSwitchSubject = NetworkSwitchSubject.getSubject()

0 comments on commit 73f1bf0

Please sign in to comment.