This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # architecture (33)
- # asami (1)
- # aws (1)
- # babashka (19)
- # beginners (48)
- # bristol-clojurians (1)
- # calva (5)
- # cider (12)
- # cljdoc (15)
- # cljsrn (7)
- # clojure (151)
- # clojure-europe (19)
- # clojure-losangeles (1)
- # clojure-nl (2)
- # clojure-provo (4)
- # clojure-spec (9)
- # clojure-uk (21)
- # clojuredesign-podcast (28)
- # clojurescript (33)
- # core-typed (1)
- # cryogen (2)
- # css (6)
- # cursive (10)
- # data-science (1)
- # datomic (20)
- # events (3)
- # expound (72)
- # figwheel-main (5)
- # fulcro (43)
- # graalvm (6)
- # helix (1)
- # kaocha (13)
- # leiningen (2)
- # malli (1)
- # meander (93)
- # membrane (4)
- # off-topic (17)
- # pathom (6)
- # portland-or (5)
- # re-frame (25)
- # reagent (6)
- # reitit (7)
- # reveal (30)
- # shadow-cljs (25)
- # vim (2)
I feel like this should be an FAQ, but I’m damned if I can find anything anywhere. Is there such a thing as a “weak” subscription in re-frame? By which I mean the ability to have a component use some state within
app-db without updating whenever it changes?
app-db and want to use it when I re-create the Vega component, but not update the component when the cache changes.
There are clearly ways that I can address this (e.g. store the cached state within a normal
clojure.core/atom , or “reach around” re-frame and access
app-db directly) but it strikes me that what I really want is a way to tell re-frame that I’m interested in the value, but don’t want to update when it changes?
A direct access to
app-db won't work by itself because it's a ratom - changes to it will still trigger component re-rendering if you deref it within the component's render function.
With that being said, you can try deref'ing the subscription outside of the render function. Something like:
(defn my-component  (let [not-updating-state @(subscribe [...])] (fn  [:div not-updating-state])))
(although I’m coming around to the idea that a “normal”
clojure.core/atom might be the least unpleasant if this really isn’t something that re-agent supports out of the box).
Well if my approach works then it's something that Reagent does support out of the box. ;)
But your initial intention might be ill-placed. If what you cache is genuinely a part of your app's state then all is good. But if it's just a cache that can be reset at any moment without affecting the app in any meaningful way then I would argue that it should not be stored in
It’s state that would be in
(ultimately, I’m planning to implement something that allows collaborative editing, so I’ll need to broadcast this state to other people looking at the same diagram, and that will definitely be easier if I can use re-agent’s subscription mechanism to facilitate it)
Would binding reagent's
nil work in this situation?
What I gather from reagent's code is that this makes the deref non-reactive.
(binding [reagent.ratom/*ratom-context* nil] @(rf/subscribe [:sub]))
*ratom-context* is not a part of the public API, so even if it does work you still shouldn't use it as long as there are other solutions.
I'm using reitit and bidi. I would bet that for every more or less known CLJS router there's a user of it here.
I noticed something weird in my re-frame app, where extractor functions didn’t seem to be providing any benefit — in fact they seemed to make computation exponentially worse. I’ve boiled the example down to a few lines of code. I ran into the issue in 1.1.0, but it still seems to be there in 1.1.2. I feel like I must be doing something very wrong, so any help would be appreciated! Basically (code to follow), the library’s “propagation pruning” does not appear to be working for me. Dereferencing a subscription that uses another subscription as input causes that underlying subscription to be dereferenced twice. Another layer in the signal graph, and the base layer gets recomputed four times. Etc.
(require '[re-frame.core :as rf]) (rf/reg-event-db ::assoc (fn [db [_ k v]] (println ["assoc" k v]) (assoc db k v))) (rf/reg-sub ::get (fn [db [_ k]] (println "get " k) (get db k))) ;; Initialize some data: (rf/dispatch [::assoc :a 'a]) (= 'a @(rf/subscribe [::get :a])) ; "get :a" prints as a side effect ;; Let's build a new subscription in the signal graph: (rf/reg-sub ::a-derived :<- [::get :a] (fn [v _] (println "derived from a") (keyword v))) (= :a @(rf/subscribe [::a-derived])) ; "get :a" prints TWICE as a side effect. (= :a @(rf/subscribe [::a-derived])) ; no data has changed, but "get :a" still prints TWICE. ;; That 2x, how bad is it? (rf/reg-sub ::double-derived :<- [::a-derived] (fn [kw _] (println "double derived!") (str kw))) (= ":a" @(rf/subscribe [::double-derived])) ; "get :a" prints four times, "derived from a" prints twice ;; Maybe I should stop re-subscribing each time? (def double-derived (rf/subscribe [::double-derived])) @double-derived ; "get :a" prints four times, "derived from a" prints twice. @double-derived ; "get :a" prints four times, "derived from a" prints twice. (rf/reg-sub ::triple-derived :<- [::double-derived] (fn [s _] (println "triple derived!!") (str "Behold: " s))) (def triple-derived (rf/subscribe [::triple-derived])) @triple-derived ; "get :a" prints eight times @triple-derived ; "get :a" prints eight times
In my app, of course the subscriptions are dereferenced inside reagent views — this is just a REPL-friendly minimal example. Again, if anyone can tell me where I’m going wrong, I’d greatly appreciate it!
Interesting — on further testing, I think that this was a phantom bug caused by my initial attempt to verify propagation pruning. With more careful testing in the app, propagation pruning does seem to work. So I guess my question is, why does it work the way it does in that minimal example? And what’s the difference between a deref inside a leaf node (view) and a deref at the REPL?
Dereferencing subscriptions is designed to work properly only within render functions and reactions.
I don't know the exact reasons for why you see that behavior, but inside the render functions
reagent.ratom/*ratom-context* is properly set.
I'm just treating deref'ing subs anywhere other than render functions as UB.
@U2FRKM4TW Thank you, that is very helpful. The bug was a case of the observer effect, which is always confusing.
Actually, it's a pretty nice find - I debugged it a bit and created https://github.com/day8/re-frame/issues/657.