Skip to content

Commit 094cd49

Browse files
Merge pull request #58 from mtorromeo/fix-reactive-state
Fixed reactivity of the $asyncComputed properties
2 parents 949349e + 1e91959 commit 094cd49

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

Diff for: src/index.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ const AsyncComputed = {
1919
.asyncComputed = Vue.config.optionMergeStrategies.computed
2020

2121
Vue.mixin({
22+
data () {
23+
return {
24+
_asyncComputed: {},
25+
}
26+
},
2227
beforeCreate () {
2328
const optionData = this.$options.data
2429
const asyncComputed = this.$options.asyncComputed || {}
25-
this.$asyncComputed = {}
26-
27-
if (!Object.keys(asyncComputed).length) return
2830

2931
if (!this.$options.computed) this.$options.computed = {}
3032

33+
this.$options.computed.$asyncComputed = () => this.$data._asyncComputed
34+
35+
if (!Object.keys(asyncComputed).length) return
36+
3137
for (const key in asyncComputed) {
3238
const getter = getterFn(key, this.$options.asyncComputed[key])
3339
this.$options.computed[prefix + key] = getter
@@ -74,17 +80,17 @@ const AsyncComputed = {
7480
if (!newPromise || !newPromise.then) {
7581
newPromise = Promise.resolve(newPromise)
7682
}
77-
setAsyncState(this.$asyncComputed[key], 'updating')
83+
setAsyncState(this, key, 'updating')
7884

7985
newPromise.then(value => {
8086
if (thisPromise !== promiseId) return
81-
setAsyncState(this.$asyncComputed[key], 'success')
87+
setAsyncState(this, key, 'success')
8288
this[key] = value
8389
}).catch(err => {
8490
if (thisPromise !== promiseId) return
8591

86-
setAsyncState(this.$asyncComputed[key], 'error')
87-
this.$asyncComputed[key].exception = err
92+
setAsyncState(this, key, 'error')
93+
Vue.set(this.$data._asyncComputed[key], 'exception', err)
8894
if (pluginOptions.errorHandler === false) return
8995

9096
const handler = (pluginOptions.errorHandler === undefined)
@@ -98,25 +104,25 @@ const AsyncComputed = {
98104
}
99105
})
100106
}
101-
this.$asyncComputed[key] = {
107+
Vue.set(this.$data._asyncComputed, key, {
102108
exception: null,
103109
update: () => {
104110
watcher(getterOnly(this.$options.asyncComputed[key]).apply(this))
105111
}
106-
}
107-
setAsyncState(this.$asyncComputed[key], 'updating')
112+
})
113+
setAsyncState(this, key, 'updating')
108114
this.$watch(prefix + key, watcher, { immediate: true })
109115
}
110116
}
111117
})
112118
}
113119
}
114120

115-
function setAsyncState (stateObject, state) {
116-
stateObject.state = state
117-
stateObject.updating = state === 'updating'
118-
stateObject.error = state === 'error'
119-
stateObject.success = state === 'success'
121+
function setAsyncState (vm, stateObject, state) {
122+
vm.$set(vm.$data._asyncComputed[stateObject], 'state', state)
123+
vm.$set(vm.$data._asyncComputed[stateObject], 'updating', state === 'updating')
124+
vm.$set(vm.$data._asyncComputed[stateObject], 'error', state === 'error')
125+
vm.$set(vm.$data._asyncComputed[stateObject], 'success', state === 'success')
120126
}
121127

122128
function getterOnly (fn) {

0 commit comments

Comments
 (0)