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) g.SetManagerFunc(vm.Layout)
// Start RPC calling routine // Start RPC calling routine
go peers.Fetch(g) go peers.FetchLoop(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err) log.Panicln(err)

View File

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