Move wrap to ratom

This commit is contained in:
Dan Holmsand 2015-02-01 19:22:46 +01:00
parent 742a8d3575
commit 5798a3f056
3 changed files with 60 additions and 49 deletions

View File

@ -197,7 +197,7 @@ re-rendered."
Probably useful only for passing to child components."
[value reset-fn & args]
(assert (ifn? reset-fn))
(util/make-wrapper value reset-fn args))
(ratom/make-wrapper value reset-fn args))
;; RCursor

View File

@ -137,51 +137,3 @@
(doseq [v (vals @roots)]
(apply re-render-component v))
"Updated")
;;; Wrapper
(deftype Wrapper [^:mutable state callback ^:mutable changed]
IAtom
IDeref
(-deref [this] state)
IReset
(-reset! [this newval]
(set! changed true)
(set! state newval)
(callback newval)
state)
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)))
IEquiv
(-equiv [_ other]
(and (instance? Wrapper other)
;; If either of the wrappers have changed, equality
;; cannot be relied on.
(not changed)
(not (.-changed other))
(= state (.-state other))
(= callback (.-callback other))))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-write writer "#<wrap: ")
(pr-writer state writer opts)
(-write writer ">")))
(defn make-wrapper [value callback-fn args]
(Wrapper. value
(partial-ifn. callback-fn args nil)
false))

View File

@ -29,6 +29,9 @@
(conj (if (nil? captured) #{} captured)
derefable))))))
;;; Atom
(deftype RAtom [^:mutable state meta validator ^:mutable watches]
IAtom
@ -89,6 +92,9 @@
([x & {:keys [meta validator]}] (RAtom. x meta validator nil)))
;;; cursor
(declare make-reaction)
(deftype RCursor [ratom path ^:mutable reaction]
@ -168,6 +174,10 @@
(str "src must be an atom or a function, not " src))
(RCursor. src path nil))))
;;;; reaction
(defprotocol IDisposable
(dispose! [this]))
@ -300,3 +310,52 @@
(when debug (swap! -running inc))
(-update-watching reaction derefed))
reaction))
;;; wrap
(deftype Wrapper [^:mutable state callback ^:mutable changed]
IAtom
IDeref
(-deref [this] state)
IReset
(-reset! [this newval]
(set! changed true)
(set! state newval)
(callback newval)
state)
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)))
IEquiv
(-equiv [_ other]
(and (instance? Wrapper other)
;; If either of the wrappers have changed, equality
;; cannot be relied on.
(not changed)
(not (.-changed other))
(= state (.-state other))
(= callback (.-callback other))))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-write writer "#<wrap: ")
(pr-writer state writer opts)
(-write writer ">")))
(defn make-wrapper [value callback-fn args]
(Wrapper. value
(util/partial-ifn. callback-fn args nil)
false))