From cd88f2ad1bd3ca73cfb7c4c87172a3af64c53393 Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Wed, 23 Nov 2016 19:12:32 +0100 Subject: [PATCH] Don't fetch balances on every new block if syncing #3582 --- js/src/redux/providers/balances.js | 28 +++++++++++++++++++++++++ js/src/redux/providers/statusReducer.js | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/js/src/redux/providers/balances.js b/js/src/redux/providers/balances.js index bcc8eca0a..c0260ba71 100644 --- a/js/src/redux/providers/balances.js +++ b/js/src/redux/providers/balances.js @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +import { throttle } from 'lodash'; + import { getBalances, getTokens } from './balancesActions'; import { setAddressImage } from './imagesActions'; @@ -37,11 +39,20 @@ export default class Balances { this._accountsInfo = null; this._tokenreg = null; + this._fetchingBalances = false; this._fetchingTokens = false; this._fetchedTokens = false; this._tokenregSubId = null; this._tokenregMetaSubId = null; + + // Throttled `retrieveTokens` function + // that gets called max once every 20s + this._throttledRetrieveTokens = throttle( + this._retrieveTokens, + 20 * 1000, + { trailing: true } + ); } start () { @@ -72,6 +83,15 @@ export default class Balances { return console.warn('_subscribeBlockNumber', error); } + const { syncing } = this._store.getState().nodeStatus; + + // If syncing, only retrieve balances once every + // few seconds + if (syncing) { + return this._throttledRetrieveTokens(); + } + + this._throttledRetrieveTokens.cancel(); this._retrieveTokens(); }) .catch((error) => { @@ -140,10 +160,16 @@ export default class Balances { } _retrieveBalances () { + if (this._fetchingBalances) { + return; + } + if (!this._accountsInfo) { return; } + this._fetchingBalances = true; + const addresses = Object .keys(this._accountsInfo) .filter((address) => { @@ -161,9 +187,11 @@ export default class Balances { }); this._store.dispatch(getBalances(this._balances)); + this._fetchingBalances = false; }) .catch((error) => { console.warn('_retrieveBalances', error); + this._fetchingBalances = false; }); } diff --git a/js/src/redux/providers/statusReducer.js b/js/src/redux/providers/statusReducer.js index 07fa993b9..f0ef0cb1b 100644 --- a/js/src/redux/providers/statusReducer.js +++ b/js/src/redux/providers/statusReducer.js @@ -39,7 +39,7 @@ const initialState = { }, netPort: new BigNumber(0), rpcSettings: {}, - syncing: false, + syncing: true, isConnected: false, isConnecting: false, isPingable: false,