mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-22 06:48:08 +00:00
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:
parent
f1337bcb6b
commit
9839b258cb
1
.idea/codeStyleSettings.xml
generated
1
.idea/codeStyleSettings.xml
generated
@ -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>
|
||||||
|
@ -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
|
||||||
|
@ -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?))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user