Rename with-resource to with-kept and clean up

This commit is contained in:
Dan Holmsand 2015-09-19 11:27:50 +02:00
parent 0b553ef6bd
commit ea7cd26cf5
1 changed files with 16 additions and 21 deletions

View File

@ -13,34 +13,29 @@
(deref co#)
co#))
(defmacro with-resource [bindings & body]
(defmacro with-kept [bindings & body]
(assert (vector? bindings))
(let [v (gensym "res-v")
init (gensym "res-init-v")
bs (into [] (map-indexed (fn [i x]
(if (even? i)
x
(let [pos (-> (/ i 2) int)]
`(if ~init
(aget ~v ~pos)
(aset ~v ~pos ~x)))))
bindings))
(let [v (gensym "bind-v")
bs (->> bindings
(map-indexed (fn [i x]
(if (even? i)
x
(let [pos (quot i 2)]
`(if (> (alength ~v) ~pos)
(aget ~v ~pos)
(aset ~v ~pos ~x))))))
vec)
[forms destroy] (let [fin (last body)]
(if (and (list? fin)
(= 'finally (first fin)))
[(butlast body) `(fn [] ~@(rest fin))]
[body nil]))]
`(let [o# (cljs.core/js-obj)
~v (reagent.ratom/get-cached-values (quote ~v) o#)
~init (if (true? (.-init ~v))
true
(do
(set! (.-init ~v) true)
false))]
`(let [destroy-obj# (cljs.core/js-obj)
~v (reagent.ratom/get-cached-values (quote ~v) destroy-obj#)]
(let ~bs
(let [dest# ~destroy
(let [destroy# ~destroy
res# (do ~@forms)]
(if (reagent.ratom/reactive?)
(set! (.-destroy o#) ~destroy)
(dest#))
(set! (.-destroy destroy-obj#) destroy#)
(destroy#))
res#)))))