Fork me on GitHub
#re-frame
<
2019-04-30
>
jeeq03:04:31

What is the best way to handle creating mutiple similar components? Let’s say for example my app-db is

{:gods {:a {:name "A"
                         :nature "T"}
                   :b {:name "B"
                        :nature "F"}
                    :c {:name "C"
                         :nature "random"}}}
Is it recommended to create subscriptions for :god-a-nature, :god-b-nature, :god-c-nature separately? (seems dirty…) Would it be the same for names as well? Now if I want to create list of these 3 gods my initial thought was that I would pass(?) god-a to reagent component (form-2) and set up subscriptions for the specific god. But actually it doesn’t work that way because subscriptions are defined separately and I would have to subscribe to specific subscriptions I’ve set up for each god and each property. What is the recommended way to write this? Sorry, I’m a noob at clojure and re-frame, just trying to learn.

caleb.macdonaldblack04:04:02

I can't seem to be able to use with-redefs on an interceptor. Is there any other way I can mock out this interceptor? I'm using day8.re-frame/test

caleb.macdonaldblack04:04:51

@jeeq I didn't test if any of that works. But hopefully it gives you an idea of how to deal with that data structure.

lilactown04:04:40

@caleb.macdonaldblack how are you using with-redefs?

caleb.macdonaldblack04:04:17

with-redefs has no effect here. The real interceptor is being called instead

lilactown04:04:43

updates are execute asynchronously, and with-redefs is synchronous

caleb.macdonaldblack04:04:31

Yeah I know that with-redefs doesn't work for things like promises. Not sure how I could go about testing this using rf-test though

caleb.macdonaldblack04:04:58

I could break the function out and test that.

caleb.macdonaldblack04:04:17

Although if I could mock that interceptor I feel that would be best

lilactown04:04:23

dispatches are async, so you can’t rely on with-redefs

lilactown04:04:15

it’s been awhile since I did a lot of things with interceptors, but I think you could put your own interceptor in the chain that could replace the sync-user-persisted one

lilactown04:04:53

the interceptor chain is kept in the context I think, so you can have an intercepter that on :begin loops through the interceptors and replaces the one with :id :sync-user-persisted with your :fake-interceptor

lilactown04:04:25

it kind of begs the question what you’re testing here?

caleb.macdonaldblack04:04:17

I'd like to test the event handler function and that the interceptor is being called

caleb.macdonaldblack04:04:43

Interestingly enough this with-redefs is working

caleb.macdonaldblack04:04:08

rf-test/run-test-sync makes rf/dispatch synchronous for testing purposes

lilactown04:04:57

yep, my other thought was to use dispatch-sync

caleb.macdonaldblack04:04:09

So with-redefs should and does work. Now that I know I can redef interceptors I just need to figure out what is preventing my other one from working

lilactown04:04:48

my guess is that reg-event-fx captures the original function

caleb.macdonaldblack04:04:14

with-redefs doesn't have any effect with this arrangement

caleb.macdonaldblack04:04:10

So perhaps interceptors are async

lilactown04:04:17

I think what’s happening is that reg-event-fx stores the interceptor foobar as a value the first way

caleb.macdonaldblack04:04:38

I'm defining that before I use with-redefs

caleb.macdonaldblack04:04:50

And it's referring to the value

lilactown04:04:38

adding an indirection of (foobar) helps because it does a lookup of what’s in the foobar var at when the event is run

lilactown04:04:29

maybe even using a var-quote:

(rf/reg-event-fx
 :foobar
 [#'foobar]
 (fn [_ _ ]))
would fix it

caleb.macdonaldblack04:04:34

I tried that it doesn't work

caleb.macdonaldblack04:04:13

Breaking out the interceptor function and redefining that works

lilactown04:04:45

hm, does it work without var-quoting the after?

caleb.macdonaldblack04:04:07

Nope. Must have var-quoting

caleb.macdonaldblack05:04:00

Alright that is working for me now. Thanks for your help @lilactown 🙂

lilactown05:04:23

duckie sure thing!

dpsutton16:04:43

anyone get NPE errors with re-frame-10x here:

(defn log-trace? [trace]
  (let [render-operation? (or (= (:op-type trace) :render)
                              (= (:op-type trace) :componentWillUnmount))
        component-path    (get-in trace [:tags :component-path] "")]
    (if-not render-operation?
      true
      (not (str/includes? component-path "devtools outer")))))
the component-path seems to be null

dpsutton16:04:49

also complaints about multiple copies of react when things go wrong and reloading. We are react 16 and using 0.4.0 of 10x. Just wondering if anyone has seen this before?

dpsutton16:04:28

this is constant and makes it unusable unfortunately