From: Tycho Date: Tue, 20 May 2025 00:44:13 +0000 (+0800) Subject: fix(watch): update `oldValue` before running `cb` to prevent stale value (#12296) X-Git-Tag: v3.5.15~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c69c4bb59c114f2b5e03733b55ef9ace3087b5c3;p=thirdparty%2Fvuejs%2Fcore.git fix(watch): update `oldValue` before running `cb` to prevent stale value (#12296) close #12294 --- diff --git a/packages/reactivity/__tests__/watch.spec.ts b/packages/reactivity/__tests__/watch.spec.ts index 245acfd63b..9bec54e5f6 100644 --- a/packages/reactivity/__tests__/watch.spec.ts +++ b/packages/reactivity/__tests__/watch.spec.ts @@ -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) + }) }) diff --git a/packages/reactivity/src/watch.ts b/packages/reactivity/src/watch.ts index 659121ca34..648e6481b1 100644 --- a/packages/reactivity/src/watch.ts +++ b/packages/reactivity/src/watch.ts @@ -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 }