Fork me on GitHub
#re-frame
<
2017-06-08
>
souenzzo00:06:53

How to mock server response? I'm thinking in redefine :http-xhrio on my devtools namespece. So I can mock all HTTP requests

mikethompson03:06:31

@souenzzo can you ask in a different way? Give more information? Are you talking about stubbing out the re-frame-http-fx effects handler, maybe?

souenzzo04:06:39

Example: GET/user - anywhere in my app I call /user I will always mock a response defined somewhere. It will be usefull to test widgets with large data and implement some stuff without have the implementation of endpoint @mikethompson

joshkh15:06:59

is there any way to get a value from the app db in an fx? i have an effect for sending a message over a websocket and i want to drop in a token from the db

captainlexington15:06:20

Well, the db is still passed to the fx

captainlexington15:06:27

Like in this snippet

captainlexington15:06:32

(reg-event
   :my-event
   (fn [db [_ a]]
       {:http {:method :get
               :url    ""
               :on-success  [:process-blah-response]
               :on-fail     [:failed-blah]}
        :db   (assoc db :flag true)}))

jfntn15:06:29

hmm no if you registered an fx handler, it will only be given what the handler returned under that key

joshkh15:06:46

in my case it's an fx:

(reg-fx
  ::ws
  (fn [message]
    ; Would like to conj a token from app-db to message automatically
    (chsk-send! message)))

joshkh15:06:04

ah okay, thanks guys

jfntn15:06:50

I’d have to check but it’d be nice if injected interceptors were run before re-frame’s dofx, this would allow to add an interceptor that would enrich the returned fx, in this case filling in the value for you

joshkh15:06:47

yes, that would be nice!

jfntn15:06:22

Unfortunately that won’t work unless you redefine reg-event-fx https://github.com/Day8/re-frame/blob/master/src/re_frame/core.cljc#L89

joshkh15:06:45

so then i could just chuck the token in an atom somewhere and deref it in my already side-effecty fx

joshkh15:06:55

what could go wrong 🙂

jfntn15:06:25

then you should make that an fx too

captainlexington15:06:29

Well, could you bake the token into the message before it gets passed to the handler?

captainlexington15:06:38

I think this is a part of re-frame I've never seen before

jfntn15:06:25

simplest solution would be to add the token to the fx’s data indeed

joshkh15:06:35

i could but that's a lot of repeated code for every handler that uses my api

jfntn15:06:21

could also pass the whole db for convenience, let the fx handler pluck the token out

captainlexington15:06:58

Well, I guess the follow-up question is: would it work if you called the interceptors before do-fix? Is that a feasible pull request, or would it be an api-breaking change?

joshkh15:06:21

this is really more of a thought exercise for me

joshkh15:06:29

but the good news is that i think i can do it with an interceptor

joshkh15:06:31

specifically, in the :after context i can edit any :effects that match my ::ws key

joshkh15:06:42

hey, it worked. great.

(def jwt
  (->interceptor
    :id :jwt-interceptor
    :after (fn trimv-after
             [{:keys [coeffects] :as context}]
             (update-in context [:effects :myapp.fx/ws 1] (partial cons (get-in (:db coeffects) [:identity :token]))))))

bouchardsyl16:06:15

hi, pretty new to re-frame, I'm using a library called "re-frame-storage" to persist some values of state in local storage (that part does work), but I'd appreciate advice to adapt the usual db initialisation to load local storage values OR defaults?

akiroz16:06:27

@lessylvains hi, author here~ are you using it as (co)fx or interceptor mode?

akiroz16:06:48

If you use it in interceptor mode, in your init event: - check if your persisted app-db sub-key already has valid data (loaded from local storage) - if not, initialize it with your default values

bouchardsyl16:06:45

@akiroz thank you I will try that 🙂

jfntn16:06:23

@joshk oh cool, I read the src wrong, and forgot to parse the interceptors right to left for the after functions

joshkh16:06:40

thanks just the same for looking

mikethompson22:06:01

@joshkh @akiroz > but the good news is that i think i can do it with an interceptor > specifically, in the :after context i can edit any :effects that match my ::ws key Yes, that's the approach which came to my mind. BTW. I'm hoping the 3rd inforgraphic panel is useful for conveying the detail: https://github.com/Day8/re-frame/blob/master/docs/EventHandlingInfographic.md combined with the words in: https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md#executing-a-chain