Cleanup new deref watching a bit

This commit is contained in:
Dan Holmsand 2015-10-03 22:36:40 +02:00
parent c8b86af635
commit dabffb86b6
1 changed files with 24 additions and 27 deletions

View File

@ -31,29 +31,32 @@
(recur (inc i)) (recur (inc i))
false)))))) false))))))
(defn- ^boolean in-arr [a x]
(not (or (nil? a)
(== -1 (.indexOf a x)))))
(defn- in-context [obj f] (defn- in-context [obj f]
(binding [*ratom-context* obj] (binding [*ratom-context* obj]
(f))) (f)))
(defn- deref-capture [f r] (defn- deref-capture [f r]
(let [watch (.-watching r)] (set! (.-captured r) nil)
(set! (.-captured r) nil) (when (dev?)
(when (dev?) (set! (.-ratomGeneration r)
(set! (.-ratomGeneration r) (set! generation (inc generation))))
(set! generation (inc generation)))) (let [res (in-context r f)
(let [res (in-context r f) c (.-captured r)]
capt (.-captured r)] (set! (.-dirty? r) false)
(set! (.-dirty? r) false) (when-not (arr-eq c (.-watching r))
(when-not (arr-eq capt watch) (._update-watching r c))
(._update-watching r capt)) res))
res)))
(defn- notify-deref-watcher! [derefable] (defn- notify-deref-watcher! [derefable]
(when-some [r *ratom-context*] (when-some [r *ratom-context*]
(let [c (.-captured r)] (let [c (.-captured r)]
(if (nil? c) (if (nil? c)
(set! (.-captured r) (array derefable)) (set! (.-captured r) (array derefable))
(when (== -1 (.indexOf c derefable)) (when-not (in-arr c derefable)
(.push c derefable)))))) (.push c derefable))))))
(defn- check-watches [old new] (defn- check-watches [old new]
@ -62,15 +65,14 @@
new) new)
(defn- add-w [this key f] (defn- add-w [this key f]
(set! (.-watchesArr this) nil)
(let [w (.-watches this)] (let [w (.-watches this)]
(set! (.-watches this) (check-watches w (assoc w key f))))) (set! (.-watches this) (check-watches w (assoc w key f)))
(set! (.-watchesArr this) nil)))
(defn- remove-w [this key] (defn- remove-w [this key]
(set! (.-watchesArr this) nil) (let [w (.-watches this)]
(let [w (.-watches this) (set! (.-watches this) (check-watches w (dissoc w key)))
r -running] (set! (.-watchesArr this) nil)))
(set! (.-watches this) (check-watches w (dissoc w key)))))
(defn- notify-w [this old new] (defn- notify-w [this old new]
(let [w (.-watchesArr this) (let [w (.-watchesArr this)
@ -79,9 +81,7 @@
w)] w)]
(when (nil? w) (when (nil? w)
;; Copy watches to an array for speed ;; Copy watches to an array for speed
(reduce-kv (fn [_ k f] (reduce-kv #(.push a %2 %3)
(.push a k)
(.push a f))
nil (.-watches this))) nil (.-watches this)))
(let [len (alength a)] (let [len (alength a)]
(loop [i 0] (loop [i 0]
@ -89,8 +89,7 @@
(let [k (aget a i) (let [k (aget a i)
f (aget a (inc i))] f (aget a (inc i))]
(f k this old new)) (f k this old new))
(recur (+ 2 i)))))) (recur (+ 2 i)))))))
nil)
(defn- pr-atom [a writer opts s] (defn- pr-atom [a writer opts s]
(-write writer (str "#<" s " ")) (-write writer (str "#<" s " "))
@ -397,12 +396,10 @@
wg watching] wg watching]
(set! watching der) (set! watching der)
(doseq [w der] (doseq [w der]
(when (or (nil? wg) (when-not (in-arr wg w)
(== -1 (.indexOf wg w)))
(-add-watch w this handle-reaction-change))) (-add-watch w this handle-reaction-change)))
(doseq [w wg] (doseq [w wg]
(when (or (nil? der) (when-not (in-arr der w)
(== -1 (.indexOf der w)))
(-remove-watch w this)))) (-remove-watch w this))))
nil) nil)