Subscriptions sugar - changed to provide a singleton

This commit is contained in:
Stuart Mitchell 2016-06-22 15:41:22 +12:00
parent 5c7d4ea3d1
commit f4fb65037e
2 changed files with 25 additions and 7 deletions

View File

@ -163,11 +163,14 @@
(fn [] (f (multi-deref subscriptions) q-vec d-vec)))))) (fn [] (f (multi-deref subscriptions) q-vec d-vec))))))
arrow-args ;; the user uses the :<- sugar arrow-args ;; the user uses the :<- sugar
(register (register
sub-name sub-name
(fn [db q-vec d-vec] (fn [db q-vec d-vec]
(let [subscriptions (map subscribe arrow-subs)] ;; this let needs to be outside the fn (let [subscriptions (map subscribe arrow-subs)
(ratom/make-reaction subscriptions (if (< 1 (count subscriptions))
(fn [] (f (multi-deref subscriptions) q-vec d-vec)))))) subscriptions
(first subscriptions))] ;; automatically provide a singlton
(ratom/make-reaction
(fn [] (f (multi-deref subscriptions) q-vec d-vec))))))
:else :else
(register ;; the simple case with no subs (register ;; the simple case with no subs
sub-name sub-name

View File

@ -220,6 +220,22 @@
(reset! db/app-db {:a 1 :b 2}) (reset! db/app-db {:a 1 :b 2})
(is (= {:a [1 :c] :b [2 :c]} @test-sub)))) (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-<- (deftest test-sub-macros-chained-parameters-<-
"test the syntactial sugar" "test the syntactial sugar"
@ -241,5 +257,4 @@
(let [test-sub (subs/subscribe [:a-b-sub :c])] (let [test-sub (subs/subscribe [:a-b-sub :c])]
(reset! db/app-db {:a 1 :b 2}) (reset! db/app-db {:a 1 :b 2})
(is (= {:a 1 :b 2} @test-sub) )) (is (= {:a 1 :b 2} @test-sub) )))
)