From 744165737f20c4e5ae06bf0bdc9f4bf4506ccc2f Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 1 Feb 2015 00:41:59 +0100 Subject: [PATCH] Make Reaction always call watches Even if it is not executed automatically --- src/reagent/ratom.cljs | 12 ++++++++---- test/testcursor.cljs | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index de48485..c12b83b 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -252,8 +252,12 @@ IDeref (-deref [this] (if-not (or auto-run *ratom-context*) - (if dirty? - (set! state (f)) + (do + (when dirty? + (let [oldstate state] + (set! state (f)) + (when-not (identical? oldstate state) + (call-watches this watches oldstate state)))) state) (do (notify-deref-watcher! this) @@ -265,7 +269,7 @@ (dispose! [this] (doseq [w watching] (remove-watch w this)) - (set! watching #{}) + (set! watching nil) (set! state nil) (set! dirty? true) (when active? @@ -291,7 +295,7 @@ active (not (nil? derefed)) dirty (not active) reaction (Reaction. f nil dirty active - nil {} + nil nil runner on-set on-dispose)] (when-not (nil? derefed) (when debug (swap! -running inc)) diff --git a/test/testcursor.cljs b/test/testcursor.cljs index c8d15b2..35dbdc6 100644 --- a/test/testcursor.cljs +++ b/test/testcursor.cljs @@ -372,13 +372,25 @@ (is (= (:key @witness) :w)) ;; cursor reports that the reaction is the current atom, ;; but I guess that's ok - (is (= @(:ref @witness) @test-cursor)) (is (= (:old @witness) "old")) (is (= (:new @witness) "new")) + (is (= @(:ref @witness) @test-cursor)) + (is (= (:new @witness) "new")) + + (reset! test-atom {:a {:b {:c {:d "newer"}}}}) + ;; watch doesn't run until the value is realized + (is (= (:new @witness) "new")) + (is (= @test-cursor "newer")) + @test-cursor + (is (= (:old @witness) "new")) + (is (= (:new @witness) "newer")) + @test-cursor + (is (= (:old @witness) "new")) + (is (= (:new @witness) "newer")) ;; can we remove the watch? (remove-watch test-cursor :w) (reset! test-cursor "removed") - (is (= (:new @witness) "new")) ;; shouldn't have changed + (is (= (:new @witness) "newer")) ;; shouldn't have changed (is (= (running) runs)) ))