Skip to content

Commit

Permalink
fix(reactivity): fix recursive sync watcher on computed edge case
Browse files Browse the repository at this point in the history
close #12033
close #12037
  • Loading branch information
yyx990803 committed Sep 26, 2024
1 parent cb34b28 commit 10ff159
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/reactivity/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
WatchErrorCodes,
type WatchOptions,
type WatchScheduler,
computed,
onWatcherCleanup,
ref,
watch,
Expand Down Expand Up @@ -209,4 +210,23 @@ describe('watch', () => {
source.value++
expect(dummy).toBe(1)
})

// #12033
test('recursive sync watcher on computed', () => {
const r = ref(0)
const c = computed(() => r.value)

watch(c, v => {
if (v > 1) {
r.value--
}
})

expect(r.value).toBe(0)
expect(c.value).toBe(0)

r.value = 10
expect(r.value).toBe(1)
expect(c.value).toBe(1)
})
})
11 changes: 8 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,14 @@ export function endBatch(): void {
let error: unknown
while (batchedSub) {
let e: Subscriber | undefined = batchedSub
batchedSub = undefined
let next: Subscriber | undefined
while (e) {
const next: Subscriber | undefined = e.next
e.next = undefined
e.flags &= ~EffectFlags.NOTIFIED
e = e.next
}
e = batchedSub
batchedSub = undefined
while (e) {
if (e.flags & EffectFlags.ACTIVE) {
try {
// ACTIVE flag is effect-only
Expand All @@ -273,6 +276,8 @@ export function endBatch(): void {
if (!error) error = err
}
}
next = e.next
e.next = undefined
e = next
}
}
Expand Down

0 comments on commit 10ff159

Please sign in to comment.