Fork me on GitHub
#re-frame
<
2021-12-17
>
frankitox23:12:36

So I just found out my re-frame subscriptions are quite expensive, so I thought the best way to fix it would be to use Layer 2 and layer 3 subscriptions. An example of my current code non-optimized code:

(reg-sub
  :my-sub
  (fn [{:keys [one two three] :as app-db} _]
    (expensive-stuff one two three)))

frankitox23:12:11

For this I need 3 Layer2 subscriptions, one for each attribute. What I don't like is having to define names for each sub, so I ended up doing this

(reg-sub
  :get-db-key
  (fn [app-db [_ k]]
    (get app-db k)))

(reg-sub
  :my-sub
  :<- [:get-db-key :one]
  :<- [:get-db-key :two]
  :<- [:get-db-key :three]
  (fn [[one two three] _]
    (expensive-stuff one two three)))
Is this ok? This could help 'anonymize' the 3 subscriptions I needed. Is there any way to define anonymous subscriptions that I could use in my Layer 3 subs?

p-himik10:12:22

I'd say it's alright as long as you don't use :get-db-key in view functions, only in other subs. Personally, I would even name it ::-get-db-key to indicate that it's "private". Also, to avoid multiple signals, you can write something like :get-db-keys that returns a map of multiple keys at once. Depends on whether it's a useful pattern in other places.

frankitox14:12:28

I like the private idea! as I'll probably forget that :get-db-key is only meant as a layer 2 sub :thinking_face:

frankitox14:12:39

thanks for the suggestions!

👍 1