Experiment with with-resource

This commit is contained in:
Dan Holmsand 2015-09-18 14:23:50 +02:00
parent 54ca0f927f
commit 897b766624
2 changed files with 52 additions and 7 deletions

View File

@ -12,3 +12,31 @@
:auto-run true)] :auto-run true)]
(deref co#) (deref co#)
co#)) co#))
(defmacro with-resource [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))
[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 ~bs
(set! (.-destroy o#) ~destroy)
~@forms))))

View File

@ -6,6 +6,8 @@
(declare ^:dynamic *ratom-context*) (declare ^:dynamic *ratom-context*)
(defn reactive? [] (some? *ratom-context*))
(defonce ^boolean debug false) (defonce ^boolean debug false)
(defonce ^boolean silent false) (defonce ^boolean silent false)
@ -109,7 +111,7 @@
(defonce cached-reactions {}) (defonce cached-reactions {})
(defn- cached-reaction [f key obj] (defn- cached-reaction [f key obj destroy]
(if-some [r (get cached-reactions key)] (if-some [r (get cached-reactions key)]
(-deref r) (-deref r)
(if (some? *ratom-context*) (if (some? *ratom-context*)
@ -117,12 +119,19 @@
f :on-dispose (fn [] f :on-dispose (fn []
(set! cached-reactions (set! cached-reactions
(dissoc cached-reactions key)) (dissoc cached-reactions key))
(set! (.-reaction obj) nil))) (when (some? obj)
(set! (.-reaction obj) nil))
(when (some-> destroy .-destroy)
(.destroy destroy))))
v (-deref r)] v (-deref r)]
(set! cached-reactions (assoc cached-reactions key r)) (set! cached-reactions (assoc cached-reactions key r))
(set! (.-reaction obj) r) (when (some? obj)
(set! (.-reaction obj) r))
v) v)
(f)))) (let [res (f)]
(when (some-> destroy .-destroy)
(.destroy destroy))
res))))
(deftype Monitor [f key ^:mutable reaction] (deftype Monitor [f key ^:mutable reaction]
IReactiveAtom IReactiveAtom
@ -131,7 +140,7 @@
(-deref [this] (-deref [this]
(if-some [r reaction] (if-some [r reaction]
(-deref r) (-deref r)
(cached-reaction f key this))) (cached-reaction f key this nil)))
IEquiv IEquiv
(-equiv [o other] (-equiv [o other]
@ -190,7 +199,7 @@
(let [f (if (satisfies? IDeref ratom) (let [f (if (satisfies? IDeref ratom)
#(get-in @ratom path) #(get-in @ratom path)
#(ratom path))] #(ratom path))]
(cached-reaction f [::cursor ratom path] this)))] (cached-reaction f [::cursor ratom path] this nil)))]
(._set-state this oldstate newstate) (._set-state this oldstate newstate)
newstate)) newstate))
@ -244,6 +253,15 @@
;;; with-resource support
(defn get-cached-values [key destroy]
(cached-reaction #(let [o #js{}]
(set! (.-values o) #js[])
o)
key nil destroy))
;;;; reaction ;;;; reaction
(defprotocol IDisposable (defprotocol IDisposable
@ -422,7 +440,6 @@
(-hash [this] (goog/getUid this))) (-hash [this] (goog/getUid this)))
;;; Queueing ;;; Queueing
;; Gets set up from batching ;; Gets set up from batching