re-frame

https://github.com/Day8/re-frame/blob/master/docs/README.md https://github.com/Day8/re-frame/blob/master/docs/External-Resources.md
Clément Ronzon 2021-03-05T17:12:51.122300Z

Hey guys and gals, So I learned that there is a way to inject a subscriber into another:

(reg-sub ::foo
         (fn [_ [_ x]]
           ...))

(reg-sub ::bar
         :<- [::foo "arg"]
         (fn [foo [_ y]]
           ...))
Is there a way to make that injection dynamic? Meaning: the arguments passed to the injected subscriber would match the ones passed to the main subscriber. Something like this:
(reg-sub ::foo
         (fn [_ [_ x]]
           ...))

(reg-sub ::bar
         :<- (fn [_ [_ y]] [::foo y])
         (fn [foo [_ y]]
           ...))

p-himik 2021-03-05T17:15:29.122500Z

It's all described in the docstring for reg-sub, with examples.

Clément Ronzon 2021-03-05T17:20:43.122700Z

Would it be something similar to this?

2nd variation and explicitly suppling a `signal-fn` which returns `app-db`:

      #!clj
      (reg-sub
        :query-id
        (fn [_ _]  re-frame/app-db)   ;; <--- explicit signal-fn
        a-computation-fn)             ;; has signature:  (fn [db query-vec]  ... ret-value)

p-himik 2021-03-05T17:26:06.122900Z

Yes.

Clément Ronzon 2021-03-05T17:40:51.123100Z

I tried this:

(reg-sub ::bar
         (fn [_ [_ y]] y)
         (fn [foo [_ y]] (str foo y)))
But I get this error:
re-frame: in the reg-sub for :spui.layouts.subs/bar , the input-signals function returns: null

p-himik 2021-03-05T17:44:39.123300Z

The signal sub accepts the sub vector (there's an arity for a dynamic vector as well, you can ignore that). I.e. remove the first _ from the signal function.

Clément Ronzon 2021-03-05T17:46:18.123500Z

(reg-sub ::bar
         (fn [[_ y]] y)
         (fn [foo [_ y]] (str foo y)))
@(subscribe [::layout-subs/bar "Y"])
re-frame: in the reg-sub for :spui.layouts.subs/bar , the input-signals function returns: Y

p-himik 2021-03-05T17:47:07.123800Z

Wait, why do you return y? The signal function is supposed to be returning a subscription or a map/vector of subs.

p-himik 2021-03-05T17:47:34.124Z

Please read the documentation - it's all there, and it's under 150 lines long.

Clément Ronzon 2021-03-05T18:00:06.124400Z

oh ok, my bad, found it:

(fn [query-vec dynamic-vec]
         [(subscribe [:a-sub])
          (subscribe [:b-sub])])
This worked:
(reg-sub ::foo
         (fn [_ [_ x]] x))

(reg-sub ::bar
         (fn [[_ y]] (subscribe [::foo y]))
         (fn [foo [_ y]] (str foo y)))

Clément Ronzon 2021-03-05T18:00:06.124600Z

Thank you @p-himik !

1👍