@@ -21,11 +21,16 @@ const AsyncComputed = {
2121 Vue . mixin ( {
2222 beforeCreate ( ) {
2323 const optionData = this . $options . data
24+ const asyncComputed = this . $options . asyncComputed || { }
25+ this . $asyncComputed = { }
26+
27+ if ( ! Object . keys ( asyncComputed ) . length ) return
2428
2529 if ( ! this . $options . computed ) this . $options . computed = { }
2630
27- for ( const key in this . $options . asyncComputed || { } ) {
28- this . $options . computed [ prefix + key ] = getterFn ( key , this . $options . asyncComputed [ key ] )
31+ for ( const key in asyncComputed ) {
32+ const getter = getterFn ( key , this . $options . asyncComputed [ key ] )
33+ this . $options . computed [ prefix + key ] = getter
2934 }
3035
3136 this . $options . data = function vueAsyncComputedInjectedDataFn ( ) {
@@ -34,7 +39,7 @@ const AsyncComputed = {
3439 ? optionData . call ( this )
3540 : optionData
3641 ) || { }
37- for ( const key in this . $options . asyncComputed || { } ) {
42+ for ( const key in asyncComputed ) {
3843 const item = this . $options . asyncComputed [ key ]
3944 if ( isComputedLazy ( item ) ) {
4045 initLazy ( data , key )
@@ -59,7 +64,7 @@ const AsyncComputed = {
5964
6065 for ( const key in this . $options . asyncComputed || { } ) {
6166 let promiseId = 0
62- this . $watch ( prefix + key , newPromise => {
67+ const watcher = newPromise => {
6368 const thisPromise = ++ promiseId
6469
6570 if ( newPromise === DidNotUpdate ) {
@@ -69,13 +74,17 @@ const AsyncComputed = {
6974 if ( ! newPromise || ! newPromise . then ) {
7075 newPromise = Promise . resolve ( newPromise )
7176 }
77+ setAsyncState ( this . $asyncComputed [ key ] , 'updating' )
7278
7379 newPromise . then ( value => {
7480 if ( thisPromise !== promiseId ) return
81+ setAsyncState ( this . $asyncComputed [ key ] , 'success' )
7582 this [ key ] = value
7683 } ) . catch ( err => {
7784 if ( thisPromise !== promiseId ) return
7885
86+ setAsyncState ( this . $asyncComputed [ key ] , 'error' )
87+ this . $asyncComputed [ key ] . exception = err
7988 if ( pluginOptions . errorHandler === false ) return
8089
8190 const handler = ( pluginOptions . errorHandler === undefined )
@@ -88,13 +97,34 @@ const AsyncComputed = {
8897 handler ( err . stack )
8998 }
9099 } )
91- } , { immediate : true } )
100+ }
101+ this . $asyncComputed [ key ] = {
102+ exception : null ,
103+ update : ( ) => {
104+ watcher ( getterOnly ( this . $options . asyncComputed [ key ] ) ( ) )
105+ }
106+ }
107+ setAsyncState ( this . $asyncComputed [ key ] , 'updating' )
108+ this . $watch ( prefix + key , watcher , { immediate : true } )
92109 }
93110 }
94111 } )
95112 }
96113}
97114
115+ function setAsyncState ( stateObject , state ) {
116+ stateObject . state = state
117+ stateObject . updating = state === 'updating'
118+ stateObject . error = state === 'error'
119+ stateObject . success = state === 'success'
120+ }
121+
122+ function getterOnly ( fn ) {
123+ if ( typeof fn === 'function' ) return fn
124+
125+ return fn . get
126+ }
127+
98128function getterFn ( key , fn ) {
99129 if ( typeof fn === 'function' ) return fn
100130
0 commit comments