zodern / melte

Svelte compiler for Meteor with built in tracker integration and HMR
MIT License
33 stars 14 forks source link

Subscriptions inside tracker statements get duplicated #24

Open JoaoCardoso193 opened 1 year ago

JoaoCardoso193 commented 1 year ago

Hey, I seem to be having an issue or potentially misunderstanding tracker statements.

I see that in the Readme file, there is a Meteor.subscribe call inside a tracker statement $m. If I do that in my meteor app, my subscriptions get multiplied. If I use Tracker.autorun instead, they don't.

Using "$m" for the "mainObjects-sources" subscription: image image

Using Tracker.autorun: image image

Could you please clarify what exactly is the difference between using $m and Tracker.autorun? And why should we put "Meteor.subscribe" calls inside tracker statements if it leads to the subscriptions being multiplied?

Thanks!

zodern commented 1 year ago

Thanks for reporting this. Subscriptions should work the same with both $m and Tracker.autorun, with $m automatically ending the subscriptions when the component instance is destroyed. I will see if I can reproduce the subscriptions being duplicated. In your code, where is mainObjectId coming from, and when can it change?

JoaoCardoso193 commented 1 year ago

Hi, thanks for your quick response!

mainObjectId is the id of the currently displayed item on this page. It is fed to the page through the URL: image image

It never changes.

tosinek commented 1 year ago

Do you have rdb:svelte-meteor-data installed? I just came across a similar thing, that the sub was called twice (two different ids). Removing svelte-meteor-data fixed it.

With the zodern's suggested implementation (while having svelte-meteor-data installed) it was even causing a memory leak (when the page was embedded on another page), creating thousands of subs (thanks MontiAPM for pointing me in the right direction) image