Always queue updates in reactions, even when flushing

This commit is contained in:
Dan Holmsand 2015-10-07 11:32:42 +02:00
parent e6b32c6aee
commit e79132c1f3
6 changed files with 45 additions and 53 deletions

View File

@ -88,4 +88,5 @@
(.schedule render-queue)) (.schedule render-queue))
(defn schedule [] (defn schedule []
(.schedule render-queue)) (when (false? (.-scheduled? render-queue))
(.schedule render-queue)))

View File

@ -52,8 +52,8 @@
(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 derefed)) (set! (.-captured r) (array derefed))
(.push c derefed))))) (.push c derefed)))))
(defn- check-watches [old new] (defn- check-watches [old new]
(when debug (when debug
@ -94,7 +94,6 @@
;;; Queueing ;;; Queueing
(defonce ^:private rea-queue nil) (defonce ^:private rea-queue nil)
(def ^:private empty-context #js{})
(defn- rea-enqueue [r] (defn- rea-enqueue [r]
(when (nil? rea-queue) (when (nil? rea-queue)
@ -102,16 +101,14 @@
(batch/schedule)) (batch/schedule))
(.push rea-queue r)) (.push rea-queue r))
(defn- run-queue [q]
(set! rea-queue nil)
(dotimes [i (alength q)]
(let [r (aget q i)]
(._try-run r))))
(defn flush! [] (defn flush! []
(when-some [q rea-queue] (loop []
(binding [*ratom-context* empty-context] (let [q rea-queue]
(run-queue q)))) (when-not (nil? q)
(set! rea-queue nil)
(dotimes [i (alength q)]
(._try-run (aget q i)))
(recur)))))
(set! batch/ratom-flush flush!) (set! batch/ratom-flush flush!)
@ -375,15 +372,12 @@
(-deref this))) (-deref this)))
(_handle-change [this sender oldval newval] (_handle-change [this sender oldval newval]
(when-not (identical? oldval newval) (when-not (or (identical? oldval newval)
(if (nil? *ratom-context*) dirty?)
(do (set! dirty? true) (set! dirty? true)
(rea-enqueue this)) (if (nil? auto-run)
(if (nil? auto-run) (rea-enqueue this)
(when-not dirty? (auto-run this))))
(set! dirty? true)
(._run this))
(auto-run this)))))
(_update-watching [this derefed] (_update-watching [this derefed]
(let [new (set derefed) (let [new (set derefed)
@ -395,16 +389,14 @@
(-remove-watch w this)))) (-remove-watch w this))))
(_try-run [this other] (_try-run [this other]
(if (some? auto-run) (when (and dirty? (some? watching))
(auto-run this) (try
(when (and dirty? (some? watching)) (._run this)
(try (catch :default e
(._run this) ;; Just log error: it will most likely pop up again at deref time.
(catch :default e (error "Error in reaction:" e)
;; Just log error: it will most likely pop up again at deref time. (set! state nil)
(error "Error in reaction:" e) (notify-w this e nil)))))
(set! state nil)
(notify-w this e nil))))))
(_run [this] (_run [this]
(let [oldstate state (let [oldstate state
@ -567,13 +559,13 @@
(let [nite 100000 (let [nite 100000
a (atom 0) a (atom 0)
f (fn [] f (fn []
;; (ratom/with-let [x 1])
(quot @a 10)) (quot @a 10))
mid (make-reaction f) mid (make-reaction f)
res (track! (fn [] res (track! (fn []
;; @(ratom/track f) ;; (with-let [x 1])
;; @(track f)
(inc @mid) (inc @mid)
))] ))]
@res @res
(time (dotimes [x nite] (time (dotimes [x nite]
(swap! a inc) (swap! a inc)

View File

@ -41,7 +41,7 @@
(reset! start 1) (reset! start 1)
(r/flush) (r/flush)
(is (= @out 3)) (is (= @out 3))
(is (= @count 4)) (is (<= 2 @count 3))
(dispose const) (dispose const)
(is (= @start-base {:a {:b {:c 1}}})) (is (= @start-base {:a {:b {:c 1}}}))
(is (= (running) runs)))) (is (= (running) runs))))

View File

@ -24,17 +24,16 @@
(defn ratom-perf [] (defn ratom-perf []
(dbg "ratom-perf") (dbg "ratom-perf")
(set! rv/debug false) (set! rv/debug false)
(set! perf-check 0) (dotimes [_ 10]
(let [nite 100000 (let [nite 100000
a (rv/atom 0) a (rv/atom 0)
mid (reaction (inc @a)) mid (reaction (quot @a 10))
res (run! res (run!
(set! perf-check (inc perf-check)) (inc @mid))]
(inc @mid))] (time (dotimes [x nite]
(time (dotimes [x nite] (swap! a inc)
(swap! a inc))) (rv/flush!)))
(dispose res) (dispose res))))
(assert (= perf-check (inc nite)))))
(enable-console-print!) (enable-console-print!)
;; (ratom-perf) ;; (ratom-perf)
@ -57,7 +56,7 @@
(reset! start 1) (reset! start 1)
(r/flush) (r/flush)
(is (= @out 3)) (is (= @out 3))
(is (= @count 4)) (is (<= 2 @count 3))
(dispose const) (dispose const)
(is (= (running) runs)))) (is (= (running) runs))))

View File

@ -43,7 +43,7 @@
(is (= @count 1)) (is (= @count 1))
(sync) (sync)
(is (= @out 3)) (is (= @out 3))
(is (= @count 4)) (is (<= 2 @count 3))
(reset! start 2) (reset! start 2)
(dispose const) (dispose const)
(is (= (running) runs) "did dispose") (is (= (running) runs) "did dispose")

View File

@ -47,7 +47,7 @@
(reset! start 1) (reset! start 1)
(r/flush) (r/flush)
(is (= @out 8)) (is (= @out 8))
(is (= @count 4)) (is (<= 2 @count 3))
(dispose const) (dispose const)
(is (= (running) runs)))) (is (= (running) runs))))
@ -74,13 +74,13 @@
(is (= @count 1)) (is (= @count 1))
(sync) (sync)
(is (= @out 8)) (is (= @out 8))
(is (= @count 4)) (is (<= 2 @count 3))
(dispose const) (dispose const)
(swap! start inc) (swap! start inc)
(sync) (sync)
(is (= @count 4)) (is (<= 2 @count 3))
(is (= @const 11)) (is (= @const 11))
(is (= @count 5)) (is (<= 3 @count 4))
(is (= (running) runs)))) (is (= (running) runs))))
(deftest double-dependency (deftest double-dependency