mirror of https://github.com/vuejs/core.git
fix(watch): update `oldValue` before running `cb` to prevent stale value (#12296)
close #12294
This commit is contained in:
parent
1a664749d4
commit
c69c4bb59c
|
@ -277,4 +277,16 @@ describe('watch', () => {
|
||||||
|
|
||||||
expect(dummy).toEqual([1, 2, 3])
|
expect(dummy).toEqual([1, 2, 3])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('watch with immediate reset and sync flush', () => {
|
||||||
|
const value = ref(false)
|
||||||
|
|
||||||
|
watch(value, () => {
|
||||||
|
value.value = false
|
||||||
|
})
|
||||||
|
|
||||||
|
value.value = true
|
||||||
|
value.value = true
|
||||||
|
expect(value.value).toBe(false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -264,11 +264,11 @@ export function watch(
|
||||||
: oldValue,
|
: oldValue,
|
||||||
boundCleanup,
|
boundCleanup,
|
||||||
]
|
]
|
||||||
|
oldValue = newValue
|
||||||
call
|
call
|
||||||
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
|
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
|
||||||
: // @ts-expect-error
|
: // @ts-expect-error
|
||||||
cb!(...args)
|
cb!(...args)
|
||||||
oldValue = newValue
|
|
||||||
} finally {
|
} finally {
|
||||||
activeWatcher = currentWatcher
|
activeWatcher = currentWatcher
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue