mirror of https://github.com/status-im/reagent.git
parent
9a2d632826
commit
de1e4d4ad5
|
@ -8,12 +8,11 @@
|
||||||
|
|
||||||
(declare ^:dynamic *ratom-context*)
|
(declare ^:dynamic *ratom-context*)
|
||||||
(defonce ^boolean debug false)
|
(defonce ^boolean debug false)
|
||||||
(defonce ^boolean silent false)
|
|
||||||
(defonce ^:private generation 0)
|
(defonce ^:private generation 0)
|
||||||
(defonce ^:private -running (clojure.core/atom 0))
|
(defonce ^:private -running (clojure.core/atom 0))
|
||||||
|
|
||||||
(defn ^boolean reactive? []
|
(defn ^boolean reactive? []
|
||||||
(not (nil? *ratom-context*)))
|
(some? *ratom-context*))
|
||||||
|
|
||||||
|
|
||||||
;;; Utilities
|
;;; Utilities
|
||||||
|
@ -189,14 +188,14 @@
|
||||||
(as-> (aget o cache-key) _
|
(as-> (aget o cache-key) _
|
||||||
(dissoc _ k)
|
(dissoc _ k)
|
||||||
(aset o cache-key _))
|
(aset o cache-key _))
|
||||||
(when-not (nil? obj)
|
(when (some? obj)
|
||||||
(set! (.-reaction obj) nil))
|
(set! (.-reaction obj) nil))
|
||||||
(when-not (nil? destroy)
|
(when (some? destroy)
|
||||||
(destroy x))))
|
(destroy x))))
|
||||||
v (-deref r)]
|
v (-deref r)]
|
||||||
(aset o cache-key (assoc m k r))
|
(aset o cache-key (assoc m k r))
|
||||||
(when debug (swap! -running inc))
|
(when debug (swap! -running inc))
|
||||||
(when-not (nil? obj)
|
(when (some? obj)
|
||||||
(set! (.-reaction obj) r))
|
(set! (.-reaction obj) r))
|
||||||
v)))))
|
v)))))
|
||||||
|
|
||||||
|
@ -260,7 +259,7 @@
|
||||||
(_set-state [this oldstate newstate]
|
(_set-state [this oldstate newstate]
|
||||||
(when-not (identical? oldstate newstate)
|
(when-not (identical? oldstate newstate)
|
||||||
(set! state newstate)
|
(set! state newstate)
|
||||||
(when-not (nil? watches)
|
(when (some? watches)
|
||||||
(notify-w this oldstate newstate))))
|
(notify-w this oldstate newstate))))
|
||||||
|
|
||||||
IDeref
|
IDeref
|
||||||
|
@ -348,9 +347,9 @@
|
||||||
(-notify-watches [this old new] (notify-w this old new))
|
(-notify-watches [this old new] (notify-w this old new))
|
||||||
(-add-watch [this key f] (add-w this key f))
|
(-add-watch [this key f] (add-w this key f))
|
||||||
(-remove-watch [this key]
|
(-remove-watch [this key]
|
||||||
(let [n (count watches)]
|
(let [was-empty (empty? watches)]
|
||||||
(remove-w this key)
|
(remove-w this key)
|
||||||
(when (and (pos? n)
|
(when (and (not was-empty)
|
||||||
(empty? watches)
|
(empty? watches)
|
||||||
(nil? auto-run))
|
(nil? auto-run))
|
||||||
(dispose! this))))
|
(dispose! this))))
|
||||||
|
@ -396,14 +395,14 @@
|
||||||
(-remove-watch w this))))
|
(-remove-watch w this))))
|
||||||
|
|
||||||
(_try-run [this other]
|
(_try-run [this other]
|
||||||
(if-not (nil? auto-run)
|
(if (some? auto-run)
|
||||||
(auto-run this)
|
(auto-run this)
|
||||||
(when (and dirty? (not (nil? watching)))
|
(when (and dirty? (some? watching))
|
||||||
(try
|
(try
|
||||||
(._run this)
|
(._run this)
|
||||||
(catch :default e
|
(catch :default e
|
||||||
;; 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.
|
||||||
(when-not silent (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))))))
|
||||||
|
|
||||||
|
@ -420,15 +419,15 @@
|
||||||
res))
|
res))
|
||||||
|
|
||||||
(_set-opts [this {:keys [auto-run on-set on-dispose no-cache]}]
|
(_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
|
(set! (.-auto-run this) (case auto-run
|
||||||
true run
|
true run
|
||||||
auto-run)))
|
auto-run)))
|
||||||
(when-not (nil? on-set)
|
(when (some? on-set)
|
||||||
(set! (.-on-set this) on-set))
|
(set! (.-on-set this) on-set))
|
||||||
(when-not (nil? on-dispose)
|
(when (some? on-dispose)
|
||||||
(set! (.-on-dispose this) on-dispose))
|
(set! (.-on-dispose this) on-dispose))
|
||||||
(when-not (nil? no-cache)
|
(when (some? no-cache)
|
||||||
(set! (.-nocache? this) no-cache)))
|
(set! (.-nocache? this) no-cache)))
|
||||||
|
|
||||||
IRunnable
|
IRunnable
|
||||||
|
@ -463,7 +462,7 @@
|
||||||
(set! dirty? true)
|
(set! dirty? true)
|
||||||
(doseq [w (set wg)]
|
(doseq [w (set wg)]
|
||||||
(-remove-watch w this))
|
(-remove-watch w this))
|
||||||
(when-not (nil? (.-on-dispose this))
|
(when (some? (.-on-dispose this))
|
||||||
(.on-dispose this s))))
|
(.on-dispose this s))))
|
||||||
|
|
||||||
IEquiv
|
IEquiv
|
||||||
|
@ -523,7 +522,7 @@
|
||||||
(let [oldval state]
|
(let [oldval state]
|
||||||
(set! changed true)
|
(set! changed true)
|
||||||
(set! state newval)
|
(set! state newval)
|
||||||
(when-not (nil? watches)
|
(when (some? watches)
|
||||||
(notify-w this oldval newval))
|
(notify-w this oldval newval))
|
||||||
(callback newval)
|
(callback newval)
|
||||||
newval))
|
newval))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns reagenttest.testratom
|
(ns reagenttest.testratom
|
||||||
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
||||||
[reagent.ratom :as rv :refer-macros [run! reaction]]
|
[reagent.ratom :as rv :refer-macros [run! reaction]]
|
||||||
[reagent.debug :refer-macros [dbg]]
|
[reagent.debug :as debug :refer-macros [dbg]]
|
||||||
[reagent.core :as r]))
|
[reagent.core :as r]))
|
||||||
|
|
||||||
(defn fixture [f]
|
(defn fixture [f]
|
||||||
|
@ -269,18 +269,18 @@
|
||||||
b (reaction (if @a (throw (js/Error. "fail"))))
|
b (reaction (if @a (throw (js/Error. "fail"))))
|
||||||
c (run! (try @b (catch :default e
|
c (run! (try @b (catch :default e
|
||||||
(swap! catch-count inc))))]
|
(swap! catch-count inc))))]
|
||||||
(set! rv/silent true)
|
(debug/track-warnings
|
||||||
(is (= @catch-count 0))
|
(fn []
|
||||||
(reset! a false)
|
(is (= @catch-count 0))
|
||||||
(r/flush)
|
(reset! a false)
|
||||||
(is (= @catch-count 0))
|
(r/flush)
|
||||||
(reset! a true)
|
(is (= @catch-count 0))
|
||||||
(r/flush)
|
(reset! a true)
|
||||||
(is (= @catch-count 1))
|
(r/flush)
|
||||||
(reset! a false)
|
(is (= @catch-count 1))
|
||||||
(r/flush)
|
(reset! a false)
|
||||||
(is (= @catch-count 1))
|
(r/flush)
|
||||||
(set! rv/silent false)
|
(is (= @catch-count 1))))
|
||||||
(dispose c)
|
(dispose c)
|
||||||
(is (= runs (running)))))
|
(is (= runs (running)))))
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns reagenttest.testratomasync
|
(ns reagenttest.testratomasync
|
||||||
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
||||||
[reagent.ratom :as rv :refer-macros [run! reaction]]
|
[reagent.ratom :as rv :refer-macros [run! reaction]]
|
||||||
[reagent.debug :refer-macros [dbg]]
|
[reagent.debug :as debug :refer-macros [dbg]]
|
||||||
[reagent.core :as r]))
|
[reagent.core :as r]))
|
||||||
|
|
||||||
(defn fixture [f]
|
(defn fixture [f]
|
||||||
|
@ -282,15 +282,15 @@
|
||||||
b (reaction (if @a (throw (js/Error. "reaction fail"))))
|
b (reaction (if @a (throw (js/Error. "reaction fail"))))
|
||||||
c (ar (fn [] (try @b (catch js/Object e
|
c (ar (fn [] (try @b (catch js/Object e
|
||||||
(swap! catch-count inc)))))]
|
(swap! catch-count inc)))))]
|
||||||
(set! rv/silent true)
|
(debug/track-warnings
|
||||||
(is (= @catch-count 0))
|
(fn []
|
||||||
(reset! a false)
|
(is (= @catch-count 0))
|
||||||
@c
|
(reset! a false)
|
||||||
(is (= @catch-count 0))
|
@c
|
||||||
(reset! a true)
|
(is (= @catch-count 0))
|
||||||
(is (= @catch-count 0))
|
(reset! a true)
|
||||||
(sync)
|
(is (= @catch-count 0))
|
||||||
(is (= @catch-count 1))
|
(sync)
|
||||||
(set! rv/silent false)
|
(is (= @catch-count 1))
|
||||||
(dispose c)
|
(dispose c)))
|
||||||
(is (= runs (running)))))
|
(is (= runs (running)))))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns reagenttest.testtrack
|
(ns reagenttest.testtrack
|
||||||
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
(:require [cljs.test :as t :refer-macros [is deftest testing]]
|
||||||
[reagent.ratom :as rv :refer [track] :refer-macros [run! reaction]]
|
[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]))
|
[reagent.core :as r]))
|
||||||
|
|
||||||
(defn fixture [f]
|
(defn fixture [f]
|
||||||
|
@ -213,19 +213,17 @@
|
||||||
b (track #(if @a (throw (js/Error. "fail"))))
|
b (track #(if @a (throw (js/Error. "fail"))))
|
||||||
c (run! (try @b (catch :default e
|
c (run! (try @b (catch :default e
|
||||||
(swap! catch-count inc))))]
|
(swap! catch-count inc))))]
|
||||||
(set! rv/silent true)
|
(debug/track-warnings
|
||||||
|
(fn []
|
||||||
(is (= @catch-count 0))
|
(is (= @catch-count 0))
|
||||||
(reset! a false)
|
(reset! a false)
|
||||||
(sync)
|
(sync)
|
||||||
(is (= @catch-count 0))
|
(is (= @catch-count 0))
|
||||||
(reset! a true)
|
(reset! a true)
|
||||||
(sync)
|
(sync)
|
||||||
(is (= @catch-count 1))
|
(is (= @catch-count 1))
|
||||||
(reset! a false)
|
(reset! a false)
|
||||||
(sync)
|
(sync)
|
||||||
(is (= @catch-count 1))
|
(is (= @catch-count 1))))
|
||||||
|
|
||||||
(set! rv/silent false)
|
|
||||||
(dispose c)
|
(dispose c)
|
||||||
(is (= runs (running)))))
|
(is (= runs (running)))))
|
||||||
|
|
Loading…
Reference in New Issue