aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test-wttrin-make-default.el45
-rw-r--r--wttrin.el17
2 files changed, 60 insertions, 2 deletions
diff --git a/tests/test-wttrin-make-default.el b/tests/test-wttrin-make-default.el
index c6f845b..623200c 100644
--- a/tests/test-wttrin-make-default.el
+++ b/tests/test-wttrin-make-default.el
@@ -61,6 +61,51 @@ unbound); persistence is left to `wttrin--savehist-register'."
(should (memq 'wttrin-favorite-location savehist-additional-variables))))
;;; --------------------------------------------------------------------------
+;;; mode-line refresh when the favorite changes
+;;; --------------------------------------------------------------------------
+
+;;; Normal Cases
+
+(ert-deftest test-wttrin--set-favorite-location-normal-mode-line-on-refreshes ()
+ "Normal: changing the favorite while the mode-line is active clears the
+stale cache and fetches fresh weather for the new location immediately."
+ (let ((wttrin-favorite-location "Oslo, NO")
+ (wttrin-mode-line-mode t)
+ (wttrin--mode-line-cache (cons 0.0 "Oslo, NO: sun"))
+ (fetched nil))
+ (cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
+ (lambda () (setq fetched t)))
+ ((symbol-function 'wttrin--mode-line-set-placeholder)
+ (lambda () nil)))
+ (wttrin--set-favorite-location "Paris, FR")
+ (should (null wttrin--mode-line-cache))
+ (should fetched))))
+
+;;; Boundary Cases
+
+(ert-deftest test-wttrin--set-favorite-location-boundary-mode-line-off-no-fetch ()
+ "Boundary: with the mode-line inactive, changing the favorite does not fetch."
+ (let ((wttrin-favorite-location "Oslo, NO")
+ (wttrin-mode-line-mode nil)
+ (fetched nil))
+ (cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
+ (lambda () (setq fetched t))))
+ (wttrin--set-favorite-location "Paris, FR")
+ (should-not fetched))))
+
+(ert-deftest test-wttrin--set-favorite-location-boundary-unchanged-no-fetch ()
+ "Boundary: re-promoting the current favorite does not refetch the mode-line."
+ (let ((wttrin-favorite-location "Paris, FR")
+ (wttrin-mode-line-mode t)
+ (fetched nil))
+ (cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
+ (lambda () (setq fetched t)))
+ ((symbol-function 'wttrin--mode-line-set-placeholder)
+ (lambda () nil)))
+ (wttrin--set-favorite-location "Paris, FR")
+ (should-not fetched))))
+
+;;; --------------------------------------------------------------------------
;;; wttrin-make-default
;;; --------------------------------------------------------------------------
diff --git a/wttrin.el b/wttrin.el
index c4b31bc..89db893 100644
--- a/wttrin.el
+++ b/wttrin.el
@@ -857,8 +857,11 @@ Persistence is handled by `wttrin--savehist-register', which registers the
variable when savehist loads and again on `savehist-save-hook', so the value
survives restarts without the Emacs custom-variable mechanism, and setting it
here works whether or not savehist is loaded."
- (setq wttrin-favorite-location location)
- (setq wttrin--location-history (delete location wttrin--location-history)))
+ (let ((changed (not (equal location wttrin-favorite-location))))
+ (setq wttrin-favorite-location location)
+ (setq wttrin--location-history (delete location wttrin--location-history))
+ (when (and changed (bound-and-true-p wttrin-mode-line-mode))
+ (wttrin--mode-line-refresh-now))))
(defun wttrin-make-default ()
"Make the location shown in this buffer the favorite (persisted) default.
@@ -1049,6 +1052,16 @@ Force-refresh cache and update tooltip without opening buffer."
(format "Fetching weather for %s..."
(or (wttrin--favorite-location-display-name) "favorite"))))
+(defun wttrin--mode-line-refresh-now ()
+ "Discard the cached mode-line weather and fetch fresh data immediately.
+Called when `wttrin-favorite-location' changes so the mode-line stops
+showing the previous location's weather instead of waiting for the next
+scheduled refresh."
+ (setq wttrin--mode-line-cache nil)
+ (setq wttrin--mode-line-rendered-stale nil)
+ (wttrin--mode-line-set-placeholder)
+ (wttrin--mode-line-fetch-weather))
+
(defvar wttrin--buffer-refresh-timer nil
"Timer object for proactive buffer cache refresh.")