From fcad39a01530bffc13a074b6e68d8d0efffd5aa6 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Sat, 27 Jul 2019 15:37:56 +0800 Subject: [PATCH] fix: move update address to sync process and buffer it update address will block main process now --- .../neuron-wallet/src/listeners/address.ts | 20 +++++++++++++++++-- packages/neuron-wallet/src/main.ts | 4 ---- .../src/startup/sync-block-task/task.ts | 5 +++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/neuron-wallet/src/listeners/address.ts b/packages/neuron-wallet/src/listeners/address.ts index 42a8bd4018..e619c742e0 100644 --- a/packages/neuron-wallet/src/listeners/address.ts +++ b/packages/neuron-wallet/src/listeners/address.ts @@ -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(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 => { diff --git a/packages/neuron-wallet/src/main.ts b/packages/neuron-wallet/src/main.ts index fb5aa36c7f..e030fe05a6 100644 --- a/packages/neuron-wallet/src/main.ts +++ b/packages/neuron-wallet/src/main.ts @@ -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() diff --git a/packages/neuron-wallet/src/startup/sync-block-task/task.ts b/packages/neuron-wallet/src/startup/sync-block-task/task.ts index 2e8503264a..2669063269 100644 --- a/packages/neuron-wallet/src/startup/sync-block-task/task.ts +++ b/packages/neuron-wallet/src/startup/sync-block-task/task.ts @@ -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,