Merge pull request #3591 from ethcore/ng-sync-ws

Don't fetch balances on every new block if syncing
This commit is contained in:
Gav Wood 2016-11-23 19:58:14 +01:00 committed by GitHub
commit 0501658d1b
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';
@ -38,11 +40,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 () {
@ -73,6 +84,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) => {
@ -135,10 +155,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) => {
@ -156,9 +182,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,