wendellrocha / enhanced_meteorify

A dart package for interacting with Meteor
BSD 2-Clause "Simplified" License
3 stars 3 forks source link

Question: re-subscribe same name with different arguments #7

Closed joyjiyuan closed 2 years ago

joyjiyuan commented 2 years ago

Technically, what happens when one of these reactive sources changes is the following:

  1. The reactive data source invalidates the autorun computation (marks it so that it re-runs in the next Tracker flush cycle).
  2. The subscription detects this, and given that anything is possible in next computation run, marks itself for destruction.
  3. The computation re-runs, with .subscribe() being re-called either with the same or different arguments.
  4. If the subscription is run with the same arguments then the “new” subscription discovers the old “marked for destruction” subscription that’s sitting around, with the same data already ready, and reuses that.
  5. If the subscription is run with different arguments, then a new subscription is created, which connects to the publication on the server.
  6. At the end of the flush cycle (i.e. after the computation is done re-running), the old subscription checks to see if it was re-used, and if not, sends a message to the server to tell the server to shut it down.

Step 4 above is an important detail—that the system cleverly knows not to re-subscribe if the autorun re-runs and subscribes with the exact same arguments. This holds true even if the new subscription is set up somewhere else in the template hierarchy. For example, if a user navigates between two pages that both subscribe to the exact same subscription, the same mechanism will kick in and no unnecessary subscribing will happen.

Does the package implements this mechanism?

When I re-subscribe same name with different arguments which one is better performance? unsubscribe first or just resubscribe?

Thanks

wendellrocha commented 2 years ago

Unfortunately the package does not implement Tracker. I recommend doing the unsubscribe and resubscribe with the arguments updated so that the previous subscribe doesn't remain active/in memory.

joyjiyuan commented 2 years ago

Thank you for reply. I will follow your recommendation.