Fork me on GitHub
#re-frame
<
2021-07-09
>
zackteo07:07:52

Hello, I have been trying to use re-frame-http-fx but I keep facing the issue detailed here - https://github.com/day8/re-frame-http-fx#status-0 Am able to access my GET http link with chrome and clj-http.client . Does that mean it is likely to be a cross-site scripting issue?

zackteo07:07:11

Okay yeap it was CORS, I used this https://github.com/r0man/ring-cors

4
hkjels07:07:31

There’s also re-frame-http-fetch or something like that

hkjels07:07:18

uses the fetch-api where cors is just a boolean away

zackteo07:07:16

I see I see!

zackteo16:07:41

Is ther a way to download the value of a subcription via re-frame-10x ?

p-himik17:07:56

The value of a sub is an arbitrary data. Before downloading it, you'd have to serialize it somehow.

p-himik17:07:27

And you can't write an algorithm that can serialize every possible JS value.

zackteo17:07:37

Okay, so in that sense the way about it is to put it as text into a div and copy that? o:

p-himik17:07:15

The "traces" tab allows you to: 1. Switch the output to show only subs 2. Filter those subs 3. Press the "Print" button to output the value to the browser's JS console

p-himik17:07:36

In the JS console, you can do whatever you want to it. To make it more comfortable, you can right click the printed value and press "Store object as global variable".

p-himik17:07:50

Goes well together with cljs-devtools and dirac.

p-himik17:07:23

Alas, the "subs" tab doesn't offer that "Print" button.

zackteo17:07:48

Not sure what I was doing wrong but couldn't use the print button in the traces tab. But it did however display out all the details for me to copy (unlike when I was looking at app-db tab where the last few values ended being left as ...

zackteo17:07:39

But thanks 🙂

zackteo17:07:46

Whoops right, it goes to the js console

lilactown21:07:00

I remember someone in this channel talking about constructing effects data using function chaining; who was that? tell me your secrets again 😛

p-himik21:07:18

If I understand you correctly, it was probably @U3JH98J4R

emccue21:07:11

@U4YGF4NGM refresh my memory a bit?

emccue21:07:39

I think i know what you are asking for, but i want to make sure before i go off

lilactown21:07:39

something about how you organize your events/effects, where instead of writing the effects in an event as data you use helper functions to construct the effects:

(rf/reg-event-fx
  ::something
  (fn [{:keys [db]} [_ foo bar]]
    (-> (do-thing {} foo)
        (do-another-thing bar)
        (do-one-last-thing))))

emccue21:07:20

yeah so the first bit is every event handler is a reg-event-fx for consistency

emccue21:07:36

and we only ever return :db and :fx

emccue21:07:03

for :fx, we have namespaces dedicated to them that look basically like this

emccue21:07:04

(rf/reg-fx
  ::alert
  (fn [contents]
    (js/alert contents)))

(defn alert [contents]
  [[::alert contents]])

(defn alert-uppercase [contents]
  (alert (string/upper-case contents)))

emccue21:07:29

so each "effect" is a function you call that returns a vector of vectors

emccue21:07:37

{:fx (alert "abc")}

emccue21:07:55

and if you want to combine fx, we have a helper function that concats them together

emccue21:07:25

{:fx (fx/in-order
         (alert "abc")
         (http-req {...}))}

emccue21:07:31

then if we have some process that we want to re-use in multiple handlers that will update state and perform some effects, we make a function

emccue21:07:54

(defn do-thing [db]
  {:db (inc db}
   :fx (alert-fx/alert "abc")})

emccue21:07:53

and we have another helper that lets us compose functions that have the signature db -> {:db, :fx}

emccue21:07:17

(rf/reg-event-fx
  ::abc
  (fn [{:keys [db]} [_ abc]]
    (let [process (compose-effect-fns
                             do-thing
                             (fn [db] {:db (* db 2)}))]
       (process db))))

emccue21:07:34

and for new stuff we aren't allowing dispatches within a handler - keeping dispatches to only stuff the user did or reactions to outside processes

emccue21:07:48

beyond that nowadays its gotten to

emccue22:07:37

(defevent abc
  [a b c]
  :handler
  (fn [{:keys [db]}]
    {:db (* db a b c)
     :fx (alert-fx/alert "AAA")}))

lilactown22:07:20

how do you handle like, "do a network request and then update the app-db"?

emccue22:07:01

which produces a constant event:abc equal to ::abc , a function abc, which when called will produce a vector you dispatch and a function handler:abc which is your re-frame handler

emccue22:07:21

do you mean app-db updates you do as soon as you launch the request or when it comes back

emccue22:07:42

(defevent successfully-did-thing
  [response]
  :handler
  (fn [{:keys [db]}]
    {:db (* db 2)}))

(defevent failed-to-do-thing
  [error]
  :handler
  (fn [{:keys [db]}]
    {:db (/ db 5)}))

(defevent do-thing
  [user-id]
  :handler
  (fn [{:keys [db]}]
    {:db (+ db 1)
     :fx (http-fx/request 
           {:uri "..." 
            :params {... user-id ... }
            :on-success successfully-did-thing
            :on-failure failed-to-do-thing})}))

emccue22:07:08

(we re-wrote the normal :http-xhrio to support some stuff like functions for on-success, but same concept)

emccue22:07:45

does that make sense?

5
p-himik22:07:21

@U3JH98J4R That's at least the third time I see how you write extensively on this topic. Have you considered making a blog post or at least a detailed gist about it? Would be easier to share, publicly improve upon, and discuss in a persistent manner that's not possible on this Slack.

emccue22:07:42

eh? its a pattern we still haven't worked out all the issues with and are using as a tool to patch up a large codebase. The ideal end point might be a library other than re-frame because it explicitly goes against what I think is intended design

emccue22:07:22

plus its literally just the way elm does stuff being forced onto re-frame - not without reason, but certainly without elegance

emccue22:07:02

but yeah next time it comes up I'll try to make a gist

👍 5
2
emccue22:07:05

also i don't have a blog to put it on - i know that makes me a heretic

lilactown22:07:21

a gist or something durable would be great for me! thanks for taking the time to write this down

lilactown22:07:31

bouncing around meetings rn but I will digest and maybe ask a couple more qs in a bit

lilactown23:07:00

mind if I post this in a work slack?

emccue00:07:24

do tell me any feedback though