Smarter Status Redux dispatch #3240

This commit is contained in:
Nicolas Gotchac 2016-11-17 18:44:01 +01:00
parent b79d65c0f3
commit 50473a69f4

View File

@ -15,11 +15,16 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { statusBlockNumber, statusCollection, statusLogs } from './statusActions'; import { statusBlockNumber, statusCollection, statusLogs } from './statusActions';
import { isEqual } from 'lodash';
export default class Status { export default class Status {
constructor (store, api) { constructor (store, api) {
this._api = api; this._api = api;
this._store = store; this._store = store;
this.__pingable = false;
this._apiStatus = {};
this._status = {};
} }
start () { start () {
@ -34,7 +39,9 @@ export default class Status {
this._api.parity this._api.parity
.enode() .enode()
.then((enode) => { .then((enode) => {
this._store.dispatch(statusCollection({ enode })); if (this._store.state.nodeStatus.enode !== enode) {
this._store.dispatch(statusCollection({ enode }));
}
}) })
.catch(() => { .catch(() => {
window.setTimeout(() => { window.setTimeout(() => {
@ -58,8 +65,12 @@ export default class Status {
} }
_pollPing = () => { _pollPing = () => {
const dispatch = (status, timeout = 500) => { const dispatch = (pingable, timeout = 500) => {
this._store.dispatch(statusCollection({ isPingable: status })); if (pingable !== this._pingable) {
this._pingable = pingable;
this._store.dispatch(statusCollection({ isPingable: pingable }));
}
setTimeout(this._pollPing, timeout); setTimeout(this._pollPing, timeout);
}; };
@ -79,13 +90,23 @@ export default class Status {
} }
_pollStatus = () => { _pollStatus = () => {
const { secureToken, isConnected, isConnecting, needsToken } = this._api; const { isConnected, isConnecting, needsToken, secureToken } = this._api;
const nextTimeout = (timeout = 1000) => { const nextTimeout = (timeout = 1000) => {
setTimeout(this._pollStatus, timeout); setTimeout(this._pollStatus, timeout);
}; };
this._store.dispatch(statusCollection({ isConnected, isConnecting, needsToken, secureToken })); const apiStatus = {
isConnected,
isConnecting,
needsToken,
secureToken
};
if (!isEqual(apiStatus, this._apiStatus)) {
this._store.dispatch(statusCollection(apiStatus));
this._apiStatus = apiStatus;
}
if (!isConnected) { if (!isConnected) {
nextTimeout(250); nextTimeout(250);
@ -111,7 +132,7 @@ export default class Status {
.then(([clientVersion, coinbase, defaultExtraData, extraData, gasFloorTarget, hashrate, minGasPrice, netChain, netPeers, netPort, nodeName, rpcSettings, syncing, traceMode]) => { .then(([clientVersion, coinbase, defaultExtraData, extraData, gasFloorTarget, hashrate, minGasPrice, netChain, netPeers, netPort, nodeName, rpcSettings, syncing, traceMode]) => {
const isTest = netChain === 'morden' || netChain === 'testnet'; const isTest = netChain === 'morden' || netChain === 'testnet';
this._store.dispatch(statusCollection({ const status = {
clientVersion, clientVersion,
coinbase, coinbase,
defaultExtraData, defaultExtraData,
@ -127,7 +148,12 @@ export default class Status {
syncing, syncing,
isTest, isTest,
traceMode traceMode
})); };
if (!isEqual(status, this._status)) {
this._store.dispatch(statusCollection(status));
this._status = status;
}
}) })
.catch((error) => { .catch((error) => {
console.error('_pollStatus', error); console.error('_pollStatus', error);