I don't want to unsubscribe when using Tracker.autorun

Sorry, my English is very poor!!!

let data = new ReactiveVar({})
Tracker.autorun(() => {
Meteor.subscribe(‘item’, data.get())
})

When I use data.set(),the recalculation stops the subscription and resubscribes.

I don’t want to unsubscribe when using Tracker.autorun

You are mixing up some things. The subscription should not be in the autorun. Here’s what its doing:

It subscribes at first.
Then the reactive var is triggered inmediatly
The autorun is then triggered again somehow (likely because of the reactive var being set somewhere
The subscribe is triggered again which will cause it to unsubscribe and then resubscribe again.

I want to control subscription content in a responsive manner, but I don’t delete data previously subscribed

I think you are mixing up subscriptions and collections. Each subscription connects to a publication on the server. New data is being pushed into a minimongo collection on the client. Multiple subscriptions can fill a collection. For example:

Meteor.subscribe('topArticles'); // top 10 articles

Will fill the collection with 10 articles. Subscribing to another publication like below will simply add some extra:

Meteor.subscribe('featuredArticles'); // adds another 5 articles

This will total the amount to 15 articles. Simply calling a subscription again with different parameters will unsubscribe it automatically before subscribing again.

In your helpers you can use minimongo to fetch reactive data. While keeping subscriptions open:

Meteor.subscribe('item');

Tracker.autorun(() => {
  const item = Items.findOne();
});
2 Likes

Actually, it can be - and should be if a reactive parameter is required in the subscription. The behaviour described by @steinsgatehz is exactly what I would expect given the code sample.

However, I also think your understanding and solution to the issue faced by @steinsgatehz is correct. The behaviour of multiple subscriptions to the same collection is sometimes not obvious, as most of the documentation covers this in a somewhat ad hoc and anecdotal manner.

3 Likes

I understand multiple subscriptions to the same collection,Thank you for your reply. I have solved the problem in another way

Old but good: https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/

2 Likes

Nice :slight_smile: . I’d forgotten about that :roll_eyes: