Skip to content

Commit 3bda3e8

Browse files
authored
fix(reactivity): sync watch should be executed correctly (#11589)
close #11577
1 parent 3653bc0 commit 3bda3e8

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

packages/reactivity/src/effect.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ export enum EffectFlags {
4545
NOTIFIED = 1 << 3,
4646
DIRTY = 1 << 4,
4747
ALLOW_RECURSE = 1 << 5,
48-
NO_BATCH = 1 << 6,
49-
PAUSED = 1 << 7,
48+
PAUSED = 1 << 6,
5049
}
5150

5251
/**
@@ -169,9 +168,6 @@ export class ReactiveEffect<T = any>
169168
) {
170169
return
171170
}
172-
if (this.flags & EffectFlags.NO_BATCH) {
173-
return this.trigger()
174-
}
175171
if (!(this.flags & EffectFlags.NOTIFIED)) {
176172
this.flags |= EffectFlags.NOTIFIED
177173
this.nextEffect = batchedEffect
@@ -267,6 +263,7 @@ export function endBatch(): void {
267263
return
268264
}
269265

266+
batchDepth--
270267
let error: unknown
271268
while (batchedEffect) {
272269
let e: ReactiveEffect | undefined = batchedEffect
@@ -286,7 +283,6 @@ export function endBatch(): void {
286283
}
287284
}
288285

289-
batchDepth--
290286
if (error) throw error
291287
}
292288

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

+28
Original file line numberDiff line numberDiff line change
@@ -1856,4 +1856,32 @@ describe('api: watch', () => {
18561856

18571857
warn.mockRestore()
18581858
})
1859+
1860+
it('should be executed correctly', () => {
1861+
const v = ref(1)
1862+
let foo = ''
1863+
1864+
watch(
1865+
v,
1866+
() => {
1867+
foo += '1'
1868+
},
1869+
{
1870+
flush: 'sync',
1871+
},
1872+
)
1873+
watch(
1874+
v,
1875+
() => {
1876+
foo += '2'
1877+
},
1878+
{
1879+
flush: 'sync',
1880+
},
1881+
)
1882+
1883+
expect(foo).toBe('')
1884+
v.value++
1885+
expect(foo).toBe('12')
1886+
})
18591887
})

packages/runtime-core/src/apiWatch.ts

-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ function doWatch(
394394

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

0 commit comments

Comments
 (0)