mirror of https://github.com/status-im/reagent.git
Allow deref of Reaction outside active Reaction
This commit is contained in:
parent
99cc5ef506
commit
0e7f5518c0
|
@ -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]
|
||||
|
|
|
@ -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)))))
|
||||
|
|
Loading…
Reference in New Issue