Make subs only remove self from subscription cache when disposing

Adds a check for subscriptions to make sure that when they are dissocing
their cache key from the query cache that they are only removing
themselves. In certain cases when reloading/re-rendering the app,
subscriptions would be created and destroyed several times because each
version of subscription destroyed the following one.
This commit is contained in:
Daniel Compton 2017-11-20 17:15:06 +13:00
parent 1fdd8eb285
commit 68c82cd69d
2 changed files with 10 additions and 6 deletions

1
checkouts/re-frame-trace Symbolic link
View File

@ -0,0 +1 @@
../../re-frame-trace/

View File

@ -44,12 +44,15 @@
[query-v dynv r]
(let [cache-key [query-v dynv]]
;; when this reaction is no longer being used, remove it from the cache
(add-on-dispose! r #(do (swap! query->reaction dissoc cache-key)
(trace/with-trace {:operation (first-in-vector query-v)
:op-type :sub/dispose
:tags {:query-v query-v
:reaction (reagent-id r)}}
nil)))
(add-on-dispose! r #(trace/with-trace {:operation (first-in-vector query-v)
:op-type :sub/dispose
:tags {:query-v query-v
:reaction (reagent-id r)}}
(swap! query->reaction
(fn [query-cache]
(if (and (contains? query-cache cache-key) (identical? r (get query-cache cache-key)))
(dissoc query-cache cache-key)
query-cache)))))
;; cache this reaction, so it can be used to deduplicate other, later "=" subscriptions
(swap! query->reaction assoc cache-key r)
(trace/merge-trace! {:tags {:reaction (reagent-id r)}})