Make sure setter can override values in Reaction

Also assert that cursor src isn't a vector.

And a little cleanup.
This commit is contained in:
Dan Holmsand 2015-02-01 10:06:34 +01:00
parent 744165737f
commit 58fd72cd86
1 changed files with 18 additions and 19 deletions

View File

@ -137,7 +137,7 @@
IPrintWithWriter IPrintWithWriter
(-pr-writer [a writer opts] (-pr-writer [a writer opts]
(-write writer "#<Cursor: ") (-write writer (str "#<Cursor: " path " "))
(pr-writer (._peek a) writer opts) (pr-writer (._peek a) writer opts)
(-write writer ">")) (-write writer ">"))
@ -163,8 +163,9 @@
(RCursor. path src nil)) (RCursor. path src nil))
(do (do
(assert (or (satisfies? IDeref src) (assert (or (satisfies? IDeref src)
(ifn? src)) (and (ifn? src)
"src must be an atom or a function") (not (vector? src))))
(str "src must be an atom or a function, not " src))
(RCursor. src path nil)))) (RCursor. src path nil))))
(defprotocol IDisposable (defprotocol IDisposable
@ -177,12 +178,6 @@
(-update-watching [this derefed]) (-update-watching [this derefed])
(-handle-change [k sender oldval newval])) (-handle-change [k sender oldval newval]))
(defn- call-watches [obs watches oldval newval]
(reduce-kv (fn [_ key f]
(f key obs oldval newval)
nil)
nil watches))
(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]
@ -190,9 +185,10 @@
IWatchable IWatchable
(-notify-watches [this oldval newval] (-notify-watches [this oldval newval]
(when on-set (reduce-kv (fn [_ key f]
(on-set oldval newval)) (f key this oldval newval)
(call-watches this watches oldval newval)) nil)
nil watches))
(-add-watch [this k wf] (-add-watch [this k wf]
(set! watches (assoc watches k wf))) (set! watches (assoc watches k wf)))
@ -203,11 +199,14 @@
(dispose! this))) (dispose! this)))
IReset IReset
(-reset! [a new-value] (-reset! [a newval]
(let [old-value state] (let [oldval state]
(set! state new-value) (set! state newval)
(-notify-watches a old-value new-value) (when on-set
new-value)) (set! dirty? true)
(on-set oldval newval))
(-notify-watches a oldval newval)
newval))
ISwap ISwap
(-swap! [a f] (-swap! [a f]
@ -246,7 +245,7 @@
(set! active? true)) (set! active? true))
(set! dirty? false) (set! dirty? false)
(set! state res) (set! state res)
(call-watches this watches oldstate state) (-notify-watches this oldstate state)
res)) res))
IDeref IDeref
@ -257,7 +256,7 @@
(let [oldstate state] (let [oldstate state]
(set! state (f)) (set! state (f))
(when-not (identical? oldstate state) (when-not (identical? oldstate state)
(call-watches this watches oldstate state)))) (-notify-watches this oldstate state))))
state) state)
(do (do
(notify-deref-watcher! this) (notify-deref-watcher! this)