Get rid of active?

This commit is contained in:
Dan Holmsand 2015-09-10 20:40:37 +02:00
parent 67a724b540
commit 0b77d9af31
3 changed files with 30 additions and 37 deletions

View File

@ -30,6 +30,10 @@
(conj (if (nil? captured) #{} captured)
derefable))))))
(defn- check-watches [old new]
(when debug
(swap! -running + (- (count new) (count old))))
new)
;;; Atom
@ -83,9 +87,9 @@
nil)
nil watches))
(-add-watch [this key f]
(set! watches (assoc watches key f)))
(set! watches (check-watches watches (assoc watches key f))))
(-remove-watch [this key]
(set! watches (dissoc watches key)))
(set! watches (check-watches watches (dissoc watches key))))
IHash
(-hash [this] (goog/getUid this)))
@ -203,7 +207,6 @@
(def dirty 2)
(deftype Reaction [f ^:mutable state ^:mutable ^number dirtyness
^:mutable ^boolean active?
^:mutable watching ^:mutable watches
auto-run on-set on-dispose ^:mutable ^boolean norun?]
IAtom
@ -216,11 +219,11 @@
nil)
nil watches))
(-add-watch [this k wf]
(set! watches (assoc watches k wf)))
(-add-watch [this key f]
(set! watches (check-watches watches (assoc watches key f))))
(-remove-watch [this k]
(set! watches (dissoc watches k))
(-remove-watch [this key]
(set! watches (check-watches watches (dissoc watches key)))
(when (and (empty? watches)
(nil? auto-run))
(dispose! this)))
@ -266,20 +269,19 @@
(set! dirtyness clean))))
(-handle-change [this sender oldval newval]
(when active?
(let [old-dirty dirtyness
new-dirty (if (identical? oldval newval)
(if (instance? Reaction sender)
maybe-dirty clean)
dirty)]
(when (> new-dirty old-dirty)
(set! dirtyness new-dirty)
(if (some? auto-run)
(when-not norun?
(-check-clean this)
(when-not (== dirtyness clean)
(auto-run this)))
(-notify-watches this state state))))))
(let [old-dirty dirtyness
new-dirty (if (identical? oldval newval)
(if (instance? Reaction sender)
maybe-dirty clean)
dirty)]
(when (> new-dirty old-dirty)
(set! dirtyness new-dirty)
(if (some? auto-run)
(when-not norun?
(-check-clean this)
(when-not (== dirtyness clean)
(auto-run this)))
(-notify-watches this state state)))))
(-update-watching [this derefed]
(doseq [w derefed]
@ -299,9 +301,6 @@
derefed (captured this)]
(when (not= derefed watching)
(-update-watching this derefed))
(when-not active?
(when debug (swap! -running inc))
(set! active? true))
(set! norun? false)
(set! dirtyness clean)
(set! state res)
@ -332,9 +331,6 @@
(set! watching nil)
(set! state nil)
(set! dirtyness dirty)
(when active?
(when debug (swap! -running dec))
(set! active? false))
(when on-dispose
(on-dispose)))
@ -352,13 +348,10 @@
(defn make-reaction [f & {:keys [auto-run on-set on-dispose derefed]}]
(let [runner (if (= auto-run true) run auto-run)
active (not (nil? derefed))
dirty (if (not active) dirty clean)
reaction (Reaction. f nil dirty active
nil nil
dirty (if (nil? derefed) dirty clean)
reaction (Reaction. f nil dirty nil nil
runner on-set on-dispose false)]
(when-not (nil? derefed)
(when debug (swap! -running inc))
(-update-watching reaction derefed))
reaction))

View File

@ -193,14 +193,14 @@
:on-dispose #(reset! disposed-cns true))]
@cns
(is (= @res 2))
(is (= (+ 4 runs) (running)))
(is (= (+ 5 runs) (running)))
(is (= @count-b 1))
(is (= {:a 0 :b 0} @a-base))
(reset! a -1)
(is (= @res 1))
(is (= @disposed nil))
(is (= @count-b 2))
(is (= (+ 4 runs) (running)) "still running")
(is (= (+ 5 runs) (running)) "still running")
(is (= {:a -1 :b 0} @a-base))
(reset! a 2)
(is (= @res 1))

View File

@ -26,7 +26,7 @@
(time (dotimes [x nite]
(swap! a inc)))
(dispose res)
(assert (= perf-check nite))))
(assert (= perf-check (inc nite)))))
(enable-console-print!)
;; (ratom-perf)
@ -193,13 +193,13 @@
:on-dispose #(reset! disposed-cns true))]
@cns
(is (= @res 2))
(is (= (+ 3 runs) (running)))
(is (= (+ 4 runs) (running)))
(is (= @count-b 1))
(reset! a -1)
(is (= @res 1))
(is (= @disposed nil))
(is (= @count-b 2))
(is (= (+ 3 runs) (running)) "still running")
(is (= (+ 4 runs) (running)) "still running")
(reset! a 2)
(is (= @res 1))
(is (= @disposed true))