Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const AsyncComputed = {
} else {
data[key] = null
}
data[key + '$updating'] = false
}
return data
}
Expand All @@ -58,18 +59,22 @@ const AsyncComputed = {

for (const key in this.$options.asyncComputed || {}) {
let promiseId = 0
const updatingKey = key + '$updating'
this.$watch(prefix + key, newPromise => {
const thisPromise = ++promiseId
this[updatingKey] = true

if (!newPromise || !newPromise.then) {
newPromise = Promise.resolve(newPromise)
}

newPromise.then(value => {
if (thisPromise !== promiseId) return
this[updatingKey] = false
this[key] = value
}).catch(err => {
if (thisPromise !== promiseId) return
this[updatingKey] = false

if (pluginOptions.errorHandler === false) return

Expand Down
39 changes: 39 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,42 @@ test("Async computed values can be calculated lazily with a default", t => {
})
})
})

test("Updating flag is set while porperty is re-computed", t => {
t.plan(10)
const vm = new Vue({
asyncComputed: {
a () {
return new Promise(resolve => {
setTimeout(() => resolve('done'), 10)
})
},
b () {
const data = this.c
return new Promise(resolve => {
setTimeout(() => resolve(data), 20)
})
}
},
data: () => ({
c: false
})
})
t.equal(vm.a$updating, true)
t.equal(vm.b$updating, true)
vm.$watch('a', function (val) {
t.equal(val, 'done')
t.equal(vm.a$updating, false)
t.equal(vm.b$updating, true)
})
vm.$watch('b', function (val) {
t.equal(val, vm.c)
t.equal(vm.b$updating, false)
if (!vm.c) {
vm.c = true
Vue.nextTick(() => {
t.equal(vm.b$updating, true)
})
}
})
})