From af7622ce172d548e5525a5dd74d1e46eb0408805 Mon Sep 17 00:00:00 2001 From: George Lima Date: Thu, 30 May 2019 01:07:40 -0300 Subject: [PATCH] chore(status-pill): avoid requests queue --- app/components/status-pill.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/components/status-pill.js b/app/components/status-pill.js index 51b4d49..6c25e96 100644 --- a/app/components/status-pill.js +++ b/app/components/status-pill.js @@ -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 { timer: ?IntervalID = null; - constructor(props) { + requestOnTheFly: boolean = false; + + constructor(props: Props) { super(props); this.state = { @@ -113,20 +116,18 @@ class Component extends PureComponent { } 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 { 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);