From b17ff0a57600888af90eb5244a2c2e4362d2a54d Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sat, 26 Sep 2015 13:49:25 +0200 Subject: [PATCH] Pass state to on-dispose in Reaction Use to simplify with-let some more --- src/reagent/ratom.clj | 16 +++++++--------- src/reagent/ratom.cljs | 36 ++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/reagent/ratom.clj b/src/reagent/ratom.clj index a2ce1d2..4b67b7b 100644 --- a/src/reagent/ratom.clj +++ b/src/reagent/ratom.clj @@ -16,22 +16,20 @@ (defmacro with-let [bindings & body] (assert (vector? bindings)) - (let [v (gensym "bind-v") - key (str v) + (let [v (gensym "with-let") + k (keyword v) [forms destroy] (let [fin (last body)] (if (and (list? fin) (= 'finally (first fin))) [(butlast body) `(fn [] ~@(rest fin))] [body nil])) - destroy-obj (when destroy - `(cljs.core/js-obj)) asserting (if *assert* true false)] - `(let [destroy-obj# ~destroy-obj - ~v (reagent.ratom/with-let-value ~key destroy-obj#)] + `(let [~v (reagent.ratom/with-let-value ~k)] (when ~asserting (when-some [c# reagent.ratom/*ratom-context*] (when (== (.-generation ~v) (.-ratomGeneration c#)) - (d/error "Warning: The same with-let is being used more than once in the same reactive context.")) + (d/error "Warning: The same with-let is being used more " + "than once in the same reactive context.")) (set! (.-generation ~v) (.-ratomGeneration c#)))) (when (zero? (alength ~v)) (aset ~v 0 (let ~bindings @@ -39,8 +37,8 @@ (let [res# (do ~@forms)] (when-some [destroy# ~destroy] (if (reagent.ratom/reactive?) - (when (nil? (.-destroy destroy-obj#)) - (set! (.-destroy destroy-obj#) destroy#)) + (when (< (alength ~v) 2) + (aset ~v 1 destroy#)) (destroy#))) res#))))) ((aget ~v 0))))) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 2bc04c7..dd46828 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -135,14 +135,13 @@ (-deref r) (if (some? *ratom-context*) (let [r (make-reaction - f :on-dispose (fn [] + f :on-dispose (fn [x] (set! cached-reactions (dissoc cached-reactions key)) (when (some? obj) (set! (.-reaction obj) nil)) - (when-not (or (nil? destroy) - (nil? (.-destroy destroy))) - (.destroy destroy)) + (when-not (nil? destroy) + (destroy x)) nil)) v (-deref r)] (set! cached-reactions (assoc cached-reactions key r)) @@ -280,10 +279,14 @@ ;;; with-let support -(defn with-let-value [key destroy] +(defn with-let-destroy [v] + (when (< 1 (alength v)) + ((aget v 1)))) + +(defn with-let-value [key] (if-some [c *ratom-context*] (cached-reaction array [(reaction-key c) key] - nil destroy) + nil with-let-destroy) (array))) @@ -438,7 +441,7 @@ (-notify-watches this oldstate newstate)))) (when (and (some? on-dispose) (nil? watches)) - (on-dispose))) + (on-dispose state))) (do (notify-deref-watcher! this) (when-not (== dirtyness clean) @@ -449,12 +452,13 @@ (dispose! [this] (doseq [w watching] (remove-watch w this)) - (set! watching nil) - (set! state nil) - (set! auto-run nil) - (set! dirtyness dirty) - (when (some? on-dispose) - (on-dispose)) + (let [s state] + (set! watching nil) + (set! state nil) + (set! auto-run nil) + (set! dirtyness dirty) + (when (some? on-dispose) + (on-dispose s))) nil) IEquiv @@ -589,12 +593,10 @@ false nil)) (comment - (def perf-check 0) (defn ratom-perf [] (dbg "ratom-perf") (set! debug false) (dotimes [_ 10] - (set! perf-check 0) (let [nite 100000 a (atom 0) f (fn [] @@ -602,14 +604,12 @@ (quot @a 10)) mid (make-reaction f) res (make-reaction (fn [] - (set! perf-check (inc perf-check)) ;; @(track f) (inc @mid)) :auto-run true)] @res (time (dotimes [x nite] (swap! a inc))) - (dispose! res) - (assert (= perf-check (inc nite)))))) + (dispose! res)))) (enable-console-print!) (ratom-perf))