]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): callback not called when using `flush:sync` (#1633)
authorCarlos Rodrigues <david-181@hotmail.com>
Sun, 19 Jul 2020 17:30:24 +0000 (18:30 +0100)
committerGitHub <noreply@github.com>
Sun, 19 Jul 2020 17:30:24 +0000 (13:30 -0400)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 3deb2122fc0c43e791c6407deb290aa448f020d7..07b985c3c08c6ca2f79404c617a1e42dfa7e662b 100644 (file)
@@ -611,4 +611,23 @@ describe('api: watch', () => {
       oldValue: 2
     })
   })
+
+  it('should work sync', () => {
+    const v = ref(1)
+    let calls = 0
+
+    watch(
+      v,
+      () => {
+        ++calls
+      },
+      {
+        flush: 'sync'
+      }
+    )
+
+    expect(calls).toBe(0)
+    v.value++
+    expect(calls).toBe(1)
+  })
 })
index f3a584312147ffe9c4d82012486b1c7b61bd7134..fcc8c3203089cbbbfd157ad58f3c63b89d753499 100644 (file)
@@ -71,8 +71,6 @@ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
 
 export type WatchStopHandle = () => void
 
-const invoke = (fn: Function) => fn()
-
 // Simple effect.
 export function watchEffect(
   effect: WatchEffect,
@@ -262,7 +260,7 @@ function doWatch(
 
   let scheduler: (job: () => any) => void
   if (flush === 'sync') {
-    scheduler = invoke
+    scheduler = job
   } else if (flush === 'pre') {
     // ensure it's queued before component updates (which have positive ids)
     job.id = -1