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

View File

@ -229,3 +229,13 @@
(dispose b) (dispose b)
(is (= runs (running))))) (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)))))