From 65a50cdd0d15423883d127ff753b84c525328a13 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Mon, 29 Apr 2019 09:43:42 -0700 Subject: [PATCH] fix: avoid startup delay updating node count in large clusters --- src/App.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/App.js b/src/App.js index 9a2a5814..375304e0 100755 --- a/src/App.js +++ b/src/App.js @@ -235,29 +235,32 @@ class App extends Component { try { const oldNodes = this.state.nodes; const newNodes = await this.connection.getClusterNodes(); + const nodes = []; let modified = oldNodes.length !== newNodes.length; + + const maybeSetState = () => { + if (modified) { + this.setState({nodes}); + modified = false; + } + }; for (const newNode of newNodes) { const oldNode = oldNodes.find(node => node.id === newNode.id); if (oldNode) { - newNode.lat = oldNode.lat; - newNode.lng = oldNode.lng; - newNode.terminated = oldNode.terminated; + nodes.push(oldNode); } else { const ip = newNode.gossip.split(':')[0]; const [lat, lng] = await geoip(ip); newNode.lat = lat; newNode.lng = lng; + nodes.push(newNode); modified = true; } + maybeSetState(); } - - if (modified) { - console.log('newNodes', newNodes); - this.setState({nodes: newNodes}); - } + maybeSetState(); } catch (err) { - this.setState({nodes: []}); console.log('getClusterNodes failed:', err.message); }