Skip to content

Commit

Permalink
refactor: replaced array-based batchedCleanup with a linked list fo…
Browse files Browse the repository at this point in the history
…r computed subscribers
  • Loading branch information
jh-leong committed Sep 30, 2024
1 parent 2468ba6 commit 46c96ba
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,13 @@ export class ReactiveEffect<T = any>

let batchDepth = 0
let batchedSub: Subscriber | undefined
const batchedCleanup: (() => void)[] = []
let batchedComputed: Subscriber | undefined

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
})
sub.next = batchedComputed
batchedComputed = sub
return
}
sub.next = batchedSub
Expand All @@ -265,11 +263,15 @@ export function endBatch(): void {
return
}

if (batchedCleanup.length) {
for (let i = 0; i < batchedCleanup.length; i++) {
batchedCleanup[i]()
if (batchedComputed) {
let e: Subscriber | undefined = batchedComputed
batchedComputed = undefined
while (e) {
const next: Subscriber | undefined = e.next
e.next = undefined
e.flags &= ~EffectFlags.NOTIFIED
e = next
}
batchedCleanup.length = 0
}

let error: unknown
Expand Down

0 comments on commit 46c96ba

Please sign in to comment.