Simplify capture logic

This commit is contained in:
Dan Holmsand 2015-10-02 14:05:54 +02:00
parent 9a13446eea
commit 4007e6db74

View File

@ -20,10 +20,8 @@
(+ @-running)) (+ @-running))
(defn capture-derefed [f obj] (defn capture-derefed [f obj]
(set! (.-cljsCaptured obj) nil) (set! (.-cljsCaptured obj) (.-watching obj))
;; (if-not (nil? (.-watching obj)) (set! (.-reaPos obj) 0)
;; (set! (.-reaPos obj) 0))
(set! (.-reaPos obj) -1)
(when (dev?) (when (dev?)
(set! (.-ratomGeneration obj) (set! (.-ratomGeneration obj)
(set! generation (inc generation)))) (set! generation (inc generation))))
@ -31,18 +29,19 @@
(f))) (f)))
(defn captured [obj] (defn captured [obj]
(when-not (and (== -1 (.-reaPos obj)) (let [c (.-cljsCaptured obj)]
(nil? (.-cljsCaptured obj))) (when-not (nil? c)
obj)) (let [p (.-reaPos obj)]
(when (or (== p -1)
(== p (alength c)))
obj)))))
(defn- -captured [obj] (defn- -captured [obj]
(let [c (.-cljsCaptured obj)] (let [obj (captured obj)]
(set! (.-cljsCaptured obj) nil) (when-not (nil? obj)
(if (nil? c) (let [c (.-cljsCaptured obj)]
(when (== (inc (.-reaPos obj)) (set! (.-cljsCaptured obj) nil)
(some-> (.-watching obj) alength)) c))))
(.-watching obj))
c)))
(defn- add-item [a x] (defn- add-item [a x]
(when (== -1 (.indexOf a x)) (when (== -1 (.indexOf a x))
@ -52,22 +51,18 @@
(when-some [obj *ratom-context*] (when-some [obj *ratom-context*]
(let [c (.-cljsCaptured obj)] (let [c (.-cljsCaptured obj)]
(if (nil? c) (if (nil? c)
(do (set! (.-reaPos obj) -1)
(set! (.-cljsCaptured obj) (array derefable)))
;; Try to avoid allocating new array ;; Try to avoid allocating new array
(let [p (inc (.-reaPos obj)) (let [p (.-reaPos obj)]
w (.-watching obj)] (if (and (not (== p -1))
(if (and (not (nil? w)) (identical? derefable (aget c p)))
(identical? derefable (aget w p))) (set! (.-reaPos obj) (inc p))
(set! (.-reaPos obj) p) (if (== p -1)
(set! (.-cljsCaptured obj) (add-item c derefable)
(if (nil? w) (let [c1 (set! (.-cljsCaptured obj) (.slice c 0 p))]
(array derefable) (add-item c1 derefable)
(doto (.slice w 0 p) (set! (.-reaPos obj) -1))))))))
(add-item derefable))))))
(add-item c derefable))
#_(if (nil? c)
(set! (.-cljsCaptured obj) (array derefable))
(when (== -1 (.indexOf c derefable))
(.push c derefable)))))
nil) nil)
(defn- ^number arr-len [x] (defn- ^number arr-len [x]