mirror of https://github.com/status-im/reagent.git
Swap order of arguments to cursor
So now it should be called as (r/cursor atom path). The old order still works though.
This commit is contained in:
parent
976d8ba4f6
commit
86bf871329
|
@ -207,16 +207,14 @@ re-rendered."
|
|||
|
||||
Behaves like a Reagent atom but focuses updates and derefs to
|
||||
the specified path within the wrapped Reagent atom. e.g.,
|
||||
(let [c (cursor [:nested :content] ra)]
|
||||
(let [c (cursor ra [:nested :content])]
|
||||
... @c ;; equivalent to (get-in @ra [:nested :content])
|
||||
... (reset! c 42) ;; equivalent to (swap! ra assoc-in [:nested :content] 42)
|
||||
... (swap! c inc) ;; equivalence to (swap! ra update-in [:nested :content] inc)
|
||||
)
|
||||
"
|
||||
([path ra]
|
||||
(assert (or (satisfies? IDeref ra)
|
||||
(ifn? ra)))
|
||||
(ratom/cursor path ra)))
|
||||
([src path]
|
||||
(ratom/cursor src path)))
|
||||
|
||||
;; Utilities
|
||||
|
||||
|
|
|
@ -91,15 +91,14 @@
|
|||
|
||||
(declare make-reaction)
|
||||
|
||||
(deftype RCursor [path ratom setf ^:mutable reaction]
|
||||
(deftype RCursor [ratom path ^:mutable reaction]
|
||||
IAtom
|
||||
|
||||
IEquiv
|
||||
(-equiv [o other]
|
||||
(and (instance? RCursor other)
|
||||
(= path (.-path other))
|
||||
(= ratom (.-ratom other))
|
||||
(= setf (.-setf other))))
|
||||
(= ratom (.-ratom other))))
|
||||
|
||||
Object
|
||||
(_reaction [this]
|
||||
|
@ -110,8 +109,10 @@
|
|||
:on-set (if (= path [])
|
||||
#(reset! ratom %2)
|
||||
#(swap! ratom assoc-in path %2)))
|
||||
(make-reaction #(ratom path)
|
||||
:on-set #(ratom path %2))))
|
||||
(do
|
||||
(assert (ifn? ratom))
|
||||
(make-reaction #(ratom path)
|
||||
:on-set #(ratom path %2)))))
|
||||
reaction))
|
||||
|
||||
(_peek [this]
|
||||
|
@ -151,14 +152,13 @@
|
|||
(-remove-watch (._reaction this) key))
|
||||
|
||||
IHash
|
||||
(-hash [this] (hash [ratom path setf])))
|
||||
(-hash [this] (hash [ratom path])))
|
||||
|
||||
(defn cursor
|
||||
([path ra]
|
||||
(RCursor. path ra nil nil))
|
||||
([path ra setf args]
|
||||
(RCursor. path ra
|
||||
(util/partial-ifn. setf args nil) nil)))
|
||||
[src path]
|
||||
(if (satisfies? IDeref path)
|
||||
(RCursor. path src nil)
|
||||
(RCursor. src path nil)))
|
||||
|
||||
(defprotocol IDisposable
|
||||
(dispose! [this]))
|
||||
|
|
|
@ -268,8 +268,8 @@
|
|||
foobar (fn
|
||||
([path] (get-in @a path))
|
||||
([path v] (swap! a assoc :foobar v)))
|
||||
c2 (r/cursor [:foo] foo)
|
||||
c3 (r/cursor [:foo] foobar)]
|
||||
c2 (r/cursor foo [:foo])
|
||||
c3 (r/cursor foobar [:foo])]
|
||||
|
||||
(is (= @c "bar"))
|
||||
(is (= @c2 "bar"))
|
||||
|
@ -277,12 +277,12 @@
|
|||
(is (= c (r/cursor [:foo] a)))
|
||||
(is (not= c (r/cursor [:foo] a1)))
|
||||
(is (not= c (r/cursor [:foobar] a)))
|
||||
(is (= c2 (r/cursor [:foo] foo)))
|
||||
(is (= c3 (r/cursor [:foo] foobar)))
|
||||
(is (= c2 (r/cursor foo [:foo])))
|
||||
(is (= c3 (r/cursor foobar [:foo])))
|
||||
(is (= c2 c2))
|
||||
(is (not= c2 (r/cursor [:foo] foobar)))
|
||||
(is (not= c3 (r/cursor [:foo] foo)))
|
||||
(is (not= c2 (r/cursor [:foobar] foo)))
|
||||
(is (not= c2 (r/cursor foobar [:foo])))
|
||||
(is (not= c3 (r/cursor foo [:foo])))
|
||||
(is (not= c2 (r/cursor foo [:foobar])))
|
||||
|
||||
(reset! c2 "foobar")
|
||||
(is (= @c2 "foobar"))
|
||||
|
@ -294,7 +294,7 @@
|
|||
(is (= @c "bar"))
|
||||
(is (= @a {:foo "bar"}))
|
||||
(is (= c (r/cursor [:foo] a)))
|
||||
(is (= c2 (r/cursor [:foo] foo)))
|
||||
(is (= c2 (r/cursor foo [:foo])))
|
||||
|
||||
(reset! c3 "foo")
|
||||
(is (= @a {:foo "bar" :foobar "foo"}))))
|
||||
|
|
Loading…
Reference in New Issue