Skip to content

Commit 8facaef

Browse files
authored
fix(watch): callback not called when using flush:sync (#1633)
1 parent 4655d69 commit 8facaef

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,23 @@ describe('api: watch', () => {
611611
oldValue: 2
612612
})
613613
})
614+
615+
it('should work sync', () => {
616+
const v = ref(1)
617+
let calls = 0
618+
619+
watch(
620+
v,
621+
() => {
622+
++calls
623+
},
624+
{
625+
flush: 'sync'
626+
}
627+
)
628+
629+
expect(calls).toBe(0)
630+
v.value++
631+
expect(calls).toBe(1)
632+
})
614633
})

packages/runtime-core/src/apiWatch.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
7171

7272
export type WatchStopHandle = () => void
7373

74-
const invoke = (fn: Function) => fn()
75-
7674
// Simple effect.
7775
export function watchEffect(
7876
effect: WatchEffect,
@@ -262,7 +260,7 @@ function doWatch(
262260

263261
let scheduler: (job: () => any) => void
264262
if (flush === 'sync') {
265-
scheduler = invoke
263+
scheduler = job
266264
} else if (flush === 'pre') {
267265
// ensure it's queued before component updates (which have positive ids)
268266
job.id = -1

0 commit comments

Comments
 (0)