mirror of https://github.com/status-im/reagent.git
Rename with-resource to with-kept and clean up
This commit is contained in:
parent
0b553ef6bd
commit
ea7cd26cf5
|
@ -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#)))))
|
||||
|
|
Loading…
Reference in New Issue