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]]
...))
It's all described in the docstring for reg-sub
, with examples.
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)
Yes.
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
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.
(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
Wait, why do you return y
? The signal function is supposed to be returning a subscription or a map/vector of subs.
Please read the documentation - it's all there, and it's under 150 lines long.
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)))
Thank you @p-himik !