From 0c50c2d05c5301e2e65763a79ca36068190249d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Wed, 26 Jun 2019 20:23:36 -0400 Subject: [PATCH] Extract fetch to its own function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- main.go | 2 +- peers.go | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index cf618f0..e8f9afe 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/peers.go b/peers.go index 243b9ab..66459c4 100644 --- a/peers.go +++ b/peers.go @@ -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")