chore(status-pill): avoid requests queue

This commit is contained in:
George Lima 2019-05-30 01:07:40 -03:00
parent b30a0ccf50
commit af7622ce17
1 changed files with 24 additions and 7 deletions

View File

@ -99,12 +99,15 @@ type State = {
showTooltip: boolean,
};
const MINUTE_IN_MILI = 60000;
const INTERVAL_AFTER_READY = 60000;
const INTERVAL_BEFORE_READY = 10000;
class Component extends PureComponent<Props, State> {
timer: ?IntervalID = null;
constructor(props) {
requestOnTheFly: boolean = false;
constructor(props: Props) {
super(props);
this.state = {
@ -113,20 +116,18 @@ class Component extends PureComponent<Props, State> {
}
componentDidMount() {
const { getBlockchainStatus } = this.props;
this.timer = setInterval(() => getBlockchainStatus(), 2000);
this.timer = setInterval(() => this.updateStatus(), INTERVAL_BEFORE_READY);
}
componentDidUpdate(prevProps: Props) {
const { getBlockchainStatus, nodeSyncType } = this.props;
const { nodeSyncType } = this.props;
if (
prevProps.nodeSyncType === NODE_SYNC_TYPES.SYNCING
&& nodeSyncType === NODE_SYNC_TYPES.READY
) {
// if the status is "ready", we can increase the interval to avoid useless rpc calls
this.cleanUpdateInterval();
this.timer = setInterval(() => getBlockchainStatus(), MINUTE_IN_MILI);
this.timer = setInterval(() => this.updateStatus(), INTERVAL_AFTER_READY);
}
}
@ -134,6 +135,22 @@ class Component extends PureComponent<Props, State> {
this.cleanUpdateInterval();
}
updateStatus = () => {
if (this.requestOnTheFly) return;
this.requestOnTheFly = true;
const { getBlockchainStatus } = this.props;
getBlockchainStatus()
.then(() => {
this.requestOnTheFly = false;
})
.catch(() => {
this.requestOnTheFly = false;
});
};
cleanUpdateInterval = () => {
if (this.timer) {
clearInterval(this.timer);