Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3591 from ethcore/ng-sync-ws
Browse files Browse the repository at this point in the history
Don't fetch balances on every new block if syncing
  • Loading branch information
gavofyork authored Nov 23, 2016
2 parents 273165e + cd88f2a commit 0501658
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions js/src/redux/providers/balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { throttle } from 'lodash';

import { getBalances, getTokens } from './balancesActions';
import { setAddressImage } from './imagesActions';

Expand All @@ -38,11 +40,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 () {
Expand Down Expand Up @@ -73,6 +84,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) => {
Expand Down Expand Up @@ -135,10 +155,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) => {
Expand All @@ -156,9 +182,11 @@ export default class Balances {
});

this._store.dispatch(getBalances(this._balances));
this._fetchingBalances = false;
})
.catch((error) => {
console.warn('_retrieveBalances', error);
this._fetchingBalances = false;
});
}

Expand Down
2 changes: 1 addition & 1 deletion js/src/redux/providers/statusReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const initialState = {
},
netPort: new BigNumber(0),
rpcSettings: {},
syncing: false,
syncing: true,
isConnected: false,
isConnecting: false,
isPingable: false,
Expand Down

0 comments on commit 0501658

Please sign in to comment.