Get rid of silent in ratom

And some small cleanup
This commit is contained in:
Dan Holmsand 2015-10-04 12:53:23 +02:00
parent 9a2d632826
commit de1e4d4ad5
4 changed files with 54 additions and 57 deletions

View File

@ -8,12 +8,11 @@
(declare ^:dynamic *ratom-context*)
(defonce ^boolean debug false)
(defonce ^boolean silent false)
(defonce ^:private generation 0)
(defonce ^:private -running (clojure.core/atom 0))
(defn ^boolean reactive? []
(not (nil? *ratom-context*)))
(some? *ratom-context*))
;;; Utilities
@ -189,14 +188,14 @@
(as-> (aget o cache-key) _
(dissoc _ k)
(aset o cache-key _))
(when-not (nil? obj)
(when (some? obj)
(set! (.-reaction obj) nil))
(when-not (nil? destroy)
(when (some? destroy)
(destroy x))))
v (-deref r)]
(aset o cache-key (assoc m k r))
(when debug (swap! -running inc))
(when-not (nil? obj)
(when (some? obj)
(set! (.-reaction obj) r))
v)))))
@ -260,7 +259,7 @@
(_set-state [this oldstate newstate]
(when-not (identical? oldstate newstate)
(set! state newstate)
(when-not (nil? watches)
(when (some? watches)
(notify-w this oldstate newstate))))
IDeref
@ -348,9 +347,9 @@
(-notify-watches [this old new] (notify-w this old new))
(-add-watch [this key f] (add-w this key f))
(-remove-watch [this key]
(let [n (count watches)]
(let [was-empty (empty? watches)]
(remove-w this key)
(when (and (pos? n)
(when (and (not was-empty)
(empty? watches)
(nil? auto-run))
(dispose! this))))
@ -396,14 +395,14 @@
(-remove-watch w this))))
(_try-run [this other]
(if-not (nil? auto-run)
(if (some? auto-run)
(auto-run this)
(when (and dirty? (not (nil? watching)))
(when (and dirty? (some? watching))
(try
(._run this)
(catch :default e
;; Just log error: it will most likely pop up again at deref time.
(when-not silent (error "Error in reaction:" e))
(error "Error in reaction:" e)
(set! state nil)
(notify-w this e nil))))))
@ -420,15 +419,15 @@
res))
(_set-opts [this {:keys [auto-run on-set on-dispose no-cache]}]
(when-not (nil? auto-run)
(when (some? auto-run)
(set! (.-auto-run this) (case auto-run
true run
auto-run)))
(when-not (nil? on-set)
(when (some? on-set)
(set! (.-on-set this) on-set))
(when-not (nil? on-dispose)
(when (some? on-dispose)
(set! (.-on-dispose this) on-dispose))
(when-not (nil? no-cache)
(when (some? no-cache)
(set! (.-nocache? this) no-cache)))
IRunnable
@ -463,7 +462,7 @@
(set! dirty? true)
(doseq [w (set wg)]
(-remove-watch w this))
(when-not (nil? (.-on-dispose this))
(when (some? (.-on-dispose this))
(.on-dispose this s))))
IEquiv
@ -523,7 +522,7 @@
(let [oldval state]
(set! changed true)
(set! state newval)
(when-not (nil? watches)
(when (some? watches)
(notify-w this oldval newval))
(callback newval)
newval))

View File

@ -1,7 +1,7 @@
(ns reagenttest.testratom
(:require [cljs.test :as t :refer-macros [is deftest testing]]
[reagent.ratom :as rv :refer-macros [run! reaction]]
[reagent.debug :refer-macros [dbg]]
[reagent.debug :as debug :refer-macros [dbg]]
[reagent.core :as r]))
(defn fixture [f]
@ -269,18 +269,18 @@
b (reaction (if @a (throw (js/Error. "fail"))))
c (run! (try @b (catch :default e
(swap! catch-count inc))))]
(set! rv/silent true)
(is (= @catch-count 0))
(reset! a false)
(r/flush)
(is (= @catch-count 0))
(reset! a true)
(r/flush)
(is (= @catch-count 1))
(reset! a false)
(r/flush)
(is (= @catch-count 1))
(set! rv/silent false)
(debug/track-warnings
(fn []
(is (= @catch-count 0))
(reset! a false)
(r/flush)
(is (= @catch-count 0))
(reset! a true)
(r/flush)
(is (= @catch-count 1))
(reset! a false)
(r/flush)
(is (= @catch-count 1))))
(dispose c)
(is (= runs (running)))))

View File

@ -1,7 +1,7 @@
(ns reagenttest.testratomasync
(:require [cljs.test :as t :refer-macros [is deftest testing]]
[reagent.ratom :as rv :refer-macros [run! reaction]]
[reagent.debug :refer-macros [dbg]]
[reagent.debug :as debug :refer-macros [dbg]]
[reagent.core :as r]))
(defn fixture [f]
@ -282,15 +282,15 @@
b (reaction (if @a (throw (js/Error. "reaction fail"))))
c (ar (fn [] (try @b (catch js/Object e
(swap! catch-count inc)))))]
(set! rv/silent true)
(is (= @catch-count 0))
(reset! a false)
@c
(is (= @catch-count 0))
(reset! a true)
(is (= @catch-count 0))
(sync)
(is (= @catch-count 1))
(set! rv/silent false)
(dispose c)
(debug/track-warnings
(fn []
(is (= @catch-count 0))
(reset! a false)
@c
(is (= @catch-count 0))
(reset! a true)
(is (= @catch-count 0))
(sync)
(is (= @catch-count 1))
(dispose c)))
(is (= runs (running)))))

View File

@ -1,7 +1,7 @@
(ns reagenttest.testtrack
(:require [cljs.test :as t :refer-macros [is deftest testing]]
[reagent.ratom :as rv :refer [track] :refer-macros [run! reaction]]
[reagent.debug :refer-macros [dbg]]
[reagent.debug :as debug :refer-macros [dbg]]
[reagent.core :as r]))
(defn fixture [f]
@ -213,19 +213,17 @@
b (track #(if @a (throw (js/Error. "fail"))))
c (run! (try @b (catch :default e
(swap! catch-count inc))))]
(set! rv/silent true)
(is (= @catch-count 0))
(reset! a false)
(sync)
(is (= @catch-count 0))
(reset! a true)
(sync)
(is (= @catch-count 1))
(reset! a false)
(sync)
(is (= @catch-count 1))
(set! rv/silent false)
(debug/track-warnings
(fn []
(is (= @catch-count 0))
(reset! a false)
(sync)
(is (= @catch-count 0))
(reset! a true)
(sync)
(is (= @catch-count 1))
(reset! a false)
(sync)
(is (= @catch-count 1))))
(dispose c)
(is (= runs (running)))))