]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): `once` option should be ignored by watchEffect (#11884)
authorYang Mingshan <y.mingshan3@gmail.com>
Mon, 16 Sep 2024 02:56:32 +0000 (10:56 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Sep 2024 02:56:32 +0000 (10:56 +0800)
packages/reactivity/__tests__/watch.spec.ts
packages/reactivity/src/watch.ts

index c8d48543fb17ce62d559efdbf59e7db6eb12efb2..b3d18e19f71205181594de041ca40458ccd78f66 100644 (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)
+  })
 })
index ceda454a568dfd3d65a9be9b7370059bd9b15497..073bf88b93f021491177818956f910ee6759d7e0 100644 (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()
     }
   }