]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): update `oldValue` before running `cb` to prevent stale value (#12296)
authorTycho <jh.leong@outlook.com>
Tue, 20 May 2025 00:44:13 +0000 (08:44 +0800)
committerGitHub <noreply@github.com>
Tue, 20 May 2025 00:44:13 +0000 (08:44 +0800)
close #12294

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

index 245acfd63be844be594e56d4ea76875a19334827..9bec54e5f6859f070fb6bf35ea4b8739d092b7e9 100644 (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)
+  })
 })
index 659121ca34b9544a1b6879cc2fc6ded2cad8adf3..648e6481b18faaabd27d600f0f64b20bef0fb4e8 100644 (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
         }