pond: set state once in ConnMgr.getActualState

This commit is contained in:
Łukasz Magiera 2019-07-25 16:40:01 +02:00
parent 583f4e02cb
commit 4381448c04

View File

@ -2,8 +2,7 @@ import React from 'react';
import Cristal from 'react-cristal' import Cristal from 'react-cristal'
async function awaitReducer(prev, c) { async function awaitReducer(prev, c) {
await prev return {...await prev, ...await c}
await c
} }
class ConnMgr extends React.Component { class ConnMgr extends React.Component {
@ -19,29 +18,29 @@ class ConnMgr extends React.Component {
this.state = {conns: {}, lock: true} this.state = {conns: {}, lock: true}
this.getActualState() this.getActualState()
setInterval(this.getActualState, 2000) setInterval(this.getActualState, 500)
} }
async getActualState() { async getActualState() {
const nodes = this.props.nodes const nodes = this.props.nodes
let keys = Object.keys(nodes) let keys = Object.keys(nodes)
await keys.filter((_, i) => i > 0).map(async (kfrom, i) => { const newConns = await keys.filter((_, i) => i > 0).map(async (kfrom, i) => {
await keys.filter((_, j) => i >= j).map(async kto => { return await keys.filter((_, j) => i >= j).map(async kto => {
const fromNd = this.props.nodes[kfrom] const fromNd = this.props.nodes[kfrom]
const toNd = this.props.nodes[kto] const toNd = this.props.nodes[kto]
const connectedness = await fromNd.conn.call('Filecoin.NetConnectedness', [toNd.peerid]) const connectedness = await fromNd.conn.call('Filecoin.NetConnectedness', [toNd.peerid])
this.setState(prev => ({conns: {...prev.conns, [`${kfrom},${kto}`]: connectedness === 1}})) return {[`${kfrom},${kto}`]: connectedness === 1}
}).reduce(awaitReducer) }).reduce(awaitReducer, Promise.resolve({}))
}).reduce(awaitReducer) }).reduce(awaitReducer, Promise.resolve({}))
this.setState({lock: false}) this.setState({conns: newConns, lock: false})
} }
async connect(action, from, to) { async connect(action, from, to, noupdate) {
const fromNd = this.props.nodes[from] const fromNd = this.props.nodes[from]
const toNd = this.props.nodes[to] const toNd = this.props.nodes[to]
@ -53,7 +52,8 @@ class ConnMgr extends React.Component {
await fromNd.conn.call('Filecoin.NetDisconnect', [toNd.peerid]) await fromNd.conn.call('Filecoin.NetDisconnect', [toNd.peerid])
} }
this.setState(prev => ({conns: {...prev.conns, [`${from},${to}`]: action}})) if (!noupdate)
this.setState(prev => ({conns: {...prev.conns, [`${from},${to}`]: action}}))
} }
connectAll(discon) { connectAll(discon) {
@ -63,7 +63,7 @@ class ConnMgr extends React.Component {
keys.filter((_, i) => i > 0).forEach((kfrom, i) => { keys.filter((_, i) => i > 0).forEach((kfrom, i) => {
keys.filter((_, j) => i >= j).forEach((kto, i) => { keys.filter((_, j) => i >= j).forEach((kto, i) => {
this.connect(!discon, kfrom, kto) this.connect(!discon, kfrom, kto, true)
}) })
}) })
} }