fix(watch): update `oldValue` before running `cb` to prevent stale value (#12296)

close #12294
This commit is contained in:
Tycho 2025-05-20 08:44:13 +08:00 committed by GitHub
parent 1a664749d4
commit c69c4bb59c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -277,4 +277,16 @@ describe('watch', () => {
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)
})
})

View File

@ -264,11 +264,11 @@ export function watch(
: oldValue,
boundCleanup,
]
oldValue = newValue
call
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
: // @ts-expect-error
cb!(...args)
oldValue = newValue
} finally {
activeWatcher = currentWatcher
}