Fork me on GitHub
#re-frame
<
2020-02-13
>
markgdawson07:02:44

Question about layered subscriptions. I want to do something like:

@(rf/subscribe [:my-sub id])
I want [:my-sub] to be a subscription derived from
[:another-subscription id]
How can I do this? I'd normally have
:<- [:another-subscription]
But how do I tell :my-sub that the argument (id) should be passed on into :another-subscription, or am I doing something that isn't idiomatic? πŸ™‚

p-himik08:02:41

Two ways to solve it: 1. If it's used only in one place: @(subscribe [@(subscribe [:something id]) id]) 2, If it's used in multiple places, reg-sub-raw that uses other subs sounds like what you need.

flowthing08:02:53

(re-frame/reg-sub :my-sub
  (fn [[_ id _] _]
    (re-frame/subscribe [:another-subscription id]))
  (fn [result-of-another-subscription _]
    (do-something-with result-of-another-subscription)))

p-himik08:02:05

That's not the same thing as replacing a sub ID though, as the OP wants.

flowthing08:02:13

Oh, in that case I misunderstood the question.

markgdawson08:02:12

Sorry both, I think I might have explained badly. Looking back "id" was a poor choice of parameter. I think @U4ZDX466T has the use case I'm looking for, but the nested subscribes by @U2FRKM4TW very cool. So thank you both! πŸ™‚

πŸ‘ 4
ingesol08:02:49

For the suggestion from @U4ZDX466T, a good beginner trick is to first skip all the destructuring and just print values coming through, so you properly understand what’s going on. Common stumbling point:

(f/reg-sub :my-derived-sub
  (fn [arg1 arg2]
    (println arg1 arg2)
    (f/subscribe [::my-sub (second arg1)]))
  (fn [sub-value arg2]
    (println arg2)
    (do-something sub-value)))

πŸ‘ 8
ingesol08:02:43

I see people struggling a lot with these things, both in events and subs (myself included) πŸ™‚

flowthing08:02:00

Yeah, that's good point. I always forget the order in which the arguments arrive myself, too. πŸ™‚

p-himik08:02:09

Also, don't use the 2-arity version of the signal function: https://github.com/day8/re-frame/wiki/Dynamic-Subscriptions

4
4
rschmukler19:02:00

Hey! Does live-reloading / redefining a subscription not cause it to get recomputed? I'm working w/ live-reload in a cider session and it looks like even if I create a new subscription it still returns the old value. I presume that this is because the underlying dependencies (db, or higher subs in layer 3) haven't changed so it's using a cached value? Is there any way to force it to re-evaluate on redefinition?

rschmukler19:02:00

Thank you so much! Should have checked the FAQs, sorry. This is exactly what I needed πŸ™‚

πŸ‘ 4