Be compatible with ClojureScript 0.0-2173: implement IAtom, ISwap,

IReset
This commit is contained in:
Dan Holmsand 2014-02-22 07:59:47 +01:00
parent 7951b43aeb
commit 908452c126
2 changed files with 47 additions and 6 deletions

View File

@ -4,7 +4,7 @@
:license {:name "MIT"} :license {:name "MIT"}
:description "A simple ClojureScript interface to React" :description "A simple ClojureScript interface to React"
:dependencies [[org.clojure/clojure "1.5.1"] :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"] :plugins [[lein-cljsbuild "1.0.1"]
[com.cemerick/clojurescript.test "0.2.1"]] [com.cemerick/clojurescript.test "0.2.1"]]
:profiles {:prod {:cljsbuild :profiles {:prod {:cljsbuild

View File

@ -28,7 +28,9 @@
(conj (if (nil? captured) #{} captured) (conj (if (nil? captured) #{} captured)
derefable)))))) derefable))))))
(deftype RAtom [state meta validator watches] (deftype RAtom [^:mutable state meta validator ^:mutable watches]
IAtom
IEquiv IEquiv
(-equiv [o other] (identical? o other)) (-equiv [o other] (identical? o other))
@ -37,6 +39,26 @@
(notify-deref-watcher! this) (notify-deref-watcher! this)
state) 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 IMeta
(-meta [_] meta) (-meta [_] meta)
@ -53,9 +75,9 @@
nil) nil)
nil watches)) nil watches))
(-add-watch [this key f] (-add-watch [this key f]
(set! (.-watches this) (assoc watches key f))) (set! watches (assoc watches key f)))
(-remove-watch [this key] (-remove-watch [this key]
(set! (.-watches this) (dissoc watches key))) (set! watches (dissoc watches key)))
IHash IHash
(-hash [this] (goog/getUid this))) (-hash [this] (goog/getUid this)))
@ -85,6 +107,8 @@
(deftype Reaction [f ^:mutable state ^:mutable dirty? ^:mutable active? (deftype Reaction [f ^:mutable state ^:mutable dirty? ^:mutable active?
^:mutable watching ^:mutable watches ^:mutable watching ^:mutable watches
auto-run on-set on-dispose] auto-run on-set on-dispose]
IAtom
IWatchable IWatchable
(-notify-watches [this oldval newval] (-notify-watches [this oldval newval]
(when on-set (when on-set
@ -99,6 +123,23 @@
(when (empty? watches) (when (empty? watches)
(dispose! this))) (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 IComputedImpl
(-handle-change [this sender oldval newval] (-handle-change [this sender oldval newval]
(when (and active? (not dirty?) (not (identical? oldval newval))) (when (and active? (not dirty?) (not (identical? oldval newval)))