From e9eebb3088ad271d9395e0c1884d5db4ac581026 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 23 Nov 2016 12:32:50 +0100 Subject: [PATCH] Optimise multi-balance fetching (one set) --- js/src/views/Signer/store.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/js/src/views/Signer/store.js b/js/src/views/Signer/store.js index fe9a48514..1bb63bbe2 100644 --- a/js/src/views/Signer/store.js +++ b/js/src/views/Signer/store.js @@ -24,7 +24,11 @@ export default class Store { } @action setBalance = (address, balance) => { - this.balances = Object.assign({}, this.balances, { [address]: balance }); + this.setBalances({ [address]: balance }); + } + + @action setBalances = (balances) => { + this.balances = Object.assign({}, this.balances, balances); } fetchBalance (address) { @@ -38,11 +42,25 @@ export default class Store { }); } - fetchBalances (addresses) { - addresses.forEach((address) => { - if (address) { - this.fetchBalance(address); - } - }); + fetchBalances (_addresses) { + const addresses = _addresses.filter((address) => address) || []; + + if (!addresses.length) { + return; + } + + Promise + .all(addresses.map((address) => this._api.eth.getBalance(address))) + .then((_balances) => { + this.setBalances( + addresses.reduce((balances, address, index) => { + balances[address] = _balances[index]; + return balances; + }, {}) + ); + }) + .catch((error) => { + console.warn('Store:fetchBalances', error); + }); } }