Fork me on GitHub
#re-frame
<
2021-03-05
>
Clément Ronzon17:03:51

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-himik17:03:29

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

Clément Ronzon17:03:43

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)

Clément Ronzon17:03:51

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-himik17:03:39

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 Ronzon17:03:18

(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-himik17:03:07

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

p-himik17:03:34

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

Clément Ronzon18:03:06

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)))