Fork me on GitHub
#re-frame
<
2023-03-14
>
tatut12:03:56

should this https://github.com/day8/re-frame/blob/master/docs/FAQs/UseASubscriptionInAnEventHandler.md#the-better-way still work? I tried the interceptor workaround, but still getting the warning from using subscribe outside reactive context (as per issue #740)

tatut12:03:41

or is there a way to get subscription value for a given app db value? there are some subscriptions chains that have logic I would rather not duplicate

tatut12:03:50

I made the following hack workaround to get the value of a sub:

(defn- get-subscription-value [db sub]
  (let [handler-fn (re-frame.registrar/get-handler :sub (first sub))]
    (handler-fn db sub)))

(defn subscribe-val
  "Get value of subscription for the given db."
  [db subv]
  (with-redefs [re-frame.subs/subscribe (partial get-subscription-value db)]
    @(rf/subscribe subv)))
you can pass a db value and a subscription vector to that and it returns the value (bypassing cached values)

p-himik13:03:41

That hack should still produce a warning if one of those subs uses a signal sub. And if it doesn't, it's not entirely correct because the cache wouldn't be purged. I think. I ended up writing my own wrappers or alternative variants for most of re-frame API that I use. Can definitely recommend - it gives great flexibility. E.g. for this particular case, I've introduced a dynamic variable that, when set, is just like subscribe but without caching.

tatut13:03:01

it doesn’t with the redef

p-himik13:03:33

Oh, hmm. Does that work properly with advanced optimizations as well?

tatut13:03:41

that remains to be seen

tatut13:03:58

ah yes, it seems re-frame.subs/subscribe should have ^:dynamic meta for it to work in advanced compilation

p-himik13:03:44

Right. I pretty much never use with-redefs so my memory is always fuzzy when it comes to it.

tatut13:03:13

I might need to make my own wrapper for subscribe and use that when I need this

p-himik13:03:34

Although, docstrings in both CLJ and CLJS don't mention :dynamic. How did you come to the conclusion that it's needed?

tatut13:03:40

or setting static-fns to false, but I don’t want a performance hit everywhere

p-himik13:03:52

Thanks! Yeah, makes sense.

Ryan17:03:49

Ok here’s a new one for me: I load a page that should display content, check the subscription and it has content post-page load, but the DOM does not show it, however if I hot reload any code, the subscription updates.

p-himik17:03:58

Same applies for a form-3 component.

Ryan17:03:20

@U2FRKM4TW a-HA, I just changed a form 1 to form 2!

Ryan17:03:27

That was precisely it

👍 2