How to avoid Meteor Autorun with Meteor.user() inside runs each time Meteor.user changes

I am confused because I have an autorun in Meteor-Angular and I need to subcribe each time the variable this.getReactively(‘perPage’) changes, but before I need to verified if Meteor.user() exists, so I need to put it inside of the funtion autorun, but my problem is that this autorun runs each time not just when this.getReactively(‘perPage’) changes but when Meteor. user changes, I dont know if there is a way to avoid that, maybe compare previous value of Meteor.user, I dont know. Someone could help me? thanks


    Tracker.autorun(function() {
            if(this.getReactively('perPage')){
                if(Meteor.user() ){
                console.log(this.getReactively('perPage'));
                Meteor.subscribe('notifications_bell',Meteor.user().notifications_bell,this.getReactively('perPage'));
                }
            }
        }.bind(this));

Just an idea:

When you do a set on your getReactively could you include the user notifications_bell in the value? Then the autorun would not need to call Meteor.user().

I have no idea how your code is written or what getReactively is, but here is some pseudo code:

// where reactiveVar is set
reactiveVar.set({notifications_bell: Meteor.user().notifications_bell, perPageStuff: getPerPageStuff()});

...
// now autorun can look like this
Tracker.autorun(function() {
  let reactiveData = this.getReactively('per'Page);
  if(reactiveData.perPageStuff) {
    console.log(this.getReactively('perPage'));
    Meteor.subscribe('notifications_bell',reactiveData.notifications_bell, reactiveData.perPageStuff);
  }
}.bind(this));

Note: you might want to check into the reactive-dict package, maybe getReactively is a dictionary lookup?

Good luck!

Try using Meteor.userId() for the first condition - then it will only re-run if the currently logged in user changes. Meteor.userId() returns null when nobody is logged in, so your code will work as is.

For the second thing, you can use a Minimongo query with fields:

Meteor.users.find({
  _id: Meteor.userId(),
}, {
  fields: { notifications_bell: 1 }
});

This query should only re-run if the notifications_bell field changes.