Skip to content

Commit 334ad57

Browse files
Fix issue where async getters did not work with watch option
1 parent c791470 commit 334ad57

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

.babelrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"plugins": [
33
"@babel/plugin-transform-runtime",
4-
["@babel/plugin-proposal-decorators", { "legacy": true }]
4+
["@babel/plugin-proposal-decorators", { "legacy": true }],
5+
["@babel/plugin-proposal-class-properties"]
56
]
67
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function AsyncComputed<TResult>(
1414
method = options.methods[key]
1515
delete options.methods[key];
1616
} else if (options.computed?.[key]) {
17-
method = options.computed[key]
17+
method = (options.computed[key] as IAsyncComputedValue<TResult>).get
1818
delete options.computed[key];
1919
} else {
2020
throw new Error(`AsyncComputed ${key} is not a method or computed property`);

test/index.js

+51-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ test('The AsyncComputed decorator defines async computed properties', t => {
1212
@Component()
1313
class TestComponent extends Vue {
1414
@AsyncComputed()
15-
async shouldBeComputed () {
15+
async shouldBeComputed() {
1616
return 1
1717
}
1818
}
19+
1920
const vm = new TestComponent()
2021

2122
t.equal(vm.shouldBeComputed, null)
@@ -32,11 +33,12 @@ test('The AsyncComputed decorator allows options on async computed properties',
3233

3334
@Component()
3435
class TestComponent extends Vue {
35-
@AsyncComputed({ lazy: true })
36-
async shouldBeComputed () {
36+
@AsyncComputed({lazy: true})
37+
async shouldBeComputed() {
3738
return 1
3839
}
3940
}
41+
4042
const vm = new TestComponent()
4143

4244
t.equal(vm.shouldBeComputed, null)
@@ -56,12 +58,13 @@ test('The AsyncComputed decorator can be applied to getter computed properties',
5658
@Component()
5759
class TestComponent extends Vue {
5860
@AsyncComputed({default: 0})
59-
get shouldBeComputed () {
61+
get shouldBeComputed() {
6062
return new Promise(resolve => {
6163
resolve(1)
6264
})
6365
}
6466
}
67+
6568
const vm = new TestComponent()
6669

6770
t.equal(vm.shouldBeComputed, 0)
@@ -72,3 +75,47 @@ test('The AsyncComputed decorator can be applied to getter computed properties',
7275
t.equal(vm.shouldBeComputed, 1)
7376
})
7477
})
78+
79+
80+
test('The AsyncComputed decorator works on getters with watch option', t => {
81+
// t.plan(7)
82+
83+
@Component()
84+
class TestComponent extends Vue {
85+
watchValue = 'initial';
86+
computationCount = 0
87+
88+
@AsyncComputed({default: 'default', watch: ['watchValue'], lazy: false})
89+
get asyncGetter() {
90+
this.computationCount++;
91+
return new Promise(resolve => {
92+
resolve(this.watchValue)
93+
})
94+
}
95+
}
96+
97+
const vm = new TestComponent()
98+
99+
t.equal(vm.watchValue, 'initial', 'initial watch value')
100+
t.equal(vm.computationCount, 1, 'getter calculation is triggered immediately')
101+
t.equal(vm.asyncGetter, 'default', 'initial asyncGetter value')
102+
t.equal(vm.computationCount, 1, 'getter is cached during this tick')
103+
104+
Vue.nextTick(() => {
105+
t.equal(vm.asyncGetter, 'initial', 'getter is updated on next tick')
106+
t.equal(vm.computationCount, 1, 'getter is not recalculated')
107+
108+
vm.watchValue = 'updated' // reactive set
109+
t.equals(vm.asyncGetter, 'initial', 'getter returns cached value after watched value changes') // reactive get
110+
t.equal(vm.computationCount, 1, 'no immediate recalculation after watched value changes')
111+
112+
Vue.nextTick(() => {
113+
t.equal(vm.computationCount, 2, 'a recalculation is triggered on tick after watched value changes')
114+
Vue.nextTick(() => {
115+
t.equal(vm.asyncGetter, 'updated', 'getter value is updated in the ticket after recalculation was triggered')
116+
t.equal(vm.computationCount, 2, 'there were 2 calculations in total')
117+
})
118+
})
119+
})
120+
})
121+

0 commit comments

Comments
 (0)