Don't fetch balances on every new block if syncing #3582

This commit is contained in:
Nicolas Gotchac 2016-11-23 19:12:32 +01:00
parent 820e291ac8
commit cd88f2ad1b
2 changed files with 29 additions and 1 deletions

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { throttle } from 'lodash';
import { getBalances, getTokens } from './balancesActions'; import { getBalances, getTokens } from './balancesActions';
import { setAddressImage } from './imagesActions'; import { setAddressImage } from './imagesActions';
@ -37,11 +39,20 @@ export default class Balances {
this._accountsInfo = null; this._accountsInfo = null;
this._tokenreg = null; this._tokenreg = null;
this._fetchingBalances = false;
this._fetchingTokens = false; this._fetchingTokens = false;
this._fetchedTokens = false; this._fetchedTokens = false;
this._tokenregSubId = null; this._tokenregSubId = null;
this._tokenregMetaSubId = null; this._tokenregMetaSubId = null;
// Throttled `retrieveTokens` function
// that gets called max once every 20s
this._throttledRetrieveTokens = throttle(
this._retrieveTokens,
20 * 1000,
{ trailing: true }
);
} }
start () { start () {
@ -72,6 +83,15 @@ export default class Balances {
return console.warn('_subscribeBlockNumber', error); 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(); this._retrieveTokens();
}) })
.catch((error) => { .catch((error) => {
@ -140,10 +160,16 @@ export default class Balances {
} }
_retrieveBalances () { _retrieveBalances () {
if (this._fetchingBalances) {
return;
}
if (!this._accountsInfo) { if (!this._accountsInfo) {
return; return;
} }
this._fetchingBalances = true;
const addresses = Object const addresses = Object
.keys(this._accountsInfo) .keys(this._accountsInfo)
.filter((address) => { .filter((address) => {
@ -161,9 +187,11 @@ export default class Balances {
}); });
this._store.dispatch(getBalances(this._balances)); this._store.dispatch(getBalances(this._balances));
this._fetchingBalances = false;
}) })
.catch((error) => { .catch((error) => {
console.warn('_retrieveBalances', error); console.warn('_retrieveBalances', error);
this._fetchingBalances = false;
}); });
} }

View File

@ -39,7 +39,7 @@ const initialState = {
}, },
netPort: new BigNumber(0), netPort: new BigNumber(0),
rpcSettings: {}, rpcSettings: {},
syncing: false, syncing: true,
isConnected: false, isConnected: false,
isConnecting: false, isConnecting: false,
isPingable: false, isPingable: false,