Fork me on GitHub
#re-frame
<
2018-04-27
>
sveri04:04:11

@aaelony did you include [day8.re-frame/tracing-stubs "0.5.1"]in your production devs?

danielcompton07:04:47

They’re in the template to be added, but mustn’t be being added

danielcompton07:04:58

Or maybe the uberjar isn’t using that profile

sveri07:04:52

@aaelony Sorry, I meant dependencies and not devs.

aleksandr14:04:00

If different components require one (re-rfame/subscribe ...) what should to do pass it to components arguments or register it within each component separately?

aleksandr14:04:26

I explore the todomvc example and notice that the components have not props. Does Re-frame suggest to avoid props in components?

shaun-mahood15:04:41

@al.vyguzov: Not sure if I understand the question - are you trying to call the same component in different places so that they rely on different subscriptions to get the data they need?

aleksandr15:04:12

No, the different components use the same subscription. This components put in each other hierarchically

aleksandr15:04:27

And I try to realize what way is more right. Put this subscription to props or register it in different components separately

mccraigmccraig15:04:02

@al.vyguzov just subscribe at each place where you need it... no need to pass around the sub reactions

eoliphant15:04:52

Hi, does anyone have any idea why I might be getting a “no subscription handler registered for..” message for a subscription that, I’m 99% sure it’s reg-sub has been read in? I’ve required the namespace that contains the reg-sub in the namespace where the subscribe is. I’ve also played around with requiring it in the initial/core namespace just to be sure and still seeing the issue. i was also looking to see if there was any logging or something I could turn on that might help but i didn’t see any in the source

joelsanchez15:04:40

probably a typo, hard to tell without the source

eoliphant15:04:20

yeah I’ve verified that it’s not that 😞 it was working fine, It’s not even somethign I’ve changed. But I

eoliphant15:04:36

I’ve been working on a macro that would dynamically create subs based on a DSL (basically to setup calculated fields on a form).. This was one of the basic ones that was inline in the code, so that makes it even weirder

joelsanchez15:04:32

I know it's not related to your current problem, but creating a macro to dynamically register subs is probably wrong in 2 ways 1) nothing prevents you from registering subs from a defn (i.e. no macro needed) 2) subs take arbitrary arguments so you could use an argument instead of dynamically generated sub identifiers

eoliphant15:04:00

hmm, ok i thought about that too. But then there was the thing of avoiding overly generic subs lol

eoliphant15:04:22

I’m just thinking, I thought I needed to

eoliphant15:04:02

have the functions, etc setup

eoliphant15:04:18

these are level 3 subs

eoliphant15:04:57

So basically in my DSL this:

[:sf425/cash-on-hand
                   [:calc/subtract [:sf425/cash-received :sf425/cash-disbursed]]]
becomes this:
(re-frame.core/reg-sub
  :sf425/cash-on-hand
  (clojure.core/fn
    [___51800__auto__ ___51800__auto__]
    [(re-frame.core/subscribe [:form/cur-field :sf425/cash-received])
     (re-frame.core/subscribe [:form/cur-field :sf425/cash-disbursed])])
  (clojure.core/fn
    [args__51801__auto__]
    (fema-gmm.components.forms.calculations/run-calc
      :calc/subtract
      args__51801__auto__)))
The other complication is that in some cases I need to subscribe to another sub. So I have logic in a function to figure that out. But I thought I needed a macro to dynamically get everything setup properly, I can give it a whirl again

aleksandr15:04:57

