From bb2faefcbeeaa689f38c26ffde42a5a85d22c3ba Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Tue, 7 Jun 2016 16:35:17 +0200 Subject: [PATCH] Move add-on-dispose! to IDisposable And pass the reaction to the dispose function. --- src/reagent/ratom.cljs | 21 +++++++++------------ test/reagenttest/testratom.cljs | 4 +++- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 51cb189..8f50bb8 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -325,14 +325,12 @@ ;;;; reaction (defprotocol IDisposable - (dispose! [this])) + (dispose! [this]) + (add-on-dispose! [this f])) (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)) @@ -455,13 +453,6 @@ (._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 @@ -476,7 +467,13 @@ (.on-dispose this s)) (when-some [a (.-on-dispose-arr this)] (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 (-equiv [o other] (identical? o other)) diff --git a/test/reagenttest/testratom.cljs b/test/reagenttest/testratom.cljs index fec6b84..bf83036 100644 --- a/test/reagenttest/testratom.cljs +++ b/test/reagenttest/testratom.cljs @@ -248,7 +248,9 @@ 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! b (fn [r] + (is (= r b)) + (reset! disposed true))) (rv/add-on-dispose! c #(reset! disposed-c true)) (rv/add-on-dispose! cns #(reset! disposed-cns true)) @cns