Fork me on GitHub
#re-frame
<
2021-07-07
>
Dmytro Bunin09:07:11

I was wondering if there is any downsides of using a sub inside of the body of computation function of another sub. So something like:

(rf/reg-sub
  :c
  :<- [:a]
  (fn [a _]
    (let [b @(rf/subscribe [:b])]
      (+ a b))))
I could find something about having them in events, but nothing for subscription. Is this considered fine?

Oliver George09:07:58

I’ve only ever regretted the complexity associated with doing this. Consider whether you can refactor the other subscription to extract a logic helper which you could reuse directly.

p-himik10:07:27

But from the internals point of view - that would work just fine. It's just a reaction used inside another reaction - nothing wrong with that.

Pavel Klavík13:07:38

So there is no problem with this? I was told by someone that this might lead to memory leaks, so I was using reg-sub-raw instead for this type of subscriptions, where output of one subscription is later used as input of another one.

p-himik13:07:28

There shouldn't be a leak here. A subscription is removed from the cache when its list of watchers becomes empty. It should be the case here.

Pavel Klavík13:07:23

How does it work internally, when I have say

(if @(rf/subscribe [:a])
  @(rf/subscribe [:b]
  @(rf/subscribe [:c])
The function code runs, when it encounters subscription to :a, depending on it's values, it will check :b or :c while it will stop depending on the other one?

p-himik13:07:36

That's a completely separate question. And yes, because that's how if works in general - one of the subs will stop being used, its watches list will have one item fewer, and the cache will be pruned if that list becomes empty. It should be fairly easy to check.

Pavel Klavík13:07:11

Makes sense. I was reading the code of reagent.ratom little bit now. I will have to find some time to go through everything to understand it better.