Merge branch 'master' of https://github.com/Day8/re-frame
This commit is contained in:
commit
837182c1d4
|
@ -1,7 +1,7 @@
|
|||
(ns re-frame.subs
|
||||
(:require
|
||||
[re-frame.db :refer [app-db]]
|
||||
[re-frame.interop :refer [add-on-dispose! debug-enabled? make-reaction ratom? deref? dispose! reagent-id]]
|
||||
[re-frame.interop :refer [add-on-dispose! debug-enabled? make-reaction ratom? deref? dispose! reagent-id ratom]]
|
||||
[re-frame.loggers :refer [console]]
|
||||
[re-frame.utils :refer [first-in-vector]]
|
||||
[re-frame.registrar :refer [get-handler clear-handlers register-handler]]
|
||||
|
@ -15,7 +15,7 @@
|
|||
;; De-duplicate subscriptions. If two or more equal subscriptions
|
||||
;; are concurrently active, we want only one handler running.
|
||||
;; Two subscriptions are "equal" if their query vectors test "=".
|
||||
(def query->reaction (atom {}))
|
||||
(def query->reaction (ratom {}))
|
||||
|
||||
(defn clear-subscription-cache!
|
||||
"Causes all subscriptions to be removed from the cache.
|
||||
|
@ -44,14 +44,21 @@
|
|||
[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)
|
||||
(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)}}
|
||||
nil)))
|
||||
(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)
|
||||
(swap! query->reaction (fn [query-cache]
|
||||
(when debug-enabled?
|
||||
(when (contains? query-cache cache-key)
|
||||
(console :warn "re-frame: Adding a new subscription to the cache while there is an existing subscription in the cache" cache-key)))
|
||||
(assoc query-cache cache-key r)))
|
||||
(trace/merge-trace! {:tags {:reaction (reagent-id r)}})
|
||||
r)) ;; return the actual reaction
|
||||
|
||||
|
|
Loading…
Reference in New Issue