Revert experiment with lazy reaction

Needs more surgery to get this to work properly
This commit is contained in:
Dan Holmsand 2015-02-04 22:14:04 +01:00
parent d39656624d
commit c862792624
4 changed files with 49 additions and 39 deletions

View File

@ -215,8 +215,7 @@
(when on-set (when on-set
(set! dirty? true) (set! dirty? true)
(on-set oldval newval)) (on-set oldval newval))
(when (not (identical? oldval newval)) (-notify-watches a oldval newval)
(-notify-watches a oldval newval))
newval)) newval))
ISwap ISwap
@ -231,13 +230,9 @@
IComputedImpl IComputedImpl
(-handle-change [this sender oldval newval] (-handle-change [this sender oldval newval]
(when (and active? (not dirty?) (when (and active? (not dirty?) (not (identical? oldval newval)))
(or (instance? Reaction sender)
(not (identical? oldval newval))))
(set! dirty? true) (set! dirty? true)
(if auto-run ((or auto-run run) this)))
((or auto-run run) this)
(-notify-watches this state state))))
(-update-watching [this derefed] (-update-watching [this derefed]
(doseq [w derefed] (doseq [w derefed]
@ -260,8 +255,7 @@
(set! active? true)) (set! active? true))
(set! dirty? false) (set! dirty? false)
(set! state res) (set! state res)
(when (not (identical? oldstate state)) (-notify-watches this oldstate state)
(-notify-watches this oldstate state))
res)) res))
IDeref IDeref

View File

@ -50,10 +50,9 @@
(is (= @c3 1)) (is (= @c3 1))
(is (= @c3-count 1) "t1") (is (= @c3-count 1) "t1")
(swap! start inc) (swap! start inc)
;; ought to be 2, ideally (is (= @c3-count 2) "t2")
(is (= @c3-count 3) "t2")
(is (= @c3 2)) (is (= @c3 2))
(is (= @c3-count 3) "t3") (is (= @c3-count 2) "t3")
(is (= @start-base {:a {:b {:c 1}}})) (is (= @start-base {:a {:b {:c 1}}}))
(dispose c3) (dispose c3)
(is (= (running) runs)))) (is (= (running) runs))))
@ -121,14 +120,13 @@
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (is (= @res (+ 10 @a)))
(is (<= 2 @b-changed 3)) (is (<= 2 @b-changed 3))
;; Should be 2, ideally? (is (= @c-changed 2))
(is (<= 2 @c-changed 3))
(is (= @a-base {:test {:unsubscribe 3 :value 42}})) (is (= @a-base {:test {:unsubscribe 3 :value 42}}))
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (is (= @res (+ 10 @a)))
(is (<= 2 @b-changed 3)) (is (<= 2 @b-changed 3))
(is (<= 2 @c-changed 3)) (is (= @c-changed 2))
(is (= @a-base {:test {:unsubscribe 3 :value 42}})) (is (= @a-base {:test {:unsubscribe 3 :value 42}}))
(reset! a -1) (reset! a -1)

View File

@ -50,21 +50,18 @@
c3-count (rv/atom 0) c3-count (rv/atom 0)
c1 (reaction @start 1) c1 (reaction @start 1)
c2 (reaction @start) c2 (reaction @start)
c2' (reaction
(swap! c3-count inc)
(+ @c1 @c2))
c3 (rv/make-reaction c3 (rv/make-reaction
(fn [] @c2') (fn []
(swap! c3-count inc)
(+ @c1 @c2))
:auto-run true)] :auto-run true)]
(is (= @c3-count 0)) (is (= @c3-count 0))
(is (= @c3 1)) (is (= @c3 1))
(is (= @c3-count 1) "t1") (is (= @c3-count 1) "t1")
(swap! start inc) (swap! start inc)
;; this ought to be 2, but probably not worth the (is (= @c3-count 2) "t2")
;; trouble optimizing for
(is (= @c3-count 3) "t2")
(is (= @c3 2)) (is (= @c3 2))
(is (= @c3-count 3) "t3") (is (= @c3-count 2) "t3")
(dispose c3) (dispose c3)
(is (= (running) runs)))) (is (= (running) runs))))
@ -124,13 +121,12 @@
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (is (= @res (+ 10 @a)))
(is (<= 2 @b-changed 3)) (is (<= 2 @b-changed 3))
;; ought to be 2, ideally (is (= @c-changed 2))
(is (<= 2 @c-changed 3))
(reset! a 3) (reset! a 3)
(is (= @res (+ 10 @a))) (is (= @res (+ 10 @a)))
(is (<= 2 @b-changed 3)) (is (<= 2 @b-changed 3))
(is (<= 2 @c-changed 3)) (is (= @c-changed 2))
(reset! a -1) (reset! a -1)
(is (= @res (+ 2 @a))) (is (= @res (+ 2 @a)))
@ -242,15 +238,15 @@
(is (= @b 6)) (is (= @b 6))
(is (= runs (running))))) (is (= runs (running)))))
(deftest catching ;; (deftest catching
(let [runs (running) ;; (let [runs (running)
a (rv/atom false) ;; a (rv/atom false)
catch-count (atom 0) ;; catch-count (atom 0)
b (reaction (if @a (throw {}))) ;; b (reaction (if @a (throw {})))
c (run! (try @b (catch js/Object e ;; c (run! (try @b (catch js/Object e
(swap! catch-count inc))))] ;; (swap! catch-count inc))))]
(is (= @catch-count 0)) ;; (is (= @catch-count 0))
(reset! a false) ;; (reset! a false)
(is (= @catch-count 0)) ;; (is (= @catch-count 0))
(reset! a true) ;; (reset! a true)
(is (= @catch-count 1)))) ;; (is (= @catch-count 1))))

View File

@ -173,3 +173,25 @@
(r/flush) (r/flush)
(is (found-in #"value:4:" div)) (is (found-in #"value:4:" div))
(is (= @ran 7))))))) (is (= @ran 7)))))))
(deftest test-cursor
(let [state (atom {:a 0
:b 0})
a-count (atom 0)
b-count (atom 0)
derefer (fn derefer [cur count]
(swap! count inc)
[:div @cur])
comp (fn test-cursor []
[:div
[derefer (r/cursor state [:a]) a-count]
[derefer (r/cursor state [:b]) b-count]])]
(with-mounted-component [comp]
(fn [c div]
(is (= @a-count 1))
(is (= @b-count 1))
(swap! state update-in [:a] inc)
(r/flush)
(is (= @a-count 2))
(is (= @b-count 1))))))