fix(watch): `once` option should be ignored by watchEffect (#11884)

This commit is contained in:
Yang Mingshan 2024-09-16 10:56:32 +08:00 committed by GitHub
parent 2d6adf78a0
commit 49fa673493
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 13 deletions

View File

@ -193,4 +193,20 @@ describe('watch', () => {
scope.stop()
expect(calls).toEqual(['sync 2', 'post 2'])
})
test('once option should be ignored by simple watch', async () => {
let dummy: any
const source = ref(0)
watch(
() => {
dummy = source.value
},
null,
{ once: true },
)
expect(dummy).toBe(0)
source.value++
expect(dummy).toBe(1)
})
})

View File

@ -218,19 +218,11 @@ export function watch(
}
}
if (once) {
if (cb) {
const _cb = cb
cb = (...args) => {
_cb(...args)
watchHandle()
}
} else {
const _getter = getter
getter = () => {
_getter()
watchHandle()
}
if (once && cb) {
const _cb = cb
cb = (...args) => {
_cb(...args)
watchHandle()
}
}