From 92e8155307554bc21191261072f133aeac9bb5a9 Mon Sep 17 00:00:00 2001 From: red-meadow <38740713+red-meadow@users.noreply.github.com> Date: Fri, 1 Nov 2019 22:51:03 +0200 Subject: [PATCH] Move watcher initialization inside Tracker.autorun --- src/index.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 825e7fa..5baad59 100644 --- a/src/index.js +++ b/src/index.js @@ -287,14 +287,24 @@ export default { set(this.$data.$meteor.data, key, result) } - // Vue autorun - const unwatch = this.$watch(func, noop) - const watcher = this._watchers.find(w => w.getter === func) + let unwatch, watcher // Meteor autorun - let computation = this.$autorun(() => { - // Vue watcher deps are also-rebuilt - const result = watcher.get() + const computation = this.$autorun((_computation) => { + let result + + if (_computation.firstRun) { + // Initialize Vue Watcher, func will be executed by this.$watch(). + // It is important to place it here so the Tracker computation can + // be initialized with the same function execution. + unwatch = this.$watch(func, noop) + watcher = this._watchers.find(w => w.getter === func) + result = watcher.value + } else { + result = watcher.get() + } + + // Vue watcher deps are also rebuilt setResult(result) }) const unautorun = () => {