Meteor `Deps.autorun` vs `Collection.observe`

前端 未结 1 787
半阙折子戏
半阙折子戏 2021-02-02 11:05

What are the pros/cons between using Deps.autorun or Collection.observe to keep a third-party widget in sync with a reactive Meteor.Collection

相关标签:
1条回答
  • 2021-02-02 11:31

    Deps.autorun, now Tracker.autorun is a reactive computation block. Whereas the observeChanges provides a callback to when something changes.

    When you use Deps.autorun, the entire block in function() {...}, will re-run every time a reactive variable, or document changes, in any way at all (that is updated, removed or inserted), or any other reactive variable change.

    The observeChanges callbacks are more fine tuned, and fire the callbacks for added, changed or removed depending on the query.

    Based on your code above, in effect both are the same. If you had more reactive variables in the Deps.autorun block then the observeChanges way of doing it would be more efficient.

    In general the first style is more efficient, but as your code stands above they're both nearly the same and it depends on your preference.

    0 讨论(0)
提交回复
热议问题