Faster compare of derefed reactions, and refactor

This commit is contained in:
Dan Holmsand 2015-10-03 12:55:20 +02:00
parent 70dd2a5166
commit a31c410e08

View File

@ -32,48 +32,43 @@
false)))))) false))))))
(defn- in-context [obj f] (defn- in-context [obj f]
(set! (.-cljsCapPos obj) 0) (set! (.-capPos obj) 0)
(binding [*ratom-context* obj] (binding [*ratom-context* obj]
(f))) (f)))
(defn- deref-capture [f obj] (defn- deref-capture [f obj]
(let [watch (.-watching obj)] (let [watch (.-watching obj)]
(set! (.-cljsCaptured obj) (.-watching obj)) (set! (.-captured obj) (.-watching obj))
(when (dev?) (when (dev?)
(set! (.-ratomGeneration obj) (set! (.-ratomGeneration obj)
(set! generation (inc generation)))) (set! generation (inc generation))))
(let [res (in-context obj f) (let [res (in-context obj f)
capt (.-cljsCaptured obj)] capt (.-captured obj)]
(set! (.-cljsCaptured obj) nil)
(set! (.-dirty? obj) false) (set! (.-dirty? obj) false)
(when-not (arr-eq capt watch) (when-not (or (identical? capt watch)
(arr-eq capt watch))
(._update-watching obj capt)) (._update-watching obj capt))
res))) res)))
(defn check-derefs [f]
(let [ctx (js-obj)
res (in-context ctx f)]
[res (some? (.-cljsCaptured ctx))]))
(defn- add-item [a x] (defn- add-item [a x]
(when (== -1 (.indexOf a x)) (when (== -1 (.indexOf a x))
(.push a x))) (.push a x)))
(defn- notify-deref-watcher! [derefable] (defn- notify-deref-watcher! [derefable]
(when-some [obj *ratom-context*] (when-some [obj *ratom-context*]
(let [c (.-cljsCaptured obj)] (let [c (.-captured obj)]
(if (nil? c) (if (nil? c)
(do (set! (.-cljsCapPos obj) -1) (do (set! (.-capPos obj) -1)
(set! (.-cljsCaptured obj) (array derefable))) (set! (.-captured obj) (array derefable)))
;; Try to avoid allocating new array ;; Try to avoid allocating new array
(let [p (.-cljsCapPos obj)] (let [p (.-capPos obj)]
(if (== p -1) (if (== p -1)
(add-item c derefable) (add-item c derefable)
(if (identical? derefable (aget c p)) (if (identical? derefable (aget c p))
(set! (.-cljsCapPos obj) (inc p)) (set! (.-capPos obj) (inc p))
(let [c1 (set! (.-cljsCaptured obj) (.slice c 0 p))] (let [c1 (set! (.-captured obj) (.slice c 0 p))]
(add-item c1 derefable) (add-item c1 derefable)
(set! (.-cljsCapPos obj) -1))))))))) (set! (.-capPos obj) -1)))))))))
(defn- check-watches [old new] (defn- check-watches [old new]
(when debug (when debug
@ -531,6 +526,11 @@
(aset obj key rea)) (aset obj key rea))
res)) res))
(defn check-derefs [f]
(let [ctx (js-obj)
res (in-context ctx f)]
[res (some? (.-captured ctx))]))
;;; wrap ;;; wrap