Skip to content

Commit

Permalink
fix: weather data refreshing shouldn't block rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Feb 10, 2022
1 parent f3b14b4 commit a6c9375
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions widget_weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type WeatherData struct {
unit string

refresh time.Time
ready bool
fresh bool

response string
responseMutex sync.RWMutex
Expand All @@ -56,7 +56,6 @@ func (w *WeatherData) Condition() (string, error) {
w.responseMutex.RLock()
defer w.responseMutex.RUnlock()

w.ready = false
if strings.Contains(w.response, "Unknown location") {
fmt.Fprintln(os.Stderr, "unknown location:", w.location)
return "", nil
Expand All @@ -75,7 +74,6 @@ func (w *WeatherData) Temperature() (string, error) {
w.responseMutex.RLock()
defer w.responseMutex.RUnlock()

w.ready = false
if strings.Contains(w.response, "Unknown location") {
fmt.Fprintln(os.Stderr, "unknown location:", w.location)
return "", nil
Expand All @@ -89,20 +87,29 @@ func (w *WeatherData) Temperature() (string, error) {
return strings.Replace(wttr[1], "+", "", 1), nil
}

// Ready returns true when weather data is available.
func (w *WeatherData) Ready() bool {
// Fresh returns true when new weather data is available.
func (w *WeatherData) Fresh() bool {
w.responseMutex.RLock()
defer w.responseMutex.RUnlock()

return w.ready
return w.fresh
}

// Fetch retrieves weather data when required.
func (w *WeatherData) Fetch() {
// Reset marks the data as stale, so that it will be fetched again.
func (w *WeatherData) Reset() {
w.responseMutex.Lock()
defer w.responseMutex.Unlock()

if time.Since(w.refresh) < time.Minute*15 {
w.fresh = false
}

// Fetch retrieves weather data when required.
func (w *WeatherData) Fetch() {
w.responseMutex.RLock()
lastRefresh := w.refresh
w.responseMutex.RUnlock()

if time.Since(lastRefresh) < time.Minute*15 {
return
}
verbosef("Refreshing weather data...")
Expand All @@ -122,9 +129,12 @@ func (w *WeatherData) Fetch() {
return
}

w.responseMutex.Lock()
defer w.responseMutex.Unlock()

w.refresh = time.Now()
w.response = string(body)
w.ready = true
w.fresh = true
}

// NewWeatherWidget returns a new WeatherWidget.
Expand Down Expand Up @@ -154,7 +164,7 @@ func NewWeatherWidget(bw *BaseWidget, opts WidgetConfig) (*WeatherWidget, error)

// RequiresUpdate returns true when the widget wants to be repainted.
func (w *WeatherWidget) RequiresUpdate() bool {
return w.data.Ready() || w.ButtonWidget.RequiresUpdate()
return w.data.Fresh() || w.ButtonWidget.RequiresUpdate()
}

// Update renders the widget.
Expand All @@ -170,6 +180,9 @@ func (w *WeatherWidget) Update() error {
return w.render(w.dev, nil)
}

// don't trigger updates until new weather data is available
w.data.Reset()

var iconName string
switch cond {
case "mm", "mmm": // cloudy
Expand Down

0 comments on commit a6c9375

Please sign in to comment.