Move add-on-dispose! to IDisposable

And pass the reaction to the dispose function.
This commit is contained in:
Dan Holmsand 2016-06-07 16:35:17 +02:00
parent 30c9402fe9
commit bb2faefcbe
2 changed files with 12 additions and 13 deletions

View File

@ -325,14 +325,12 @@
;;;; reaction ;;;; reaction
(defprotocol IDisposable (defprotocol IDisposable
(dispose! [this])) (dispose! [this])
(add-on-dispose! [this f]))
(defprotocol IRunnable (defprotocol IRunnable
(run [this])) (run [this]))
(defprotocol IReaction
(add-on-dispose! [this f]))
(defn- handle-reaction-change [this sender old new] (defn- handle-reaction-change [this sender old new]
(._handle-change this sender old new)) (._handle-change this sender old new))
@ -455,13 +453,6 @@
(._run this false))))) (._run this false)))))
state) 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 IDisposable
(dispose! [this] (dispose! [this]
(let [s state (let [s state
@ -476,7 +467,13 @@
(.on-dispose this s)) (.on-dispose this s))
(when-some [a (.-on-dispose-arr this)] (when-some [a (.-on-dispose-arr this)]
(dotimes [i (alength a)] (dotimes [i (alength a)]
((aget a i)))))) ((aget a i) this)))))
(add-on-dispose! [this f]
;; f is called with the reaction as argument when it is no longer active
(if-some [a (.-on-dispose-arr this)]
(.push a f)
(set! (.-on-dispose-arr this) (array f))))
IEquiv IEquiv
(-equiv [o other] (identical? o other)) (-equiv [o other] (identical? o other))

View File

@ -248,7 +248,9 @@
res (rv/atom nil) res (rv/atom nil)
cns (rv/make-reaction #(reset! res @c) cns (rv/make-reaction #(reset! res @c)
:auto-run true)] :auto-run true)]
(rv/add-on-dispose! b #(reset! disposed true)) (rv/add-on-dispose! b (fn [r]
(is (= r b))
(reset! disposed true)))
(rv/add-on-dispose! c #(reset! disposed-c true)) (rv/add-on-dispose! c #(reset! disposed-c true))
(rv/add-on-dispose! cns #(reset! disposed-cns true)) (rv/add-on-dispose! cns #(reset! disposed-cns true))
@cns @cns