Fork me on GitHub
#re-frame
<
2018-08-05
>
hoopes01:08:19

hi, quick question that i can’t seem to find in the docs - can i subscribe to an event in different places? for example, :login-success i’d like to react to with different things - maybe create a websocket, etc. (currently, I want to react to that with a couple different statecharts). is there another style i should be looking at for this? Or am i thinking about it incorrectly? Thanks much for any advice!

hoopes01:08:43

like, in regular JS, i can latch a couple different things onto a promise, and when it succeeds, a couple functions are called - looking for something along those lines

soulflyer01:08:53

You can subscribe to the same information in the database multiple times. You don't subscribe to an event. An event gets run and it may set some info in the database, but you don't subscribe to the event directly.

hoopes01:08:18

right, so i can use a subscription … just about anywhere? Can i create some random function that subscribes to something, and is run when it changes?

soulflyer01:08:33

I'm going to tentatively say yes to that. I don't fully understand how it all works and what exactly gets run when, however....

hoopes01:08:27

(reg-sub app-initialized [db]
   (get-in [:some :path] db))

(defn random-func []
  (let [is-init? (rf/subscribe app-initialized)]
   (when (@is-init?) (do-something)))
still not right, but ya know

hoopes01:08:03

uh…except the second part there is actually correct, and subscribing to that thing right

soulflyer01:08:41

I have a similar situation where I call out to an api using http-fx. The call takes a map including an :on-success which expects an event. Many of these need to trigger multiple events in my app, so I define an event for it which basically just fires off 2 or 3 other events using dispath-n

hoopes01:08:36

yeah, that might be more “obvious” - instead of things listening for the one event (that might be hard to trace), i’m explicitly dispatching a set?

soulflyer01:08:56

Thats what I do. I have a call to an api that pulls a load of info from a database so it can take a few seconds. When it comes back I need to alter the ui in a few places and do a couple of other things. One event dispatching multiple others is fine and probably less error prone than having lots of things watching seperately

hoopes01:08:37

right - thanks very much for the feedback! appreciated for sure

soulflyer01:08:42

Not sure if your code above would work. I use subscription largely in the context of the UI where I know things will get updated. Gut feeling is it would be fine, but you probably need input from someone with more experience of re-frame. Or try it and see.