Allow deref of Reaction outside active Reaction

This commit is contained in:
Dan Holmsand 2015-01-30 21:20:02 +01:00
parent 99cc5ef506
commit 0e7f5518c0
2 changed files with 26 additions and 15 deletions

View File

@ -105,14 +105,15 @@
(= ratom (.-ratom other))
(= setf (.-setf other))))
Object
(_reaction [this]
(if (nil? reaction)
(set! reaction (make-reaction #(get-in @ratom path)))
reaction))
IDeref
(-deref [this]
(if (nil? *ratom-context*)
(get-in @ratom path)
(do
(if (nil? reaction)
(set! reaction (make-reaction #(get-in @ratom path))))
@reaction)))
(deref (._reaction this)))
IReset
(-reset! [a new-value]
@ -244,15 +245,15 @@
IDeref
(-deref [this]
;; TODO: relax this?
(when (not (or auto-run *ratom-context*))
(dbg [auto-run *ratom-context*]))
(assert (or auto-run *ratom-context*)
"Reaction derefed outside auto-running context")
(notify-deref-watcher! this)
(if dirty?
(run this)
state))
(if-not (or auto-run *ratom-context*)
(if dirty?
(set! state (f))
state)
(do
(notify-deref-watcher! this)
(if dirty?
(run this)
state))))
IDisposable
(dispose! [this]

View File

@ -229,3 +229,13 @@
(dispose b)
(is (= runs (running)))))
(deftest non-reactive-deref
(let [runs (running)
a (rv/atom 0)
b (rv/make-reaction #(+ 5 @a))]
(is (= @b 5))
(is (= runs (running)))
(reset! a 1)
(is (= @b 6))
(is (= runs (running)))))