mirror of https://github.com/status-im/reagent.git
Make cursor more general and flexible
Instead of passing an atom, you can now pass a function. That function is passed one argument (the path provided to cursor) when deref-ing, and two arguments (path and new value) when resetting. Remove the old setter, and the currying version.
This commit is contained in:
parent
a28dc812ae
commit
976d8ba4f6
|
@ -212,19 +212,11 @@ the specified path within the wrapped Reagent atom. e.g.,
|
|||
... (reset! c 42) ;; equivalent to (swap! ra assoc-in [:nested :content] 42)
|
||||
... (swap! c inc) ;; equivalence to (swap! ra update-in [:nested :content] inc)
|
||||
)
|
||||
The third argument may be a function, that is called with
|
||||
optional extra arguments provided to cursor, and the new value of the
|
||||
resulting 'atom'. If such a function is given, it should update the
|
||||
given Reagent atom.
|
||||
"
|
||||
([path] (fn [ra] (cursor path ra)))
|
||||
([path ra]
|
||||
(assert (satisfies? IDeref ra))
|
||||
(ratom/cursor path ra))
|
||||
([path ra reset-fn & args]
|
||||
(assert (satisfies? IDeref ra))
|
||||
(assert (ifn? reset-fn))
|
||||
(ratom/cursor path ra reset-fn args)))
|
||||
(assert (or (satisfies? IDeref ra)
|
||||
(ifn? ra)))
|
||||
(ratom/cursor path ra)))
|
||||
|
||||
;; Utilities
|
||||
|
||||
|
|
|
@ -105,13 +105,13 @@
|
|||
(_reaction [this]
|
||||
(if (nil? reaction)
|
||||
(set! reaction
|
||||
(make-reaction
|
||||
#(get-in @ratom path)
|
||||
:on-set (if setf
|
||||
#(setf %2)
|
||||
(if (= path [])
|
||||
(if (satisfies? IDeref ratom)
|
||||
(make-reaction #(get-in @ratom path)
|
||||
:on-set (if (= path [])
|
||||
#(reset! ratom %2)
|
||||
#(swap! ratom assoc-in path %2)))))
|
||||
#(swap! ratom assoc-in path %2)))
|
||||
(make-reaction #(ratom path)
|
||||
:on-set #(ratom path %2))))
|
||||
reaction))
|
||||
|
||||
(_peek [this]
|
||||
|
|
|
@ -262,18 +262,27 @@
|
|||
(let [a (atom {:foo "bar"})
|
||||
a1 (atom {:foo "bar"})
|
||||
c (r/cursor [:foo] a)
|
||||
c2 (r/cursor [:foo] a swap! a assoc :foo)
|
||||
c3 (r/cursor [:foo] a swap! a assoc :foobar)]
|
||||
foo (fn
|
||||
([path] (get-in @a path))
|
||||
([path v] (swap! a assoc-in path v)))
|
||||
foobar (fn
|
||||
([path] (get-in @a path))
|
||||
([path v] (swap! a assoc :foobar v)))
|
||||
c2 (r/cursor [:foo] foo)
|
||||
c3 (r/cursor [:foo] foobar)]
|
||||
|
||||
(is (= @c "bar"))
|
||||
(is (= @c2 "bar"))
|
||||
(is (= @c3 "bar"))
|
||||
(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] a swap! a assoc :foo)))
|
||||
(is (not= c2 (r/cursor [:foo] a swap! a assoc :foobar)))
|
||||
(is (not= c2 (r/cursor [:foo] a1 swap! a assoc :foo)))
|
||||
(is (not= c2 (r/cursor [:foo] a swap! a1 assoc :foo)))
|
||||
(is (= c2 (r/cursor [:foo] foo)))
|
||||
(is (= c3 (r/cursor [:foo] foobar)))
|
||||
(is (= c2 c2))
|
||||
(is (not= c2 (r/cursor [:foo] foobar)))
|
||||
(is (not= c3 (r/cursor [:foo] foo)))
|
||||
(is (not= c2 (r/cursor [:foobar] foo)))
|
||||
|
||||
(reset! c2 "foobar")
|
||||
(is (= @c2 "foobar"))
|
||||
|
@ -285,7 +294,7 @@
|
|||
(is (= @c "bar"))
|
||||
(is (= @a {:foo "bar"}))
|
||||
(is (= c (r/cursor [:foo] a)))
|
||||
(is (= c2 (r/cursor [:foo] a swap! a assoc :foo)))
|
||||
(is (= c2 (r/cursor [:foo] foo)))
|
||||
|
||||
(reset! c3 "foo")
|
||||
(is (= @a {:foo "bar" :foobar "foo"}))))
|
||||
|
|
Loading…
Reference in New Issue