Extract fetch to its own function

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-26 20:23:36 -04:00 committed by Jakub
parent c635fdd0c4
commit 0c50c2d05c
2 changed files with 12 additions and 7 deletions

View File

@ -73,7 +73,7 @@ func main() {
g.SetManagerFunc(vm.Layout)
// Start RPC calling routine
go peers.Fetch(g)
go peers.FetchLoop(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)

View File

@ -22,23 +22,28 @@ func NewPeersState(host string, port int) *PeersState {
return &PeersState{c: c}
}
func (p *PeersState) Fetch(g *gocui.Gui) {
func (p *PeersState) FetchLoop(g *gocui.Gui) {
for {
select {
case <-threadDone:
return
default:
peers, err := p.c.getPeers()
if err != nil {
log.Panicln(err)
}
p.list = peers
peers := p.Fetch()
writePeers(g, peers)
}
<-time.After(interval * time.Second)
}
}
func (p *PeersState) Fetch() []Peer {
peers, err := p.c.getPeers()
if err != nil {
log.Panicln(err)
}
p.list = peers
return peers
}
func writePeers(g *gocui.Gui, peers []Peer) {
g.Update(func(g *gocui.Gui) error {
v, err := g.View("main")