Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apiWatch): callback not called when using flush:sync #1633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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