diff --git a/project.clj b/project.clj index 02f21fd..358714d 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,7 @@ :license {:name "MIT"} :description "A simple ClojureScript interface to React" :dependencies [[org.clojure/clojure "1.5.1"] - [org.clojure/clojurescript "0.0-2156"]] + [org.clojure/clojurescript "0.0-2173"]] :plugins [[lein-cljsbuild "1.0.1"] [com.cemerick/clojurescript.test "0.2.1"]] :profiles {:prod {:cljsbuild diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 6b489d3..da9ba11 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -28,7 +28,9 @@ (conj (if (nil? captured) #{} captured) derefable)))))) -(deftype RAtom [state meta validator watches] +(deftype RAtom [^:mutable state meta validator ^:mutable watches] + IAtom + IEquiv (-equiv [o other] (identical? o other)) @@ -37,6 +39,26 @@ (notify-deref-watcher! this) state) + IReset + (-reset! [a new-value] + (when-not (nil? validator) + (assert (validator new-value) "Validator rejected reference state")) + (let [old-value state] + (set! state new-value) + (when-not (nil? watches) + (-notify-watches a old-value new-value)) + new-value)) + + ISwap + (-swap! [a f] + (-reset! a (f state))) + (-swap! [a f x] + (-reset! a (f state x))) + (-swap! [a f x y] + (-reset! a (f state x y))) + (-swap! [a f x y more] + (-reset! a (apply f state x y more))) + IMeta (-meta [_] meta) @@ -53,9 +75,9 @@ nil) nil watches)) (-add-watch [this key f] - (set! (.-watches this) (assoc watches key f))) + (set! watches (assoc watches key f))) (-remove-watch [this key] - (set! (.-watches this) (dissoc watches key))) + (set! watches (dissoc watches key))) IHash (-hash [this] (goog/getUid this))) @@ -83,8 +105,10 @@ nil watches)) (deftype Reaction [f ^:mutable state ^:mutable dirty? ^:mutable active? - ^:mutable watching ^:mutable watches - auto-run on-set on-dispose] + ^:mutable watching ^:mutable watches + auto-run on-set on-dispose] + IAtom + IWatchable (-notify-watches [this oldval newval] (when on-set @@ -99,6 +123,23 @@ (when (empty? watches) (dispose! this))) + IReset + (-reset! [a new-value] + (let [old-value state] + (set! state new-value) + (-notify-watches a old-value new-value) + new-value)) + + ISwap + (-swap! [a f] + (-reset! a (f state))) + (-swap! [a f x] + (-reset! a (f state x))) + (-swap! [a f x y] + (-reset! a (f state x y))) + (-swap! [a f x y more] + (-reset! a (apply f state x y more))) + IComputedImpl (-handle-change [this sender oldval newval] (when (and active? (not dirty?) (not (identical? oldval newval)))