Skip to content

Commit b75979f

Browse files
committed
fix(apiWatch): don't invoke watch callback during resuming
1 parent 604d087 commit b75979f

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

packages/reactivity/src/effect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export enum EffectFlags {
4949
DIRTY = 1 << 4,
5050
ALLOW_RECURSE = 1 << 5,
5151
PAUSED = 1 << 6,
52+
RESUMING = 1 << 7,
5253
}
5354

5455
/**
@@ -127,6 +128,7 @@ export class ReactiveEffect<T = any>
127128
this.flags &= ~EffectFlags.PAUSED
128129
if (pausedQueueEffects.has(this)) {
129130
pausedQueueEffects.delete(this)
131+
this.flags |= EffectFlags.RESUMING
130132
this.trigger()
131133
}
132134
}

packages/reactivity/src/watch.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ export function watch(
240240
if (cb) {
241241
// watch(source, cb)
242242
const newValue = effect.run()
243+
244+
// don't invoke cb during effect resuming
245+
if (effect.flags & EffectFlags.RESUMING) {
246+
oldValue = newValue
247+
effect.flags &= ~EffectFlags.RESUMING
248+
return
249+
}
250+
243251
if (
244252
deep ||
245253
forceTrigger ||

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,24 +1708,26 @@ describe('api: watch', () => {
17081708
resume()
17091709
count.value++
17101710
await nextTick()
1711-
expect(cb).toHaveBeenCalledTimes(2)
1712-
expect(cb).toHaveBeenLastCalledWith(3, 1, expect.any(Function))
1711+
// don't invoke callback during resuming
1712+
expect(cb).toHaveBeenCalledTimes(1)
1713+
expect(cb).toHaveBeenLastCalledWith(1, 0, expect.any(Function))
17131714

17141715
count.value++
17151716
await nextTick()
1716-
expect(cb).toHaveBeenCalledTimes(3)
1717+
expect(cb).toHaveBeenCalledTimes(2)
17171718
expect(cb).toHaveBeenLastCalledWith(4, 3, expect.any(Function))
17181719

17191720
pause()
17201721
count.value++
17211722
await nextTick()
1722-
expect(cb).toHaveBeenCalledTimes(3)
1723+
expect(cb).toHaveBeenCalledTimes(2)
17231724
expect(cb).toHaveBeenLastCalledWith(4, 3, expect.any(Function))
17241725

17251726
resume()
17261727
await nextTick()
1727-
expect(cb).toHaveBeenCalledTimes(4)
1728-
expect(cb).toHaveBeenLastCalledWith(5, 4, expect.any(Function))
1728+
// don't invoke callback during resuming
1729+
expect(cb).toHaveBeenCalledTimes(2)
1730+
expect(cb).toHaveBeenLastCalledWith(4, 3, expect.any(Function))
17291731
})
17301732

17311733
it('shallowReactive', async () => {

0 commit comments

Comments
 (0)