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