Fork me on GitHub
#re-frame
<
2020-08-13
>
Rohan Nicholls15:08:56

Hi 👋. I’m trying to understand the concepts in re-frame. So.. An event fx is a specialised version of an fx, and is treated more like a reducer elsewhere?

👋 3
p-himik16:08:56

What do you mean by "event fx"?

Rohan Nicholls16:08:20

I’m reading through the docs and you have reg-event-fx and it seems to be a narrower version of event-fx. I was thinking it was specialised and was more like a reducer, but having read further I’m not so sure now.

p-himik16:08:09

I still don't understand what you mean by "event-fx". There are events, effects, and coeffects. Events you use in calls to dispatch, then at some point in time re-frame handles those events by calling event handlers that you have registered with the reg-event-* functions. Different functions except different kinds of event handlers. Effects are similar, but you can only trigger them in an event handler, and they're the only place where you should employ side effects. Coeffects are inputs to event handlers.

Rohan Nicholls16:08:02

Oh. Thanks. I saw code with ‘reg-event-fx’ and later with ‘reg-fx’ and was trying to understand the difference between the two. So the event one is just registering the event and by what it is returning ie. which keys in the map, those functions registered with that key with ‘reg-fx’ are then called?

Rohan Nicholls16:08:32

Btw. Thanks 🙏 very much for your help with my confusion.

p-himik16:08:58

Something like that, yes. > I saw code with ‘reg-event-fx’ and later with ‘reg-fx’ and was trying to understand the difference between the two. Don't look at the example before you read the documentation. The documentation contains everything and is very well written. No problem.

Rohan Nicholls16:08:28

That example was in the docs. I was just trying to sort it out in my head. I had it backward until you clarified the situation. I’m continuing with the docs, which are excellent. I’m going to finish them off before messing with examples. It is a very nice system from what I’ve seen so far. Just needs some mental adjustments. I was trying to translate it into redux/ngrx terms but that landed me on a slippery slope.

👍 3
gekkostate17:08:58

Hi all, quick question. I have two values in my db which I would like to track i.e., if those values change then I would like to dispatch an event which flips a boolean. I was thinking of creating an event-fx which has an interceptor which provides the subscription (like what the https://day8.github.io/re-frame/FAQs/UseASubscriptionInAnEventHandler/ suggest) and then if the subscription is fired then so will my event and I can flip the boolean. Is this a good way to go about this sort of thing? Thanks for your time!

lilactown17:08:44

@gekkostate when you say “flip a boolean” do you mean change another value in the app-db?

gekkostate17:08:16

Yes exactly.

lilactown17:08:12

rather than store it in the app-db, could you create a subscription that uses those two values?

lilactown17:08:01

ex.:

(rf/reg-sub
  ::some-boolean
  (fn [{:keys [val-a val-b]}]
    ;; compute the boolean based on `val-a` and `val-b`
    ))

lilactown17:08:22

that’s how you should typically create derived values from the app-db

lilactown17:08:25

there are certain cases where this doesn’t quite work, e.g. if you need to know when a value changes, not what the value is

gekkostate18:08:15

This is the case we have. 😛 So, we have some auto save functionality and we want to indicate to the user that the content has been saved. So, we have a marker which shows “Saved” when the save request is successful. However, when the user makes the changes (and they can make changes in multiple places e.g., textview, input fields) we want to show “Unsaved” marker. Right now the marker is governed by a boolean. So, if we can know when some of these values have changed then we can flip the boolean to false and wait for the auto save to kick in which will make it true.

lilactown17:08:05

by your verbiage it sounds like you need that, just laying out the typical case because it’s easy to colloquially use “change” and “based on some value” in the same way