Skip to content

Commit 5725a58

Browse files
authored
fix(computedWithControl): allow optional oldValue parameter in computedWithControl getter (#4132)
1 parent 965bf05 commit 5725a58

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/shared/computedWithControl/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ describe('computedWithControl', () => {
2525
expect(computed.value).toBe('BAR')
2626
})
2727

28+
it.runIf(isVue3)('optional old value', () => {
29+
const trigger = ref(0)
30+
31+
const computed = computedWithControl(trigger, (oldValue?: number) =>
32+
oldValue ? oldValue * 2 : 1)
33+
34+
expect(computed.value).toBe(1)
35+
36+
trigger.value += 1
37+
38+
expect(computed.value).toBe(2)
39+
40+
trigger.value -= 1
41+
42+
expect(computed.value).toBe(4)
43+
})
44+
2845
it.runIf(isVue3)('custom trigger', () => {
2946
let count = 0
3047
const computed = computedWithControl(() => {}, () => count)

packages/shared/computedWithControl/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function computedWithControl<T, S>(
5454
return {
5555
get() {
5656
if (dirty.value) {
57-
v = get()
57+
v = get(v)
5858
dirty.value = false
5959
}
6060
track()

0 commit comments

Comments
 (0)