From 8facaefcc3eff1ca1fa19832172495e4272979e5 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sun, 19 Jul 2020 18:30:24 +0100 Subject: [PATCH] fix(watch): callback not called when using `flush:sync` (#1633) --- .../runtime-core/__tests__/apiWatch.spec.ts | 19 +++++++++++++++++++ packages/runtime-core/src/apiWatch.ts | 4 +--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 3deb2122fc0..07b985c3c08 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -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) + }) }) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index f3a58431214..fcc8c320308 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -71,8 +71,6 @@ export interface WatchOptions 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