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

@ -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] (defn flush! []
(loop []
(let [q rea-queue]
(when-not (nil? q)
(set! rea-queue nil) (set! rea-queue nil)
(dotimes [i (alength q)] (dotimes [i (alength q)]
(let [r (aget q i)] (._try-run (aget q i)))
(._try-run r)))) (recur)))))
(defn flush! []
(when-some [q rea-queue]
(binding [*ratom-context* empty-context]
(run-queue q))))
(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)
(rea-enqueue this))
(if (nil? auto-run)
(when-not dirty?
(set! dirty? true) (set! dirty? true)
(._run this)) (if (nil? auto-run)
(auto-run this))))) (rea-enqueue this)
(auto-run this))))
(_update-watching [this derefed] (_update-watching [this derefed]
(let [new (set derefed) (let [new (set derefed)
@ -395,8 +389,6 @@
(-remove-watch w this)))) (-remove-watch w this))))
(_try-run [this other] (_try-run [this other]
(if (some? auto-run)
(auto-run this)
(when (and dirty? (some? watching)) (when (and dirty? (some? watching))
(try (try
(._run this) (._run this)
@ -404,7 +396,7 @@
;; Just log error: it will most likely pop up again at deref time. ;; Just log error: it will most likely pop up again at deref time.
(error "Error in reaction:" e) (error "Error in reaction:" e)
(set! state nil) (set! state nil)
(notify-w this e nil)))))) (notify-w this e nil)))))
(_run [this] (_run [this]
(let [oldstate state (let [oldstate state
@ -567,11 +559,11 @@
(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

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)
(dispose res) (rv/flush!)))
(assert (= perf-check (inc nite))))) (dispose res))))
(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