mirror of https://github.com/status-im/reagent.git
Add add-on-dispose! to Reaction
This commit is contained in:
parent
5f50240c42
commit
072cce4844
|
@ -1,4 +1,4 @@
|
|||
(defproject reagent "0.6.0-alpha2"
|
||||
(defproject reagent "0.6.0-SNAPSHOT"
|
||||
:url "http://github.com/reagent-project/reagent"
|
||||
:license {:name "MIT"}
|
||||
:description "A simple ClojureScript interface to React"
|
||||
|
|
|
@ -330,6 +330,9 @@
|
|||
(defprotocol IRunnable
|
||||
(run [this]))
|
||||
|
||||
(defprotocol IReaction
|
||||
(add-on-dispose! [this f]))
|
||||
|
||||
(defn- handle-reaction-change [this sender old new]
|
||||
(._handle-change this sender old new))
|
||||
|
||||
|
@ -452,6 +455,13 @@
|
|||
(._run this false)))))
|
||||
state)
|
||||
|
||||
IReaction
|
||||
(add-on-dispose! [this f]
|
||||
;; f is called without arguments when Reaction is no longer active
|
||||
(if-some [a (.-on-dispose-arr this)]
|
||||
(.push a f)
|
||||
(set! (.-on-dispose-arr this) (array f))))
|
||||
|
||||
IDisposable
|
||||
(dispose! [this]
|
||||
(let [s state
|
||||
|
@ -463,7 +473,10 @@
|
|||
(doseq [w (set wg)]
|
||||
(-remove-watch w this))
|
||||
(when (some? (.-on-dispose this))
|
||||
(.on-dispose this s))))
|
||||
(.on-dispose this s))
|
||||
(when-some [a (.-on-dispose-arr this)]
|
||||
(dotimes [i (alength a)]
|
||||
((aget a i))))))
|
||||
|
||||
IEquiv
|
||||
(-equiv [o other] (identical? o other))
|
||||
|
|
|
@ -233,6 +233,55 @@
|
|||
(is (= @disposed-cns true))
|
||||
(is (= runs (running))))))
|
||||
|
||||
(deftest test-add-dispose
|
||||
(dotimes [x testite]
|
||||
(let [runs (running)
|
||||
a (rv/atom 0)
|
||||
disposed (rv/atom nil)
|
||||
disposed-c (rv/atom nil)
|
||||
disposed-cns (rv/atom nil)
|
||||
count-b (rv/atom 0)
|
||||
b (rv/make-reaction (fn []
|
||||
(swap! count-b inc)
|
||||
(inc @a)))
|
||||
c (rv/make-reaction #(if (< @a 1) (inc @b) (dec @a)))
|
||||
res (rv/atom nil)
|
||||
cns (rv/make-reaction #(reset! res @c)
|
||||
:auto-run true)]
|
||||
(rv/add-on-dispose! b #(reset! disposed true))
|
||||
(rv/add-on-dispose! c #(reset! disposed-c true))
|
||||
(rv/add-on-dispose! cns #(reset! disposed-cns true))
|
||||
@cns
|
||||
(is (= @res 2))
|
||||
(is (= (+ 4 runs) (running)))
|
||||
(is (= @count-b 1))
|
||||
(reset! a -1)
|
||||
(r/flush)
|
||||
(is (= @res 1))
|
||||
(is (= @disposed nil))
|
||||
(is (= @count-b 2))
|
||||
(is (= (+ 4 runs) (running)) "still running")
|
||||
(reset! a 2)
|
||||
(r/flush)
|
||||
(is (= @res 1))
|
||||
(is (= @disposed true))
|
||||
(is (= (+ 2 runs) (running)) "less running count")
|
||||
|
||||
(reset! disposed nil)
|
||||
(reset! a -1)
|
||||
(r/flush)
|
||||
(is (= 1 @res) "should be one again")
|
||||
(is (= @disposed nil))
|
||||
(reset! a 2)
|
||||
(r/flush)
|
||||
(is (= @res 1))
|
||||
(is (= @disposed true))
|
||||
(dispose cns)
|
||||
(is (= @disposed-c true))
|
||||
(is (= @disposed-cns true))
|
||||
(is (= runs (running))))))
|
||||
|
||||
|
||||
(deftest test-on-set
|
||||
(let [runs (running)
|
||||
a (rv/atom 0)
|
||||
|
|
Loading…
Reference in New Issue