From 6c9597413e8ca389da2bfca59b2dda418cb3829e Mon Sep 17 00:00:00 2001 From: Michael Zangl Date: Sat, 6 Jan 2018 16:41:14 +0100 Subject: [PATCH] Add a ... property for each computed property. --- src/index.js | 5 +++++ test/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/index.js b/src/index.js index 690bdc2..4a43a09 100644 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,7 @@ const AsyncComputed = { } else { data[key] = null } + data[key + '$updating'] = false } return data } @@ -58,8 +59,10 @@ 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) @@ -67,9 +70,11 @@ const AsyncComputed = { 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 diff --git a/test/index.js b/test/index.js index b69c53f..8bc94ff 100644 --- a/test/index.js +++ b/test/index.js @@ -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) + }) + } + }) +})