Fork me on GitHub
#re-frame
<
2018-09-28
>
xfyre14:09:11

folks, anyone figured out how to use re-frame-http-fx on React Native? I can’t get it to work because of failing dependency on xmlhttprequest module

valtteri15:09:21

@xfyre I got it working by just copying the source file to my project and installing dependencies manually. This was last December so things may have changed since.

xfyre15:09:43

@valtteri actually I figured out what the problem is, but didn’t figure out a solution yet https://github.com/JulianBirch/cljs-ajax/issues/188 one obvious workaround is downgrading re-frame-http-fx to 0.1.4

valtteri15:09:41

@xfyre I think I encountered the same issue but I can’t remember the exact details. 😕 From git it seems like I’ve ended up copying the source of re-frame-http-fx under src and declaring dependency to cljs-ajax manually in project.clj. I’ll try to see if I can find any notes why I ended up doing exactly that.

valtteri16:09:14

Nah, apparently I didn’t make any notes but I hope you’ll find the solution that works for you also.

Garrett Hopper20:09:16

Can someone give me some guidance on dealing with promises in re-frame? I have a client API that returns promises, though I can't figure out how to handle it.

(rf/reg-sub
 :client
 :<- [:credentials]
 (fn [credentials]
   (when credentials
     (create-client credentials))))

(rf/reg-sub-raw
 :request
 (fn [_]
   (let [a      (r/atom nil)
         client @(rf/subscribe [:client])]
     (.then (.query client)
            #(reset! a (js->clj %)))
     (ratom/reaction @a))))
Part of the problem is that in order to create my client, I need to do some credentials federation first, so (rf/subscribe [:client]) is nil for the first few seconds. I've tried a number of ways of having the @(rf/subscribe [:client]) inside the reaction, but the problem is that it causes an endless loop.

Garrett Hopper20:09:45

Essentially, whenever [:client] changes, I need to issue the request, however I don't want to reissue the request once the r/atom (or db) changes.

Garrett Hopper20:09:27

Is there a cleaner way than this?

(rf/reg-sub-raw
 :example
 (fn [_]
   (let [a (r/atom nil)
         b (ratom/reaction
            (let [client @(rf/subscribe [:client])]
              (when client
                (.then (.query client)
                       #(reset! a %)))))]
     @b
     (ratom/make-reaction
      (fn [] @a)
      :on-dispose #(ratom/dispose! b)))))