Fork me on GitHub
#re-frame
<
2019-08-23
>
xfyre12:08:12

@mynomoto yeah, basically

(reg-fx
  :navigate
  (fn [[navigator-obj route params]]
    (when (nil? navigator-obj)
      (throw (ex-info "nil navigator passed to :navigate effect" {:route route :params params})))
    (ocall navigator-obj :dispatch (navactions/navigate-to route params))
    )
  )

(defonce navigation-actions-map {:init       (gobj/get navigation-actions #js ["init"])
                                 :uri        (gobj/get navigation-actions #js ["uri"])
                                 :navigate   (gobj/get navigation-actions #js ["navigate"])
                                 :back       (gobj/get navigation-actions #js ["back"])
                                 :reset      (gobj/get navigation-actions #js ["reset"])
                                 :set-params (gobj/get navigation-actions #js ["setParams"])})

(defn navigate-to [route args]
  (println "[navactions] navigation action:" ((:navigate navigation-actions-map) (clj->js {:routeName route :params args})))
  ((:navigate navigation-actions-map) (clj->js {:routeName route :params args}))
  )

xfyre13:08:08

the link in #cljsrn was interesting though, I’ll check that out, too

xfyre13:08:49

I actually had another loosely related question. Is there any idiomatic way to pass app-db to custom effects?

mynomoto13:08:07

@xfyre Thanks, I will check both out. What do you mean by pass app-db to custom effects? You mean something defined by reg-fx?

xfyre13:08:43

yep, exactly - if I register some effect using reg-fx and this effect also needs data from app-db - what would be the most correct way to do it?

xfyre13:08:16

as for react-navigation - it’s quite orthogonal to re-frame per se

xfyre13:08:39

I’ve used re-frame for defining navigation as an effect (snippet above) and this is extremely convenient

xfyre13:08:08

you only need to capture your root navigator on startup using ref

mynomoto16:08:31

When I need that I create an extra reg-event-fx and separate the db part and let the reg-fx be specific and db independent, but not sure if this is what is recommended.

ag20:08:27

so there’s really no good way of accessing db in a subscription with a signal? e.g:

(reg-sub :foo ,,,)

(reg-sub 
  :bar
  <- [:foo]
  (fn [foo]
     ;; how do I access db here?
     ))

jjfine20:08:09

i think you can make a sub that returns the db and add that as another signal. problem w/ that is you will end up recalculating on every db change.

ag20:08:38

you mean like this:

(reg-sub 
  ::bar
  (fn [db]
  {:foo (subscribe [::foo])
   :db db})
   (fn [{:keys [foo db]}]
      ;; now have acces to db
     ))

jjfine20:08:46

i tried something like this a while ago. think i ended up unsafely directly accessing the db since it wasn’t important to have a super up to date db.

Lu20:08:49

You can access it without using the sugar syntax and by subscribing your foo together with the db

ag20:08:31

Yeah, just like I did above, but that’s not…. elegant

jjfine20:08:37

hmmm…i like that solution

Lu20:08:44

Oh yeah! Missed that

ag20:08:15

hmm I don’t think it’s working though… or… I’m missing something

Lu21:08:32

Straight from the docs:

(reg-sub
  :visible-todos

  ;; signal function - returns a vector of two input signals
  (fn [query-v _]
    [(subscribe [:todos])
     (subscribe [:showing])])

  ;; the computation function - 1st arg is a 2-vector of values
  (fn [[todos showing] _]
    (let [filter-fn (case showing
                      :active (complement :done)
                      :done   :done
                      :all    identity)]
      (filter filter-fn todos))))

Lu21:08:59

Just register a new subscription that returns the db

Lu21:08:16

Which is again what you’re doing anyways in the end

ag21:08:38

But I really want to access db with :<- [:foo] syntax 😢

ag21:08:49

it’s so nice and elegant

Lu21:08:31

Well then use reg-sub-raw so you have the db and you can subscribe in the reaction :)

ag21:08:17

what is reg-sub-raw ? I can’t find it

ag21:08:33

oh, no I found it

ag21:08:01

oh wow… I did not not it existed

👆 4
Lu21:08:21

That’s pretty handy have a go with it :)

mikethompson21:08:31

(reg-sub
  :db
  (fn [db _]
     db))

mikethompson21:08:30

And then

(reg-sub 
  :bar
  <- [:db]
  <- [:foo]
  (fn [[db foo] _]
     ;; access db here
     ))

jjfine21:08:23

yeah i tried that once and it didn’t really work for my use-case since the sub gets re-calculated on any change.

mikethompson21:08:24

But this is not a great idea because this subscription will fire every single time anything in app-db changes

Lu21:08:34

But why subscribing to the whole db anyways? Maybe you should have a thought about what you really need in your sub and just get that

Lu21:08:21

So you re-render only when the values you care about change