|
1 | 1 | /* eslint-disable */ |
2 | | -// Ported from https://github.com/stackblitz/alien-signals/blob/v1.0.5/src/system.ts |
| 2 | +// Ported from https://github.com/stackblitz/alien-signals/blob/v1.0.6/src/system.ts |
3 | 3 | import type { ComputedRefImpl as Computed } from './computed.js' |
4 | 4 | import type { ReactiveEffect as Effect } from './effect.js' |
5 | 5 |
|
@@ -32,9 +32,14 @@ export const enum SubscriberFlags { |
32 | 32 | Propagated = Dirty | PendingComputed, |
33 | 33 | } |
34 | 34 |
|
35 | | -let batchDepth = 0 |
| 35 | +interface QueuedLink { |
| 36 | + effect: Effect |
| 37 | + next: QueuedLink | undefined |
| 38 | +} |
36 | 39 |
|
37 | | -const queuedEffects: Effect[] = [] |
| 40 | +let batchDepth = 0 |
| 41 | +let queuedEffects: QueuedLink | undefined |
| 42 | +let queuedEffectsTail: QueuedLink | undefined |
38 | 43 |
|
39 | 44 | export function startBatch(): void { |
40 | 45 | ++batchDepth |
@@ -107,7 +112,17 @@ export function propagate(link: Link): void { |
107 | 112 | continue |
108 | 113 | } |
109 | 114 | if (subFlags & SubscriberFlags.Effect) { |
110 | | - queuedEffects.push(sub as Effect) |
| 115 | + if (queuedEffectsTail !== undefined) { |
| 116 | + queuedEffectsTail = queuedEffectsTail.next = { |
| 117 | + effect: sub as Effect, |
| 118 | + next: undefined, |
| 119 | + } |
| 120 | + } else { |
| 121 | + queuedEffectsTail = queuedEffects = { |
| 122 | + effect: sub as Effect, |
| 123 | + next: undefined, |
| 124 | + } |
| 125 | + } |
111 | 126 | } |
112 | 127 | } else if (!(subFlags & (SubscriberFlags.Tracking | targetFlag))) { |
113 | 128 | sub.flags = subFlags | targetFlag |
@@ -205,8 +220,13 @@ export function processComputedUpdate( |
205 | 220 | } |
206 | 221 |
|
207 | 222 | export function processEffectNotifications(): void { |
208 | | - while (queuedEffects.length) { |
209 | | - queuedEffects.shift()!.notify() |
| 223 | + while (queuedEffects !== undefined) { |
| 224 | + const effect = queuedEffects.effect |
| 225 | + queuedEffects = queuedEffects.next |
| 226 | + if (queuedEffects === undefined) { |
| 227 | + queuedEffectsTail = undefined |
| 228 | + } |
| 229 | + effect.notify() |
210 | 230 | } |
211 | 231 | } |
212 | 232 |
|
|
0 commit comments