Skip to content

Commit 428587c

Browse files
author
Guillaume Chau
committed
refactor: invalidate tracker computation
1 parent af3994d commit 428587c

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/index.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,7 @@ export default {
9191
if (data) {
9292
for (let key in data) {
9393
if (key.charAt(0) !== '$') {
94-
const options = data[key]
95-
let func
96-
if (typeof options === 'function') {
97-
func = options.bind(this)
98-
} else {
99-
throw Error(`Meteor data '${key}': You must provide a function which returns the result.`)
100-
}
101-
102-
this.$addMeteorData(key, func)
94+
this.$addMeteorData(key, data[key])
10395
}
10496
}
10597
}
@@ -228,50 +220,51 @@ export default {
228220
},
229221

230222
$addMeteorData (key, func) {
231-
const hasDataField = hasProperty(this.$data, key)
232-
if (!hasDataField && !hasProperty(this, key) && !hasProperty(this.$props, key)) {
233-
Object.defineProperty(this, key, {
234-
get: () => this.$data.$meteor.data[key],
235-
enumerable: true,
236-
configurable: true,
237-
})
223+
if (typeof func === 'function') {
224+
func = func.bind(this)
225+
} else {
226+
throw Error(`Meteor data '${key}': You must provide a function which returns the result.`)
238227
}
239228

240-
const setData = value => {
241-
set(hasDataField ? this.$data : this.$data.$meteor.data, key, value)
229+
if (hasProperty(this.$data, key) || hasProperty(this.$props, key) || hasProperty(this, key)) {
230+
throw Error(`Meteor data '${key}': Property already used in the component data, props or other.`)
242231
}
243232

244-
setData(null)
233+
Object.defineProperty(this, key, {
234+
get: () => this.$data.$meteor.data[key],
235+
enumerable: true,
236+
configurable: true,
237+
})
245238

246239
// Function run
247-
const run = (params) => {
248-
let result = func(params)
240+
const setResult = result => {
249241
if (result && typeof result.fetch === 'function') {
250242
result = result.fetch()
251243
}
252244
if (Vue.config.meteor.freeze) {
253245
result = Object.freeze(result)
254246
}
255-
setData(result)
247+
set(this.$data.$meteor.data, key, result)
256248
}
257249

250+
// Vue autorun
251+
const unwatch = this.$watch(func, noop)
252+
const watcher = this._watchers.find(w => w.getter === func)
253+
258254
// Meteor autorun
259-
let computation
255+
let computation = this.$autorun(() => {
256+
// Vue watcher deps are also-rebuilt
257+
const result = watcher.get()
258+
setResult(result)
259+
})
260260
const unautorun = () => {
261261
if (computation) this.$stopHandle(computation)
262262
}
263-
const autorun = () => {
264-
unautorun()
265-
computation = this.$autorun(() => {
266-
run()
267-
})
263+
// Update from Vue (override)
264+
watcher.update = () => {
265+
computation.invalidate()
268266
}
269267

270-
// Vue autorun
271-
const unwatch = this.$watch(autorun, noop, {
272-
immediate: true,
273-
})
274-
275268
return () => {
276269
unwatch()
277270
unautorun()

0 commit comments

Comments
 (0)