@@ -27,49 +27,54 @@ const AsyncComputed = {
27
27
. optionMergeStrategies
28
28
. asyncComputed = Vue . config . optionMergeStrategies . computed
29
29
30
- Vue . mixin ( {
31
- data ( ) {
32
- return {
33
- _asyncComputed : { } ,
34
- }
35
- } ,
36
- computed : {
37
- $asyncComputed ( ) {
38
- return this . $data . _asyncComputed
39
- }
40
- } ,
41
- beforeCreate ( ) {
42
- const asyncComputed = this . $options . asyncComputed || { }
30
+ Vue . mixin ( getAsyncComputedMixin ( pluginOptions ) )
31
+ }
32
+ }
43
33
44
- if ( ! Object . keys ( asyncComputed ) . length ) return
34
+ function getAsyncComputedMixin ( pluginOptions ) {
35
+ return {
36
+ data ( ) {
37
+ return {
38
+ _asyncComputed : { } ,
39
+ }
40
+ } ,
41
+ computed : {
42
+ $asyncComputed ( ) {
43
+ return this . $data . _asyncComputed
44
+ }
45
+ } ,
46
+ beforeCreate ( ) {
47
+ const asyncComputed = this . $options . asyncComputed || { }
45
48
46
- for ( const key in asyncComputed ) {
47
- const getter = getterFn ( key , asyncComputed [ key ] )
48
- this . $options . computed [ prefix + key ] = getter
49
- }
49
+ if ( ! Object . keys ( asyncComputed ) . length ) return
50
50
51
- this . $options . data = initDataWithAsyncComputed ( this . $options , pluginOptions )
52
- } ,
53
- created ( ) {
54
- for ( const key in this . $options . asyncComputed || { } ) {
55
- const item = this . $options . asyncComputed [ key ] ,
56
- value = generateDefault . call ( this , item , pluginOptions )
57
- if ( isComputedLazy ( item ) ) {
58
- silentSetLazy ( this , key , value )
59
- } else {
60
- this [ key ] = value
61
- }
62
- }
51
+ for ( const key in asyncComputed ) {
52
+ const getter = getterFn ( key , asyncComputed [ key ] )
53
+ this . $options . computed [ prefix + key ] = getter
54
+ }
63
55
64
- for ( const key in this . $options . asyncComputed || { } ) {
65
- handleAsyncComputedPropetyChanges ( this , key , pluginOptions , Vue )
56
+ this . $options . data = initDataWithAsyncComputed ( this . $options , pluginOptions )
57
+ } ,
58
+ created ( ) {
59
+ for ( const key in this . $options . asyncComputed || { } ) {
60
+ const item = this . $options . asyncComputed [ key ] ,
61
+ value = generateDefault . call ( this , item , pluginOptions )
62
+ if ( isComputedLazy ( item ) ) {
63
+ silentSetLazy ( this , key , value )
64
+ } else {
65
+ this [ key ] = value
66
66
}
67
67
}
68
- } )
68
+
69
+ for ( const key in this . $options . asyncComputed || { } ) {
70
+ handleAsyncComputedPropetyChanges ( this , key , pluginOptions )
71
+ }
72
+ }
69
73
}
70
74
}
75
+ const AsyncComputedMixin = getAsyncComputedMixin ( )
71
76
72
- function handleAsyncComputedPropetyChanges ( vm , key , pluginOptions , Vue ) {
77
+ function handleAsyncComputedPropetyChanges ( vm , key , pluginOptions ) {
73
78
let promiseId = 0
74
79
const watcher = newPromise => {
75
80
const thisPromise = ++ promiseId
@@ -89,7 +94,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
89
94
if ( thisPromise !== promiseId ) return
90
95
91
96
setAsyncState ( vm , key , 'error' )
92
- Vue . set ( vm . $data . _asyncComputed [ key ] , 'exception' , err )
97
+ vm . $ set( vm . $data . _asyncComputed [ key ] , 'exception' , err )
93
98
if ( pluginOptions . errorHandler === false ) return
94
99
95
100
const handler = ( pluginOptions . errorHandler === undefined )
@@ -103,7 +108,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
103
108
}
104
109
} )
105
110
}
106
- Vue . set ( vm . $data . _asyncComputed , key , {
111
+ vm . $ set( vm . $data . _asyncComputed , key , {
107
112
exception : null ,
108
113
update : ( ) => {
109
114
if ( ! vm . _isDestroyed ) {
@@ -181,6 +186,7 @@ function generateDefault (fn, pluginOptions) {
181
186
}
182
187
183
188
export default AsyncComputed
189
+ export { AsyncComputed as AsyncComputedPlugin , AsyncComputedMixin }
184
190
185
191
/* istanbul ignore if */
186
192
if ( typeof window !== 'undefined' && window . Vue ) {
0 commit comments