1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Widgets refresh via goroutine

All widgets now refresh their own data using their own internal go
routine. This allows them to set their own update schedule (where
RefreshInterval is the time in seconds between refreshes).

The app uses a goroutine to redraw itself once a second.
This commit is contained in:
Chris Cummer
2018-03-30 10:10:21 -07:00
committed by Chris Cummer
parent 25898dcb24
commit ab4774c86a
5 changed files with 103 additions and 25 deletions

View File

@@ -10,16 +10,19 @@ import (
)
type Widget struct {
RefreshedAt time.Time
View *tview.TextView
RefreshedAt time.Time
RefreshInterval int
View *tview.TextView
}
func NewWidget() *Widget {
widget := Widget{
RefreshedAt: time.Now(),
RefreshedAt: time.Now(),
RefreshInterval: 600,
}
widget.addView()
go widget.refresher()
return &widget
}
@@ -118,6 +121,21 @@ func icon(data *owm.CurrentWeatherData) string {
return icon
}
func (widget *Widget) refresher() {
tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Second)
quit := make(chan struct{})
for {
select {
case <-tick.C:
widget.Refresh()
case <-quit:
tick.Stop()
return
}
}
}
func (widget *Widget) refreshedAt() string {
return widget.RefreshedAt.Format("15:04:05")
}