Fork me on GitHub
#re-frame
<
2024-02-15
>
Ivan Fedorov14:02:39

Hey hey. When you subscribe inside a subscription (I know it is already a heresy), is it better to register this subscription with reg-sub-raw ? or still use reg-sub ? Or should I put it inside a signal function? Of course the official doc says that subscription calculation functions should be pure functions. this was probably discussed already, but I don’t understand how to formulate a search query for this.

p-himik14:02:44

It's not heresy, and that's exactly what reg-sub-raw is for. But if it can be a signal function, it should be a signal function, yes.

Ivan Fedorov14:02:22

Thanks a lot! What should I read to deepen the understanding of that? Or could you, pretty please, provide explanation for that?

p-himik14:02:17

I'd just refer you to the impl. :) It's very, very small.

p-himik14:02:17

Here's a documented example of using subscribe within a reg-sub-raw handler: https://day8.github.io/re-frame/flow-mechanics/#reg-sub-raw

Ivan Fedorov14:02:13

If I get it right, reg-sub-raw will not be cached or treated as a pure function, right? So using @(rf/subscribe [:smth]) inside a regular subscription is a bad move, because a regular subscription node is treated as a cacheable pure function.

p-himik14:02:14

The requirement for sub handlers to be pure functions is purely a design one. It makes things more predictable and easier to test. Of course, you should definitely avoid side-effecting subscription handlers or handlers that rely on mutable data structures that re-frame knows nothing about. But it's much less important to make them not rely on other subscriptions, since they're all a part of the same mechanism. Regarding the cache - subscribe does the caching, not any reg-... function. A regular subscription, i.e. one created with reg-sub, is nothing but a wrapper around what reg-sub-raw does. That wrapper deals with signals and tracing, and creates that needed reaction for you, that's about it.

❤️ 1
Ivan Fedorov14:02:49

Thanks a lot, Eugene!

👍 1
Kimo11:02:51

> What should I read to deepen the understanding of that? This might give you some thoughts - https://day8.github.io/re-frame/flows-advanced-topics/#reactive-context