Make Reaction always call watches

Even if it is not executed automatically
This commit is contained in:
Dan Holmsand 2015-02-01 00:41:59 +01:00
parent dfd3e540f3
commit 744165737f
2 changed files with 22 additions and 6 deletions

View File

@ -252,8 +252,12 @@
IDeref
(-deref [this]
(if-not (or auto-run *ratom-context*)
(if dirty?
(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))

View File

@ -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))
))