mirror of https://github.com/status-im/reagent.git
Make Reaction always call watches
Even if it is not executed automatically
This commit is contained in:
parent
dfd3e540f3
commit
744165737f
|
@ -252,8 +252,12 @@
|
||||||
IDeref
|
IDeref
|
||||||
(-deref [this]
|
(-deref [this]
|
||||||
(if-not (or auto-run *ratom-context*)
|
(if-not (or auto-run *ratom-context*)
|
||||||
(if dirty?
|
(do
|
||||||
|
(when dirty?
|
||||||
|
(let [oldstate state]
|
||||||
(set! state (f))
|
(set! state (f))
|
||||||
|
(when-not (identical? oldstate state)
|
||||||
|
(call-watches this watches oldstate state))))
|
||||||
state)
|
state)
|
||||||
(do
|
(do
|
||||||
(notify-deref-watcher! this)
|
(notify-deref-watcher! this)
|
||||||
|
@ -265,7 +269,7 @@
|
||||||
(dispose! [this]
|
(dispose! [this]
|
||||||
(doseq [w watching]
|
(doseq [w watching]
|
||||||
(remove-watch w this))
|
(remove-watch w this))
|
||||||
(set! watching #{})
|
(set! watching nil)
|
||||||
(set! state nil)
|
(set! state nil)
|
||||||
(set! dirty? true)
|
(set! dirty? true)
|
||||||
(when active?
|
(when active?
|
||||||
|
@ -291,7 +295,7 @@
|
||||||
active (not (nil? derefed))
|
active (not (nil? derefed))
|
||||||
dirty (not active)
|
dirty (not active)
|
||||||
reaction (Reaction. f nil dirty active
|
reaction (Reaction. f nil dirty active
|
||||||
nil {}
|
nil nil
|
||||||
runner on-set on-dispose)]
|
runner on-set on-dispose)]
|
||||||
(when-not (nil? derefed)
|
(when-not (nil? derefed)
|
||||||
(when debug (swap! -running inc))
|
(when debug (swap! -running inc))
|
||||||
|
|
|
@ -372,13 +372,25 @@
|
||||||
(is (= (:key @witness) :w))
|
(is (= (:key @witness) :w))
|
||||||
;; cursor reports that the reaction is the current atom,
|
;; cursor reports that the reaction is the current atom,
|
||||||
;; but I guess that's ok
|
;; but I guess that's ok
|
||||||
(is (= @(:ref @witness) @test-cursor))
|
|
||||||
(is (= (:old @witness) "old"))
|
(is (= (:old @witness) "old"))
|
||||||
(is (= (:new @witness) "new"))
|
(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?
|
;; can we remove the watch?
|
||||||
(remove-watch test-cursor :w)
|
(remove-watch test-cursor :w)
|
||||||
(reset! test-cursor "removed")
|
(reset! test-cursor "removed")
|
||||||
(is (= (:new @witness) "new")) ;; shouldn't have changed
|
(is (= (:new @witness) "newer")) ;; shouldn't have changed
|
||||||
(is (= (running) runs))
|
(is (= (running) runs))
|
||||||
))
|
))
|
||||||
|
|
Loading…
Reference in New Issue