@@ -91,15 +91,7 @@ export default {
91
91
if ( data ) {
92
92
for ( let key in data ) {
93
93
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 ] )
103
95
}
104
96
}
105
97
}
@@ -228,50 +220,51 @@ export default {
228
220
} ,
229
221
230
222
$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.` )
238
227
}
239
228
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.` )
242
231
}
243
232
244
- setData ( null )
233
+ Object . defineProperty ( this , key , {
234
+ get : ( ) => this . $data . $meteor . data [ key ] ,
235
+ enumerable : true ,
236
+ configurable : true ,
237
+ } )
245
238
246
239
// Function run
247
- const run = ( params ) => {
248
- let result = func ( params )
240
+ const setResult = result => {
249
241
if ( result && typeof result . fetch === 'function' ) {
250
242
result = result . fetch ( )
251
243
}
252
244
if ( Vue . config . meteor . freeze ) {
253
245
result = Object . freeze ( result )
254
246
}
255
- setData ( result )
247
+ set ( this . $data . $meteor . data , key , result )
256
248
}
257
249
250
+ // Vue autorun
251
+ const unwatch = this . $watch ( func , noop )
252
+ const watcher = this . _watchers . find ( w => w . getter === func )
253
+
258
254
// 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
+ } )
260
260
const unautorun = ( ) => {
261
261
if ( computation ) this . $stopHandle ( computation )
262
262
}
263
- const autorun = ( ) => {
264
- unautorun ( )
265
- computation = this . $autorun ( ( ) => {
266
- run ( )
267
- } )
263
+ // Update from Vue (override)
264
+ watcher . update = ( ) => {
265
+ computation . invalidate ( )
268
266
}
269
267
270
- // Vue autorun
271
- const unwatch = this . $watch ( autorun , noop , {
272
- immediate : true ,
273
- } )
274
-
275
268
return ( ) => {
276
269
unwatch ( )
277
270
unautorun ( )
0 commit comments