Skip to content

Commit 49fa673

Browse files
authoredSep 16, 2024··
fix(watch): once option should be ignored by watchEffect (#11884)
1 parent 2d6adf7 commit 49fa673

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed
 

‎packages/reactivity/__tests__/watch.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,20 @@ describe('watch', () => {
193193
scope.stop()
194194
expect(calls).toEqual(['sync 2', 'post 2'])
195195
})
196+
197+
test('once option should be ignored by simple watch', async () => {
198+
let dummy: any
199+
const source = ref(0)
200+
watch(
201+
() => {
202+
dummy = source.value
203+
},
204+
null,
205+
{ once: true },
206+
)
207+
expect(dummy).toBe(0)
208+
209+
source.value++
210+
expect(dummy).toBe(1)
211+
})
196212
})

‎packages/reactivity/src/watch.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,11 @@ export function watch(
218218
}
219219
}
220220

221-
if (once) {
222-
if (cb) {
223-
const _cb = cb
224-
cb = (...args) => {
225-
_cb(...args)
226-
watchHandle()
227-
}
228-
} else {
229-
const _getter = getter
230-
getter = () => {
231-
_getter()
232-
watchHandle()
233-
}
221+
if (once && cb) {
222+
const _cb = cb
223+
cb = (...args) => {
224+
_cb(...args)
225+
watchHandle()
234226
}
235227
}
236228

0 commit comments

Comments
 (0)
Please sign in to comment.