Fork me on GitHub
#re-frame
<
2018-07-23
>
Tommy16:07:49

If I wanted some computed value (a level 3 subscription) in an event handler, can I subscribe in the event in the event handler, or should I put re-frame/subscribe in a cofx handler, OR do I have to do it as an argument to the event handler (in the view)

manutter5116:07:35

A third alternative would be to make the computation a separate function that can be called by both the reg-sub and by the event handler. If it’s a time-consuming computation, though, I’d probably use one of the two alternatives you mentioned, with probably a slight preference for putting it as an argument in the event vector, on the grounds that it’s probably simpler that way.

Sen17:07:32

If I understand the question the problem can be solved with an interceptor, where you can get value from db which in turn should have a coeffect that you pass to your event handler.

Tommy17:07:00

I just put it in a re-frame/reg-cofx

Tommy17:07:12

thanks for the help guys!

Sen17:07:09

;; DB
(reg-fx
:computed-val
(// do whatever you want with the vallue in db))
;; events
(def your-interceptor
(re-frame.core/->interceptor
:before (fn [context] 
(let [computed-val (get-in context [:coeffects :event :computed-val ])]))))

(reg-event-fx
:your-event-handler
[your-interceptor]
{:db db
:set-computed-val-coeffect})

Tommy17:07:28

I dont think that applies to my situation, but it is an interesting pattern, thanks!

Sen17:07:57

:thumbsup:

b2berry19:07:16

What’s your favorite way to debug an http-xhrio event-fx whose :on-success isn’t firing? The network call returns a success and I see return data from the endpoint, but the :on-success isn’t dispatched and nothing shows up in the event trace (using fn-traced)

Sen19:07:04

:on-failure doesn't work either?

b2berry19:07:43

Not that I’ve seen, I was just in the process of shoving in some print statements, I’ve been relying on re-frame-tracing tool but maybe one is firing and the tracer isn’t catching it (seems unlikely)

Sen19:07:00

I would probably use an interceptor to print out whatever is happening on the event call

b2berry19:07:16

Well damn, looks like the failure dispatch is firing but not showing up in the trace

b2berry19:07:35

Just when I was starting to trust the tooling! 😄 Figures

Sen19:07:27

That's weird, but in :after you should be able to see what's happening, or if you use 10x there might be some info

eggsyntax19:07:45

fn-traced FTW!

eggsyntax19:07:00

Oh wait, that's locally defined 😊

eggsyntax19:07:05

Let's see, we cribbed that from re-frame-10x, though IIRC. looks

b2berry19:07:21

Yeah I’m using 10x with fn-traced but it’s not reliably reporting resulting dispatches from http-xhrio event-fx… I somehow got it to show the failure dispatch but then couldn’t reproduce that. Weird!

b2berry19:07:37

I’ve explored the trace through :after too and nothing

Sen19:07:48

Is there anything happening in Network tab of the browser?

Sen19:07:24

Oh, sorry, you mentioned it

Sen19:07:27

Maybe worth to reload something like repl, and maybe everything

b2berry19:07:54

Yeah I built the repl back up with fresh everything. Weirdness. I’ll keep tinkering

eggsyntax19:07:57

@b2berry is it possible that your handler fn's got the wrong arity?

b2berry20:07:37

I don’t think so, I injected print statements in them so I know they are firing now, just not seeing them in fn-traced

eggsyntax20:07:01

Cool. Very weird, I've definitely gotten to the point of trusting 10x pretty well.

b2berry20:07:04

Yeah me too this is the first time it’s had a hiccup for me. I’m a novice with web-development plumbing so who knows what I’ve messed up (and maybe it’s not me, but I’m going to say it probably is 😄 )

mikethompson22:07:37

@thmorriss there's an FAQ to answer your question

mikethompson22:07:13

@b2berry be sure to do a lein clean too. In my experience, it can sometimes fix the most intractable problems

💯 8