From f4fb65037ec29425e1bfacc0bdc5cff702e7a1ed Mon Sep 17 00:00:00 2001 From: Stuart Mitchell Date: Wed, 22 Jun 2016 15:41:22 +1200 Subject: [PATCH] Subscriptions sugar - changed to provide a singleton --- src/re_frame/subs.cljs | 13 ++++++++----- test/re-frame/test/subs.cljs | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/re_frame/subs.cljs b/src/re_frame/subs.cljs index 456c274..baac4fe 100644 --- a/src/re_frame/subs.cljs +++ b/src/re_frame/subs.cljs @@ -163,11 +163,14 @@ (fn [] (f (multi-deref subscriptions) q-vec d-vec)))))) arrow-args ;; the user uses the :<- sugar (register - sub-name - (fn [db q-vec d-vec] - (let [subscriptions (map subscribe arrow-subs)] ;; this let needs to be outside the fn - (ratom/make-reaction - (fn [] (f (multi-deref subscriptions) q-vec d-vec)))))) + sub-name + (fn [db q-vec d-vec] + (let [subscriptions (map subscribe arrow-subs) + subscriptions (if (< 1 (count subscriptions)) + subscriptions + (first subscriptions))] ;; automatically provide a singlton + (ratom/make-reaction + (fn [] (f (multi-deref subscriptions) q-vec d-vec)))))) :else (register ;; the simple case with no subs sub-name diff --git a/test/re-frame/test/subs.cljs b/test/re-frame/test/subs.cljs index 56ba891..0066ed6 100644 --- a/test/re-frame/test/subs.cljs +++ b/test/re-frame/test/subs.cljs @@ -220,6 +220,22 @@ (reset! db/app-db {:a 1 :b 2}) (is (= {:a [1 :c] :b [2 :c]} @test-sub)))) +(deftest test-sub-macros-<- + "test the syntactial sugar" + (subs/clear-handlers!) + + (subs/register-pure + :a-sub + (fn [db [_]] (:a db))) + + (subs/register-pure + :a-b-sub + :<- [:a-sub] + (fn [a [_]] {:a a})) + + (let [test-sub (subs/subscribe [:a-b-sub])] + (reset! db/app-db {:a 1 :b 2}) + (is (= {:a 1} @test-sub) ))) (deftest test-sub-macros-chained-parameters-<- "test the syntactial sugar" @@ -241,5 +257,4 @@ (let [test-sub (subs/subscribe [:a-b-sub :c])] (reset! db/app-db {:a 1 :b 2}) - (is (= {:a 1 :b 2} @test-sub) )) - ) \ No newline at end of file + (is (= {:a 1 :b 2} @test-sub) ))) \ No newline at end of file