Fix stupid with-let bug

This commit is contained in:
Dan Holmsand 2015-09-27 00:07:19 +02:00
parent 4f629d0a34
commit 6e3a2b415d
3 changed files with 38 additions and 13 deletions

View File

@ -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#)))))

View File

@ -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)

View File

@ -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)