Skip to content

Commit

Permalink
fix: move update address to sync process and buffer it
Browse files Browse the repository at this point in the history
update address will block main process now
  • Loading branch information
classicalliu committed Jul 27, 2019
1 parent c82f54a commit fcad39a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
20 changes: 18 additions & 2 deletions packages/neuron-wallet/src/listeners/address.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { remote } from 'electron'
import { ReplaySubject } from 'rxjs'
import { bufferTime } from 'rxjs/operators'
import AddressesUsedSubject from '../models/subjects/addresses-used-subject'
import AddressService from '../services/addresses'
import WalletService from '../services/wallets'
import { AccountExtendedPublicKey } from '../models/keys/key'

const isRenderer = process && process.type === 'renderer'
const addressesUsedSubject = isRenderer
? remote.require('./models/subjects/addresses-used-subject').default.getSubject()
: AddressesUsedSubject.getSubject()

// pipe not working directly
const bridge = new ReplaySubject<string[]>(1000)
addressesUsedSubject.subscribe((addresses: string[]) => {
bridge.next(addresses)
})

// update txCount when addresses used
export const register = () => {
AddressesUsedSubject.getSubject().subscribe(async (addresses: string[]) => {
const addrs = await AddressService.updateTxCountAndBalances(addresses)
bridge.pipe(bufferTime(1000)).subscribe(async (addressesList: string[][]) => {
const addresses = addressesList.reduce((acc, val) => acc.concat(val), [])
const uniqueAddresses = [...new Set(addresses)]
const addrs = await AddressService.updateTxCountAndBalances(uniqueAddresses)
const walletIds: string[] = addrs.map(addr => addr.walletId).filter((value, idx, a) => a.indexOf(value) === idx)
await Promise.all(
walletIds.map(async id => {
Expand Down
4 changes: 0 additions & 4 deletions packages/neuron-wallet/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import createMainWindow from './startup/create-main-window'
import createSyncBlockTask from './startup/sync-block-task/create'
import initConnection from './database/address/ormconfig'
import WalletsService from './services/wallets'
import { register as registerAddressListener } from './listeners/address'

// register to listen address updates
registerAddressListener()

const walletsService = WalletsService.getInstance()

Expand Down
5 changes: 5 additions & 0 deletions packages/neuron-wallet/src/startup/sync-block-task/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { initDatabase } from './init-database'
import { register as registerTxStatusListener } from '../../listeners/tx-status'
import Utils from '../../services/sync/utils'

import { register as registerAddressListener } from '../../listeners/address'

// register to listen address updates
registerAddressListener()

const {
nodeService,
addressDbChangedSubject,
Expand Down

0 comments on commit fcad39a

Please sign in to comment.