From 46c96ba38a14fa51f8ce453b0098b3b7f8a20d4a Mon Sep 17 00:00:00 2001 From: tycho Date: Mon, 30 Sep 2024 11:58:22 +0800 Subject: [PATCH] refactor: replaced array-based `batchedCleanup` with a linked list for computed subscribers --- packages/reactivity/src/effect.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index c6039b03830..243518839b3 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -234,15 +234,13 @@ export class ReactiveEffect 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 @@ -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