Skip to content

Commit

Permalink
fix(watch): once option should be ignored by watchEffect (#11884)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingshan authored Sep 16, 2024
1 parent 2d6adf7 commit 49fa673
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
18 changes: 5 additions & 13 deletions packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 49fa673

Please sign in to comment.