From 6e3a2b415d276da4418bfa32c5468c660fcc99a6 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sun, 27 Sep 2015 00:07:19 +0200 Subject: [PATCH] Fix stupid with-let bug --- src/reagent/ratom.clj | 27 +++++++++++++++++---------- src/reagent/ratom.cljs | 6 +++--- test/reagenttest/testreagent.cljs | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/reagent/ratom.clj b/src/reagent/ratom.clj index 15cb777..cee521f 100644 --- a/src/reagent/ratom.clj +++ b/src/reagent/ratom.clj @@ -18,6 +18,16 @@ (assert (vector? bindings)) (let [v (gensym "with-let") k (keyword v) + init (gensym "init") + bs (into [init `(zero? (alength ~v))] + (map-indexed (fn [i x] + (if (even? i) + x + (let [j (quot i 2)] + `(if ~init + (aset ~v ~j ~x) + (aget ~v ~j))))) + bindings)) [forms destroy] (let [fin (last body)] (if (and (list? fin) (= 'finally (first fin))) @@ -26,21 +36,18 @@ add-destroy (when destroy `(let [destroy# ~destroy] (if (reagent.ratom/reactive?) - (when (< (alength ~v) 2) - (aset ~v 1 destroy#)) + (when (nil? (.-destroy ~v)) + (set! (.-destroy ~v) destroy#)) (destroy#)))) asserting (if *assert* true false)] - `(let [~v (reagent.ratom/with-let-value ~k)] + `(let [~v (reagent.ratom/with-let-values ~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.")) (set! (.-generation ~v) (.-ratomGeneration c#)))) - (when (zero? (alength ~v)) - (aset ~v 0 (let ~bindings - (fn [] - (let [res# (do ~@forms)] - ~add-destroy - res#))))) - ((aget ~v 0))))) + (let ~bs + (let [res# (do ~@forms)] + ~add-destroy + res#))))) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 0f16918..629d6fc 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -269,10 +269,10 @@ ;;; with-let support (defn with-let-destroy [v] - (when (< 1 (alength v)) - ((aget v 1)))) + (when-some [f (.-destroy v)] + (f))) -(defn with-let-value [key] +(defn with-let-values [key] (if-some [c *ratom-context*] (cached-reaction array [(reaction-key c) key] nil with-let-destroy) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 88a4b16..5ef631a 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -602,6 +602,24 @@ (is (= [1 0] [@n1 @n2])))) (is (= [1 1] [@n1 @n2]))))) +(deftest with-let-arg + (when isClient + (let [a (atom 0) + s (r/atom "foo") + f (fn [x] + (r/with-let [] + (reset! a x) + [:div x])) + c (fn [] + (r/with-let [] + [f @s]))] + (with-mounted-component [c] + (fn [_ div] + (is (= @a "foo")) + (reset! s "bar") + (r/flush) + (is (= @a "bar"))))))) + (deftest with-let-non-reactive (let [n1 (atom 0) n2 (atom 0)