I read about that problem at re-frame doc, but I forget where exactly :((

👍 4
eoliphant15:04:52

ill poke around in there

eoliphant16:04:31

Ok, it’s fixed but I have no idea why lol. There was something weird going on, in another component that caused an error unrelated to subscriptions, etc after the one I described. Yet fixing that fixed my issue

aaelony16:04:56

Hi @sveri, I can confirm that adding [day8.re-frame/tracing-stubs "0.5.1"] to :dependencies does fix the issue of the lein uberjar failing to build. That should likely be added to the lein re-frame template as well. Many thanks

chadhs16:04:36

what should you use for an initial db state for an item that is an api response? i’m not sure how to do this since cljs-ajax and cljs-http both use async calls.

chadhs16:04:05

or should i just be setting the initial db to a map of what i expect to be returned?

shaun-mahood17:04:15

@chadhs: I usually leave that part of my app-db either empty or with some default empty structure/value - as long as you populate it later with assoc-in it doesn't seem to cause any issues

chadhs18:04:07

@U054BUGT4 that was the tactic i was going to take but then my app wouldn’t render

chadhs18:04:30

@U054BUGT4 when you say empty did you have the key with an empty value or not even in the db to begin with?

shaun-mahood18:04:19

Pretty sure I have nothing in some of them - though my app-db isn't empty at the start

chadhs18:04:13

mine is pretty simple; initial db is current-time and an api response. but i can’t seem to escape the fact that i have to be in a go block to get that response.

shaun-mahood18:04:26

If you haven't already, you could see if there's something in https://github.com/Day8/re-frame/blob/master/docs/Loading-Initial-Data.md that might help

chadhs18:04:43

i haven’t yet thank you

chadhs18:04:51

@U054BUGT4 think i got this to work; the only odd behavior i notice now is the value will update and then flip back to the default; have you seen this

shaun-mahood18:04:52

Does it change back to the default after you change your code (if figwheel hot-reloads your code for you)?

chadhs18:04:27

oh it’s a time based dispatch

(defonce do-weather (js/setInterval dispatch-weather-event 10000))

chadhs18:04:13

so it’s fetching weather data and updating the db ever 10 secs (for testing purposes, will be every 30 mins in future

chadhs18:04:54

(re-frame/reg-event-db
 ::weather
 (fn [db [_ new-weather-data]]
   (assoc db/default-db :weather-data new-weather-data)))

shaun-mahood18:04:49

When is it resetting back to the default value?

chadhs18:04:28

immediately, and i set a static value to change to just to isolate the issue and prove it wasn’t related to my http call

shaun-mahood18:04:45

If you haven't done it yet, it may be worth adding https://github.com/Day8/re-frame-10x to your project to see if it can help you track down the problem. It's a bit overkill for a really small application but if you're planning on doing a decent amount of re-frame development in the future it's a pretty fantastic tool for understanding your application (https://github.com/flexsurfer/re-frisk is also very good and may fit better depending on what the future of your project looks like).

shaun-mahood19:04:32

The other thing you might want to check is how your initial render / re-renders are working, it's possible those could be causing some of your problems. I'm sure there are docs that go into this, but I can't find them right now. I cover it near the beginning of https://youtu.be/cDzjlx6otCU (and I can answer questions if some part of that video is unclear)

chadhs19:04:29

awesome thank you, ill give this a watch too

chadhs19:04:48

im sure i’m just missing some little nuance

shaun-mahood19:04:38

Hopefully you can get it all working nicely - there are also some tricky bits to keeping things like textboxes in sync, so if you end up doing more complicate UI components a good resource is https://github.com/Day8/re-com - when I can't use their components the patterns are really helpful to steal from 🙂

chadhs20:04:58

oh cool thnank you

chadhs20:04:37

i actually did spin up a fresh project and think i solved my issue and came up with a better approach

shaun-mahood21:04:55

That's great - the docs (and some of the examples and external resources) have a lot of really useful content. Feel free to bug me more as well and I'll help if I can.

eoliphant17:04:48

@chadhs this project has an appraoch for more complex initial state setup https://github.com/Day8/re-frame-async-flow-fx

👍 4
chadhs18:04:45

@eoliphant thnx ill take a look; may take some study im pretty new to reagent / re-frame

eoliphant18:04:16

Yeah, I love the two, really cool stuff. but I’ve found that while the basic concepts are pretty straightforward, a lot of little, yet common things (like ‘booting’ your SPA) aren’t always super clear. If you haven’t already, also check out the sample apps. I didn’t when I started (just blasted through the docs) and missed some key concepts/patterns

chadhs18:04:49

true and what im building is actually taking their clock sample and just adding weather data.

chadhs18:04:27

the odd behavior i’m seeing now is values reverting to the initial db state a second after updating

chadhs20:04:08

this was very helpful

chadhs20:04:18

fyi; think im good to go now

👍 4
eoliphant19:04:29

Hey @joelsanchez you were right, got it all working and no macro hell

#_(defn gen-calc-sub
  "Create the level 3 subscription.  If the field is in calcs, it's a calculated field
  and we should directly subscribe to it, otherwise subscribe to the actual field value"
  [calcs [field [calc args]]]
  (print "creating sub for: " field)
  `(rf/reg-sub
     ~field
     (fn [_# _#]
       ~(mapv (fn [arg] (if (calcs arg)
                          `(rf/subscribe [~arg])
                          `(rf/subscribe [:form/cur-field ~arg]))) args))
     (fn [args#]
       (calc/run-calc ~calc args#))))
(defn gen-calc-sub
  [calc-fields [field [calc args]]]
  (let [level-2-func-vec (mapv
                           (fn [arg] (if (calc-fields arg)
                                       (rf/subscribe [arg])
                                       (rf/subscribe [:form/cur-field arg]))) args)
        level-2-func (fn [_ _] level-2-func-vec)
        level-3-func (fn [args] (calc/run-calc calc args))]
    (rf/reg-sub field level-2-func level-3-func)))

#_(defmacro gen-calc-subs
  [calcs]
  (print "creating subs for: " calcs)
  (let [calc-fields (set (map first calcs))                 ;for quick membership test
        ]
    `(do [email protected](map #(gen-calc-sub calc-fields %) calcs))))

(defn gen-calc-subs
  [calcs]
  (let [calc-fields (set (map first calcs))]
     (doseq  [calc calcs] (gen-calc-sub calc-fields calc))))

joelsanchez21:04:46

hi @eoliphant glad you got it working! 🙂