From ae2f440287f859aa11402ddf29e29a1c2b280a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Fri, 26 Jan 2018 11:35:23 +0100 Subject: [PATCH 1/2] Improve syncAccount action --- src/internals/accounts/sync.js | 9 +-------- src/renderer/events.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/internals/accounts/sync.js b/src/internals/accounts/sync.js index d30d9db6..0dd24e30 100644 --- a/src/internals/accounts/sync.js +++ b/src/internals/accounts/sync.js @@ -17,14 +17,7 @@ export default (send: Function) => ({ send('accounts.sync.progress', null, { kill: false }) try { - const result = await accounts.reduce( - (promise, account) => - promise.then(async results => { - const result = await syncAccount(account) - return [...results, result] - }), - Promise.resolve([]), - ) + const result = await Promise.all(accounts.map(syncAccount)) send('accounts.sync.success', result) } catch (err) { diff --git a/src/renderer/events.js b/src/renderer/events.js index 2d7e738a..a08f42c1 100644 --- a/src/renderer/events.js +++ b/src/renderer/events.js @@ -3,6 +3,7 @@ import { ipcRenderer } from 'electron' import objectPath from 'object-path' import get from 'lodash/get' +import uniqBy from 'lodash/uniqBy' import type { Accounts } from 'types/common' @@ -11,7 +12,7 @@ import { CHECK_UPDATE_TIMEOUT, SYNC_ACCOUNT_TIMEOUT } from 'constants' import { updateDevices, addDevice, removeDevice } from 'actions/devices' import { syncAccount } from 'actions/accounts' import { setUpdateStatus } from 'reducers/update' -import { getAccounts } from 'reducers/accounts' +import { getAccountData, getAccounts } from 'reducers/accounts' type MsgPayload = { type: string, @@ -66,9 +67,17 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => { sync: { success: accounts => { if (syncAccounts) { - accounts.forEach( - account => account.transactions.length > 0 && store.dispatch(syncAccount(account)), - ) + const currentState = store.getState() + accounts.forEach(account => { + const currentAccountData = getAccountData(currentState, account.id) || {} + const transactions = uniqBy( + [...currentAccountData.transactions, ...account.transactions], + tx => tx.hash, + ) + if (currentAccountData.transactions.length !== transactions.length) { + store.dispatch(syncAccount(account)) + } + }) syncTimeout = setTimeout(() => { const newAccounts = getAccounts(store.getState()) startSyncAccounts(newAccounts) From 2ffd2a8e0fddf4a0ec3f5ad461e5a38261edcfe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Fri, 26 Jan 2018 12:11:52 +0100 Subject: [PATCH 2/2] Fix SelectAccounts components, clean stuffs --- src/components/IsUnlocked/index.js | 6 ++++++ src/components/SelectAccount.js | 12 ++++++++---- src/components/base/Select/index.js | 10 +++++++--- src/constants.js | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/components/IsUnlocked/index.js b/src/components/IsUnlocked/index.js index f1f9b429..12ddc0fd 100644 --- a/src/components/IsUnlocked/index.js +++ b/src/components/IsUnlocked/index.js @@ -57,6 +57,12 @@ class IsUnlocked extends PureComponent { ...defaultState, } + componentWillMount() { + if (this.props.isLocked) { + stopSyncAccounts() + } + } + componentWillReceiveProps(nextProps) { if (this.props.isLocked && !nextProps.isLocked) { startSyncAccounts(nextProps.accounts) diff --git a/src/components/SelectAccount.js b/src/components/SelectAccount.js index e187f9aa..40e9a6c9 100644 --- a/src/components/SelectAccount.js +++ b/src/components/SelectAccount.js @@ -12,6 +12,10 @@ import Text from 'components/base/Text' import type { Account } from 'types/common' +function renderItem(accounts) { + return item => {(accounts.find(a => a.id === item) || {}).name} +} + const mapStateToProps: MapStateToProps<*, *, *> = state => ({ accounts: values(getAccounts(state)), }) @@ -24,12 +28,13 @@ type Props = { const SelectAccount = ({ accounts, value, onChange }: Props) => (