Prevent helper to run when a non-related parameter of reactive document updates

Let me try to explain my problem.

I have resource heavy helper which I should run on page as rare as possible.
The helper contains a reactive variable: Meteor.user().profile.gender
When I update denormalized field Meteor.user().statsCache.hits, not touching Meteor.user().profile.gender my helper reactively updates without any need, lagging UI. I understand it is because subscription updates and sends a new User document to client.

But how can I prevent helper to rerun if its particular reactive document parameter doesn’t change?

I thought about somehow splitting it to 2 different subscriptions, but I’m not sure if this helps as server may still push change?

So any solutions would be appreciated.

Meteor.user() is actually a findOne on the Meteor.users-collection, so if you directly use this collection, you can control the included fields in the result:

Meteor.users.findOne(Meteor.userId(), {fields: {profile: true}})

This query will only invalidate when profile changes. (Note that you cannot further control reactivity for nested fields like profile.gender as far as i remember, so that you could distinguish e.g. between profile.gender and profile.fullname).

1 Like

You should be able to nest further as this also works in publications.

So you should have something like this:
Meteor.users.findOne({ _id: Meteor.userId() }, {fields: { 'profile.gender': 1 }});

1 Like

Thank you guys, it seems it helped. I’ve isolated profile and .statsCache … It was actually in same .profile.* array