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">
|
||||
<value>
|
||||
<ClojureCodeStyleSettings>{
|
||||
:cljs.core/with-redefs 1
|
||||
:cursive.formatting/align-binding-forms true
|
||||
}</ClojureCodeStyleSettings>
|
||||
<XML>
|
||||
|
@ -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
|
||||
|
@ -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?))))
|
||||
|
Loading…
x
Reference in New Issue
Block a user