Optimise multi-balance fetching (one set)

This commit is contained in:
Jaco Greeff 2016-11-23 12:32:50 +01:00
parent dddb8752f2
commit e9eebb3088

View File

@ -24,7 +24,11 @@ export default class Store {
} }
@action setBalance = (address, balance) => { @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) { fetchBalance (address) {
@ -38,11 +42,25 @@ export default class Store {
}); });
} }
fetchBalances (addresses) { fetchBalances (_addresses) {
addresses.forEach((address) => { const addresses = _addresses.filter((address) => address) || [];
if (address) {
this.fetchBalance(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);
}); });
} }
} }