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"> <option name="PER_PROJECT_SETTINGS">
<value> <value>
<ClojureCodeStyleSettings>{ <ClojureCodeStyleSettings>{
:cljs.core/with-redefs 1
:cursive.formatting/align-binding-forms true :cursive.formatting/align-binding-forms true
}</ClojureCodeStyleSettings> }</ClojureCodeStyleSettings>
<XML> <XML>

View File

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

View File

@ -258,3 +258,35 @@
(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) )))
(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?))))