mirror of
https://github.com/status-im/reagent.git
synced 2025-01-14 05:45:06 +00:00
Move wrap to ratom
This commit is contained in:
parent
742a8d3575
commit
5798a3f056
@ -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
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user