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): prevent overwriting next property during batch processing #12075

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions packages/reactivity/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,21 @@ describe('watch', () => {
src.value = 10
expect(spy).toHaveBeenCalledTimes(2)
})

test('should ensure correct execution order in batch processing', () => {
const dummy: number[] = []
const n1 = ref(0)
const n2 = ref(0)
const sum = computed(() => n1.value + n2.value)
watch(n1, () => {
dummy.push(1)
n2.value++
})
jh-leong marked this conversation as resolved.
Show resolved Hide resolved
watch(sum, () => dummy.push(2))
watch(n1, () => dummy.push(3))

n1.value++

expect(dummy).toEqual([1, 2, 3])
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ComputedRefImpl<T = any> implements Subscriber {
// avoid infinite self recursion
activeSub !== this
) {
batch(this)
batch(this, true)
return true
} else if (__DEV__) {
// TODO warn
Expand Down
31 changes: 17 additions & 14 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,17 @@ export class ReactiveEffect<T = any>

let batchDepth = 0
let batchedSub: Subscriber | undefined
const batchedCleanup: (() => void)[] = []
jh-leong marked this conversation as resolved.
Show resolved Hide resolved

export function batch(sub: Subscriber): void {
export function batch(sub: Subscriber, isComputed = false): void {
sub.flags |= EffectFlags.NOTIFIED
if (isComputed) {
batchedCleanup.push(() => {
// clear notified flags for computed
sub.flags &= ~EffectFlags.NOTIFIED
})
return
}
sub.next = batchedSub
batchedSub = sub
}
Expand All @@ -257,24 +265,19 @@ export function endBatch(): void {
return
}

if (batchedCleanup.length) {
for (let i = 0; i < batchedCleanup.length; i++) {
batchedCleanup[i]()
}
batchedCleanup.length = 0
}

let error: unknown
while (batchedSub) {
let e: Subscriber | undefined = batchedSub
let next: Subscriber | undefined
// 1st pass: clear notified flags for computed upfront
// we use the ACTIVE flag as a discriminator between computed and effect,
// since NOTIFIED is useless for an inactive effect anyway.
while (e) {
if (!(e.flags & EffectFlags.ACTIVE)) {
e.flags &= ~EffectFlags.NOTIFIED
}
e = e.next
}
e = batchedSub
batchedSub = undefined
// 2nd pass: run effects
while (e) {
next = e.next
const next: Subscriber | undefined = e.next
e.next = undefined
e.flags &= ~EffectFlags.NOTIFIED
if (e.flags & EffectFlags.ACTIVE) {
Expand Down