|
35 | 35 | (defn cache-and-return
|
36 | 36 | "cache the reaction r"
|
37 | 37 | [query-v dynv r]
|
38 |
| - (let [cache-key [query-v dynv]] |
39 |
| - ;; when this reaction is no longer being used, remove it from the cache |
40 |
| - (add-on-dispose! r #(trace/with-trace {:operation (first-in-vector query-v) |
41 |
| - :op-type :sub/dispose |
42 |
| - :tags {:query-v query-v |
43 |
| - :reaction (reagent-id r)}} |
44 |
| - (swap! query->reaction |
45 |
| - (fn [query-cache] |
46 |
| - (if (and (contains? query-cache cache-key) (identical? r (get query-cache cache-key))) |
47 |
| - (dissoc query-cache cache-key) |
48 |
| - query-cache))))) |
49 |
| - ;; cache this reaction, so it can be used to deduplicate other, later "=" subscriptions |
50 |
| - (swap! query->reaction (fn [query-cache] |
51 |
| - (when debug-enabled? |
52 |
| - (when (contains? query-cache cache-key) |
53 |
| - (console :warn "re-frame: Adding a new subscription to the cache while there is an existing subscription in the cache" cache-key))) |
54 |
| - (assoc query-cache cache-key r))) |
55 |
| - (trace/merge-trace! {:tags {:reaction (reagent-id r)}}) |
56 |
| - r)) ;; return the actual reaction |
| 38 | + ;; Only cache the reaction when subscribe is called from a reactive context |
| 39 | + ;; where a reagent component will be able to purge the reaction from the subscription cache |
| 40 | + ;; when the component is dismounted. |
| 41 | + (when (reactive?) |
| 42 | + (let [cache-key [query-v dynv]] |
| 43 | + ;; when this reaction is no longer being used, remove it from the cache |
| 44 | + (add-on-dispose! r #(trace/with-trace {:operation (first-in-vector query-v) |
| 45 | + :op-type :sub/dispose |
| 46 | + :tags {:query-v query-v |
| 47 | + :reaction (reagent-id r)}} |
| 48 | + (swap! query->reaction |
| 49 | + (fn [query-cache] |
| 50 | + (if (and (contains? query-cache cache-key) (identical? r (get query-cache cache-key))) |
| 51 | + (dissoc query-cache cache-key) |
| 52 | + query-cache))))) |
| 53 | + ;; cache this reaction, so it can be used to deduplicate other, later "=" subscriptions |
| 54 | + (swap! query->reaction (fn [query-cache] |
| 55 | + (when debug-enabled? |
| 56 | + (when (contains? query-cache cache-key) |
| 57 | + (console :warn "re-frame: Adding a new subscription to the cache while there is an existing subscription in the cache" cache-key))) |
| 58 | + (assoc query-cache cache-key r))) |
| 59 | + (trace/merge-trace! {:tags {:reaction (reagent-id r)}}))) |
| 60 | + r) ;; Always return the actual reaction, even if it was not cached. |
57 | 61 |
|
58 | 62 | (defn cache-lookup
|
59 | 63 | ([query-v]
|
|
63 | 67 |
|
64 | 68 | ;; -- subscribe ---------------------------------------------------------------
|
65 | 69 |
|
66 |
| -(defn warn-when-not-reactive |
67 |
| - [] |
68 |
| - (when (and debug-enabled? (not (reactive?))) |
69 |
| - (console :warn |
70 |
| - "re-frame: Subscribe was called outside of a reactive context.\n" |
71 |
| - "See: https://day8.github.io/re-frame/FAQs/UseASubscriptionInAJsEvent/\n" |
72 |
| - "https://day8.github.io/re-frame/FAQs/UseASubscriptionInAnEventHandler/"))) |
73 |
| - |
74 | 70 | (defn subscribe
|
75 | 71 | ([query]
|
76 |
| - (warn-when-not-reactive) |
77 | 72 | (trace/with-trace {:operation (first-in-vector query)
|
78 | 73 | :op-type :sub/create
|
79 | 74 | :tags {:query-v query}}
|
|
92 | 87 | (cache-and-return query [] (handler-fn app-db query)))))))
|
93 | 88 |
|
94 | 89 | ([query dynv]
|
95 |
| - (warn-when-not-reactive) |
96 | 90 | (trace/with-trace {:operation (first-in-vector query)
|
97 | 91 | :op-type :sub/create
|
98 | 92 | :tags {:query-v query
|
|
0 commit comments