Fork me on GitHub
#re-frame
<
2018-11-19
>
mikethompson00:11:39

There are a set of links at the top of that page which explain it. In essence ... > Views are not imperative and they don't initiate database queries. Instead, views are simply a rendering of the current application state.

mikethompson00:11:16

@achikin so please be sure to read through the material on the end of those provided links.

achikin13:11:48

I have read the links. Those articles does not explain real practical downsides of fetching data inside a subscription.

achikin13:11:42

Moreover is seems like fetching using an event encourages more imperative approach when you need to provide concrete steps for fetching data for each component instead of doing it in a more reactive way as when you fetch data from a re-frame database itself.

mikethompson13:11:11

In re-frame, nothing "new" happens without an event. In turn, it is the associated event handler which knows how the event should change the world. In which case, it is event handlers which initiate (via an effect) the fetch.

mikethompson13:11:39

The job of a subscription is to simply deliver data

đź‘Ť 4
mikethompson13:11:53

it is reactive, not imperative.

mikethompson13:11:34

Imperative stuff happens via event handlers (and the effects they nominate)

achikin13:11:29

> The job of a subscription is to simply deliver data Why is there a difference between delivering from re-frame database and delivering from a server?

mikethompson13:11:38

So in re-frame, views are not imperative. And neither are subscriptions. They are simply nodes in a reactive signal graph.

mikethompson13:11:21

Delivering from a server is stateful and async, etc.

mikethompson13:11:34

There are timeouts and failures, etc.

achikin13:11:55

Let's imagine I have fired and event to deliver some data from a server and then I've rendered a component, which is subscribed to that data. Exactly as you suggest. It is async and prone to timeouts and failures too.

mikethompson13:11:42

Yes, so the goal is to limit that kind of stateful logic to event handlers. Spreading that kind of logic around into views or their subscriptions is not recommended.

mikethompson13:11:20

After all, if you want to, you can just put the GET into the view itself componentWillMount

mikethompson13:11:33

Why bother putting it into a subscription?

mikethompson13:11:20

But hey. If you believe you want to make subscriptions side-effect-y, the blueprint is there in that document. We certainly explored it and then backed away. But your experience may be different.

achikin13:11:22

> After all, if you want to, you can just put the GET into the view itself componentWillMount You have provided good reasons not to do that. Especially that components become less reusable when they are coupled with requests.

mikethompson13:11:06

It doesn't sound like I've convinced you

mikethompson13:11:39

I'll put some more thought into my explanations, but that's all I got for the moment :-)

achikin13:11:00

But subscriptions are about fetching data from a model, so I'm trying to understand why can't they fetch from the server.

achikin13:11:13

Believe me I'm open to any explanations.

achikin13:11:36

But at the moment is sounds like "Don't do that because Mike told you so" 🙂

mikethompson13:11:47

No, not at all.

mikethompson13:11:55

I just explained my reasons

mikethompson13:11:09

But they were obviously not very convincing for you

mikethompson13:11:57

For me, the reason is simple: don't make subscriptions have side effects.

mikethompson13:11:12

Only event handlers should have side effects.

mikethompson13:11:13

In our experience, that rule simplified things.

achikin13:11:44

> For me, the reason is simple: don't make subscriptions have side effects. I expected something like "We did this in our project and that lead to this, this and that" rather than "should not have side effects because should not have side effects"

achikin13:11:37

Believe me I do understand your general line and efforts to separate and isolate side effects. I just wonder if it worth it from practical standpoint.

achikin13:11:53

If we could fire fetch events from subscriptions - we could care less about repeating fetch events in every view change event. This is a huge plus. But what are the downsides?

mikethompson13:11:38

A more complicated computational model

mikethompson13:11:35

Because side effects can happen in more places.

mikethompson13:11:26

Everything is a trade off. I find it useful to keep the role of each domino clearly delineated.

mikethompson13:11:43

I hope all that was useful. Gotta go.

mikethompson13:11:03

Useful even if I didn't provide compelling reasons in your view

achikin13:11:34

Thank you!

mikethompson20:11:14

@achikin I don't have much time right now but I slept on my explanations and would put my position like this ... We're programmers and we create abstractions for a living because it is our primary weapon for controlling complexity. As a result, when we hear subscriptions access data from app-db we're tempted to turn it into the more general (abstract) idea that subscriptions access data including from the server. I actually went down this path at one point (hence the documentation on how to do it) but realised it broke another more important abstraction in re-frame - the computational model. It makes subscriptions side-effect-y and there's always a cost for side-effect-y-ness. re-frame goes to a fair bit of effort to corral side-effects into just one place (one domino) because the overall architecture and the apps it produces are easier to understand as a result. State machines are simpler to understand than full Turing machines because of the constraints they impose on computation. re-frame constrains you too and (hopefully) produces a simpler computational model as a result. One of the constraints is that side-effect-y stuff only happens in one place. All of the above is pretty much what I originally said, but perhaps repackaged to make more sense. Maybe.

mikethompson00:11:55

Also, have a look at ... https://purelyfunctional.tv/article/react-vs-re-frame/ specifically the section "Reacters load data on mount"

4
urzds14:11:34

Is there a way to register a global interceptor that is executed for all events? I am specing my :db and would like this check to be executed everywhere to catch trouble as quickly as possible.

mikerod14:11:55

@urzds It is common to define common interceptors in var and use them everywhere (or wherever you want)

ingesol14:11:08

@urzds Not that I know of. My solution is usually to make my own event registration functions that call the re-frame ones with some interceptors

urzds14:11:10

Thanks to both of you, both seem like a good approach.

urzds14:11:34

It is not required that there be a :db in :effects, right?

ingesol14:11:13

That´s correct, a nil db means no change

urzds14:11:21

Is it possible to "hook into re-frame's specs"? I.e. use re-frame's specs, but add my own for :db, which re-frame cannot spec by itself?

urzds14:11:28

Hm, re-frame does not even have specs, so that idea was moot.

ingesol16:11:02

Basically you can´t really “extend” the framework like that, but you can wrap it with your own functions

mikerod20:11:15

@mikethompson ah yes, that’s better than the link I mentioned

mikerod20:11:32

I have read that before too, just forgot about it being there

mikethompson21:11:17

@mikerod yeah, it is an indictment on the organisation of the docs that people know they've read something sometime, but then can't find it again. But I'm not sure what to do about it.

mikerod21:11:10

well, FAQ was a pretty obvious place to look for this - I should have thought that. It is hard to know what to do about the general situation though. I do know I’ve knew I read something there before, but had to search a bit to find it. The organization overall is good and coherent. It’s just recalling the location of specific details I think.