Better naming and comments.
This commit is contained in:
parent
b4d3e4cab6
commit
4741cd73d3
|
@ -33,13 +33,16 @@
|
||||||
|
|
||||||
(defn cache-and-return
|
(defn cache-and-return
|
||||||
"cache the reaction r"
|
"cache the reaction r"
|
||||||
[v dynv r]
|
[query-v dynv r]
|
||||||
(let [cache-key [v dynv]]
|
(let [cache-key [query-v dynv]]
|
||||||
;; when this reaction is nolonger being used, remove it from the cache
|
;; when this reaction is nolonger being used, remove it from the cache
|
||||||
(ratom/add-on-dispose! r #(do (swap! query->reaction dissoc cache-key)
|
(ratom/add-on-dispose! r #(do (swap! query->reaction dissoc cache-key)
|
||||||
(warn "Removing subscription: " cache-key)))
|
(warn "Removing subscription: " cache-key)))
|
||||||
|
|
||||||
;; cache this reaction, so it can be used for identical subscriptions
|
(.log js/console "Dispatch site: ")
|
||||||
|
(.log js/console (:dispatch-site (meta query-v)))
|
||||||
|
|
||||||
|
;; cache this reaction, so it can be used to deduplicate other, identical subscriptions
|
||||||
(swap! query->reaction assoc cache-key r)
|
(swap! query->reaction assoc cache-key r)
|
||||||
|
|
||||||
r)) ;; return the actual reaction
|
r)) ;; return the actual reaction
|
||||||
|
@ -55,16 +58,16 @@
|
||||||
|
|
||||||
(defn subscribe
|
(defn subscribe
|
||||||
"Returns a Reagent/reaction which contains a computation"
|
"Returns a Reagent/reaction which contains a computation"
|
||||||
([v]
|
([query-v]
|
||||||
(if-let [cached (cache-lookup v)]
|
(if-let [cached (cache-lookup query-v)]
|
||||||
(do (warn "Using cached subscription: " v)
|
(do (warn "Using cached subscription: " query-v)
|
||||||
cached)
|
cached)
|
||||||
(let [query-id (first-in-vector v)
|
(let [query-id (first-in-vector query-v)
|
||||||
handler-fn (get @qid->fn query-id)]
|
handler-fn (get @qid->fn query-id)]
|
||||||
(warn "Subscription crerated: " v)
|
(warn "Subscription crerated: " query-v)
|
||||||
(if-not handler-fn
|
(if-not handler-fn
|
||||||
(error "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription."))
|
(error "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription."))
|
||||||
(cache-and-return v [] (handler-fn app-db v)))))
|
(cache-and-return query-v [] (handler-fn app-db query-v)))))
|
||||||
|
|
||||||
([v dynv]
|
([v dynv]
|
||||||
(if-let [cached (cache-lookup v dynv)]
|
(if-let [cached (cache-lookup v dynv)]
|
||||||
|
|
Loading…
Reference in New Issue