Skip to content

Commit

Permalink
fix(watch): callback not called when using flush:sync (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax authored Jul 19, 2020
1 parent 4655d69 commit 8facaef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
4 changes: 1 addition & 3 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8facaef

Please sign in to comment.