Don't create subscriptions in reg-sub

This commit fixes a bug where reg-sub created subscriptions and closed
over them when using the `:<- [:sub]` sugar. Now dependent subscriptions
aren't created until they are needed, and they will be cleaned up
correctly.
This commit is contained in:
Daniel Compton 2016-10-21 09:56:20 +13:00
parent f1337bcb6b
commit 9839b258cb
No known key found for this signature in database
GPG Key ID: A6E4DC5283B5DCEC
3 changed files with 39 additions and 8 deletions

View File

@ -4,6 +4,7 @@
<option name="PER_PROJECT_SETTINGS">
<value>
<ClojureCodeStyleSettings>{
:cljs.core/with-redefs 1
:cursive.formatting/align-binding-forms true
}</ClojureCodeStyleSettings>
<XML>

View File

@ -143,20 +143,18 @@
f)
;; one sugar pair
2 (let [ret-val (subscribe (second input-args))]
(fn inp-fn
([_] ret-val)
([_ _] ret-val)))
2 (fn inp-fn
([_] (subscribe (second input-args)))
([_ _] (subscribe (second input-args))))
;; multiple sugar pairs
(let [pairs (partition 2 input-args)
vecs (map last pairs)
ret-val (map subscribe vecs)]
vecs (map last pairs)]
(when-not (every? vector? vecs)
(console :error err-header "expected pairs of :<- and vectors, got:" pairs))
(fn inp-fn
([_] ret-val)
([_ _] ret-val))))]
([_] (map subscribe vecs))
([_ _] (map subscribe vecs)))))]
(register-handler
kind
query-id

View File

@ -258,3 +258,35 @@
(let [test-sub (subs/subscribe [:a-b-sub :c])]
(reset! db/app-db {:a 1 :b 2})
(is (= {:a 1 :b 2} @test-sub) )))
(deftest test-registering-subs-doesnt-create-subscription
(subs/clear-all-handlers!)
(let [sub-called? (atom false)]
(with-redefs [subs/subscribe (fn [& args] (reset! sub-called? true))]
(subs/reg-sub
:a-sub
(fn [db [_]] (:a db)))
(subs/reg-sub
:b-sub
(fn [db [_]] (:b db)))
(subs/reg-sub
:fn-sub
(fn [[_ c] _]
[(subs/subscribe [:a-sub c])
(subs/subscribe [:b-sub c])])
(fn [db [_]] (:b db)))
(subs/reg-sub
:a-sugar-sub
:<- [:a-sub]
(fn [[a] [_ c]] {:a a}))
(subs/reg-sub
:a-b-sub
:<- [:a-sub]
:<- [:b-sub]
(fn [[a b] [_ c]] {:a a :b b})))
(is (false? @sub-called?))))