Make Reaction a little smaller

This commit is contained in:
Dan Holmsand 2015-10-03 14:46:46 +02:00
parent a31c410e08
commit b399f30daf
1 changed files with 13 additions and 14 deletions

View File

@ -359,9 +359,8 @@
(._handle-change this sender old new)) (._handle-change this sender old new))
(deftype Reaction [f ^:mutable state ^:mutable ^boolean dirty? (deftype Reaction [f ^:mutable state ^:mutable ^boolean dirty? ^boolean nocache?
^:mutable watching ^:mutable watches ^:mutable watching ^:mutable watches ^:mutable auto-run]
^:mutable auto-run on-set on-dispose ^boolean nocache?]
IAtom IAtom
IReactiveAtom IReactiveAtom
@ -376,10 +375,10 @@
IReset IReset
(-reset! [a newval] (-reset! [a newval]
(assert (ifn? on-set) "Reaction is read only.") (assert (fn? (.-on-set a)) "Reaction is read only.")
(let [oldval state] (let [oldval state]
(set! state newval) (set! state newval)
(on-set oldval newval) (.on-set a oldval newval)
(notify-w a oldval newval) (notify-w a oldval newval)
newval)) newval))
@ -408,15 +407,16 @@
nil) nil)
(_update-watching [this derefed] (_update-watching [this derefed]
(let [wg watching] (let [der (if (zero? (arr-len derefed)) nil derefed)
(set! watching derefed) wg watching]
(doseq [w derefed] (set! watching der)
(doseq [w der]
(when (or (nil? wg) (when (or (nil? wg)
(== -1 (.indexOf wg w))) (== -1 (.indexOf wg w)))
(-add-watch w this handle-reaction-change))) (-add-watch w this handle-reaction-change)))
(doseq [w wg] (doseq [w wg]
(when (or (nil? derefed) (when (or (nil? der)
(== -1 (.indexOf derefed w))) (== -1 (.indexOf der w)))
(-remove-watch w this)))) (-remove-watch w this))))
nil) nil)
@ -490,8 +490,8 @@
(set! dirty? true) (set! dirty? true)
(doseq [w wg] (doseq [w wg]
(remove-watch w this)) (remove-watch w this))
(when-not (nil? on-dispose) (when-not (nil? (.-on-dispose this))
(on-dispose s))) (.on-dispose this s)))
nil) nil)
IEquiv IEquiv
@ -505,8 +505,7 @@
(defn make-reaction [f & {:keys [auto-run on-set on-dispose]}] (defn make-reaction [f & {:keys [auto-run on-set on-dispose]}]
(let [reaction (Reaction. f nil true nil nil (let [reaction (Reaction. f nil true false nil nil nil)]
nil nil nil false)]
(._set-opts reaction {:auto-run auto-run (._set-opts reaction {:auto-run auto-run
:on-set on-set :on-set on-set
:on-dispose on-dispose}) :on-dispose on-dispose})