From 976d8ba4f632d33d9bc1240a5dcfa462b1773081 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Sat, 31 Jan 2015 13:32:48 +0100 Subject: [PATCH] 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. --- src/reagent/core.cljs | 14 +++----------- src/reagent/ratom.cljs | 14 +++++++------- test/testcursor.cljs | 23 ++++++++++++++++------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index b3300cc..2fac139 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -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 diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 63940eb..677be1e 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -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 []) - #(reset! ratom %2) - #(swap! ratom assoc-in path %2))))) + (if (satisfies? IDeref ratom) + (make-reaction #(get-in @ratom path) + :on-set (if (= path []) + #(reset! ratom %2) + #(swap! ratom assoc-in path %2))) + (make-reaction #(ratom path) + :on-set #(ratom path %2)))) reaction)) (_peek [this] diff --git a/test/testcursor.cljs b/test/testcursor.cljs index 7be46ee..3c31b96 100644 --- a/test/testcursor.cljs +++ b/test/testcursor.cljs @@ -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"}))))