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(reactivity): sync watch should be executed correctly #11589

Merged
merged 7 commits into from
Aug 13, 2024
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
8 changes: 2 additions & 6 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export enum EffectFlags {
NOTIFIED = 1 << 3,
DIRTY = 1 << 4,
ALLOW_RECURSE = 1 << 5,
NO_BATCH = 1 << 6,
PAUSED = 1 << 7,
PAUSED = 1 << 6,
}

/**
Expand Down Expand Up @@ -169,9 +168,6 @@ export class ReactiveEffect<T = any>
) {
return
}
if (this.flags & EffectFlags.NO_BATCH) {
return this.trigger()
}
if (!(this.flags & EffectFlags.NOTIFIED)) {
this.flags |= EffectFlags.NOTIFIED
this.nextEffect = batchedEffect
Expand Down Expand Up @@ -267,6 +263,7 @@ export function endBatch(): void {
return
}

batchDepth--
let error: unknown
while (batchedEffect) {
let e: ReactiveEffect | undefined = batchedEffect
Expand All @@ -286,7 +283,6 @@ export function endBatch(): void {
}
}

batchDepth--
if (error) throw error
}

Expand Down
28 changes: 28 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,4 +1856,32 @@ describe('api: watch', () => {

warn.mockRestore()
})

it('should be executed correctly', () => {
const v = ref(1)
let foo = ''

watch(
v,
() => {
foo += '1'
},
{
flush: 'sync',
},
)
watch(
v,
() => {
foo += '2'
},
{
flush: 'sync',
},
)

expect(foo).toBe('')
v.value++
expect(foo).toBe('12')
})
})
1 change: 0 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ function doWatch(

let scheduler: EffectScheduler
if (flush === 'sync') {
effect.flags |= EffectFlags.NO_BATCH
scheduler = job as any // the scheduler function gets called directly
} else if (flush === 'post') {
scheduler = () => queuePostRenderEffect(job, instance && instance.suspense)
Expand Down