@@ -133,7 +133,9 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
133
133
return this . fn ( )
134
134
}
135
135
cleanupEffect ( this )
136
- const prevSub = startTrack ( this )
136
+ const prevSub = activeSub
137
+ const prevTrackId = activeTrackId
138
+ setActiveSub ( this , startTrack ( this ) )
137
139
138
140
try {
139
141
return this . fn ( )
@@ -144,7 +146,8 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
144
146
'this is likely a Vue internal bug.' ,
145
147
)
146
148
}
147
- endTrack ( this , prevSub )
149
+ setActiveSub ( prevSub , prevTrackId )
150
+ endTrack ( this )
148
151
if ( this . canPropagate && this . allowRecurse ) {
149
152
this . canPropagate = false
150
153
this . notify ( )
@@ -310,7 +313,7 @@ function cleanupEffect(e: ReactiveEffect) {
310
313
}
311
314
}
312
315
313
- //#region Ported from https://github.com/stackblitz/alien-signals/blob/v0.3.0 /src/system.ts
316
+ //#region Ported from https://github.com/stackblitz/alien-signals/blob/v0.3.1 /src/system.ts
314
317
export interface IEffect extends Subscriber {
315
318
nextNotify : IEffect | undefined
316
319
notify ( ) : void
@@ -360,8 +363,16 @@ let queuedEffects: IEffect | undefined = undefined
360
363
let queuedEffectsTail : IEffect | undefined = undefined
361
364
let linkPool : Link | undefined = undefined
362
365
366
+ export function setActiveSub (
367
+ sub : Subscriber | undefined ,
368
+ trackId : number ,
369
+ ) : void {
370
+ activeSub = sub
371
+ activeTrackId = trackId
372
+ }
373
+
363
374
export function startBatch ( ) : void {
364
- batchDepth ++
375
+ ++ batchDepth
365
376
}
366
377
367
378
export function endBatch ( ) : void {
@@ -387,7 +398,7 @@ export function drainQueuedEffects(): void {
387
398
//#endregion System
388
399
389
400
//#region Dependency
390
- export function link ( dep : Dependency , sub : Subscriber ) : void {
401
+ export function link ( dep : Dependency , sub : Subscriber , trackId : number ) : void {
391
402
const depsTail = sub . depsTail
392
403
const old = depsTail !== undefined ? depsTail . nextDep : sub . deps
393
404
@@ -400,12 +411,12 @@ export function link(dep: Dependency, sub: Subscriber): void {
400
411
newLink . nextDep = old
401
412
newLink . dep = dep
402
413
newLink . sub = sub
403
- newLink . trackId = sub . trackId
414
+ newLink . trackId = trackId
404
415
} else {
405
416
newLink = {
406
417
dep,
407
418
sub,
408
- trackId : sub . trackId ,
419
+ trackId : trackId ,
409
420
nextDep : old ,
410
421
prevSub : undefined ,
411
422
nextSub : undefined ,
@@ -429,7 +440,7 @@ export function link(dep: Dependency, sub: Subscriber): void {
429
440
sub . depsTail = newLink
430
441
dep . subsTail = newLink
431
442
} else {
432
- old . trackId = sub . trackId
443
+ old . trackId = trackId
433
444
sub . depsTail = old
434
445
}
435
446
}
@@ -442,28 +453,9 @@ export function propagate(subs: Link): void {
442
453
top: do {
443
454
const sub = link . sub
444
455
const subTrackId = sub . trackId
456
+ const linkTrackId = link . trackId
445
457
446
- if ( subTrackId > 0 ) {
447
- if ( subTrackId === link . trackId ) {
448
- const subDirtyLevel = sub . dirtyLevel
449
- if ( subDirtyLevel < dirtyLevel ) {
450
- sub . dirtyLevel = dirtyLevel
451
- if ( subDirtyLevel === DirtyLevels . None ) {
452
- sub . canPropagate = true
453
-
454
- if ( 'subs' in sub && sub . subs !== undefined ) {
455
- subs = sub . subs
456
- subs . prevSub = link
457
- link = subs
458
- dirtyLevel = DirtyLevels . MaybeDirty
459
- stack ++
460
-
461
- continue
462
- }
463
- }
464
- }
465
- }
466
- } else if ( subTrackId === - link . trackId ) {
458
+ if ( subTrackId === - linkTrackId ) {
467
459
const subDirtyLevel = sub . dirtyLevel
468
460
const notDirty = subDirtyLevel === DirtyLevels . None
469
461
@@ -481,7 +473,7 @@ export function propagate(subs: Link): void {
481
473
subs . prevSub = link
482
474
link = subs
483
475
dirtyLevel = DirtyLevels . MaybeDirty
484
- stack ++
476
+ ++ stack
485
477
486
478
continue
487
479
} else if ( 'notify' in sub ) {
@@ -493,12 +485,30 @@ export function propagate(subs: Link): void {
493
485
queuedEffectsTail = sub
494
486
}
495
487
}
488
+ } else if ( subTrackId === linkTrackId ) {
489
+ const subDirtyLevel = sub . dirtyLevel
490
+ if ( subDirtyLevel < dirtyLevel ) {
491
+ sub . dirtyLevel = dirtyLevel
492
+ if ( subDirtyLevel === DirtyLevels . None ) {
493
+ sub . canPropagate = true
494
+
495
+ if ( 'subs' in sub && sub . subs !== undefined ) {
496
+ subs = sub . subs
497
+ subs . prevSub = link
498
+ link = subs
499
+ dirtyLevel = DirtyLevels . MaybeDirty
500
+ ++ stack
501
+
502
+ continue
503
+ }
504
+ }
505
+ }
496
506
}
497
507
498
508
link = link . nextSub !
499
509
if ( link === undefined ) {
500
510
while ( stack > 0 ) {
501
- stack --
511
+ -- stack
502
512
const prevLink = subs . prevSub !
503
513
subs . prevSub = undefined
504
514
subs = prevLink . dep . subs !
@@ -531,7 +541,7 @@ export function checkDirty(deps: Link): boolean {
531
541
if ( dirtyLevel === DirtyLevels . MaybeDirty ) {
532
542
dep . subs ! . prevSub = deps
533
543
deps = dep . deps !
534
- stack ++
544
+ ++ stack
535
545
continue
536
546
}
537
547
if ( dirtyLevel === DirtyLevels . Dirty ) {
@@ -541,7 +551,7 @@ export function checkDirty(deps: Link): boolean {
541
551
if ( stack > 0 ) {
542
552
let sub = deps . sub as IComputed
543
553
do {
544
- stack --
554
+ -- stack
545
555
const subSubs = sub . subs !
546
556
const prevLink = subSubs . prevSub !
547
557
subSubs . prevSub = undefined
@@ -575,7 +585,7 @@ export function checkDirty(deps: Link): boolean {
575
585
if ( stack > 0 ) {
576
586
let sub = deps . sub as IComputed
577
587
do {
578
- stack --
588
+ -- stack
579
589
const subSubs = sub . subs !
580
590
const prevLink = subSubs . prevSub !
581
591
subSubs . prevSub = undefined
@@ -604,33 +614,15 @@ export function checkDirty(deps: Link): boolean {
604
614
} while ( true )
605
615
}
606
616
607
- export function startTrack ( sub : Subscriber ) : Subscriber | undefined {
608
- const newTrackId = lastTrackId + 1
609
- const prevSub = activeSub
610
-
611
- activeSub = sub
612
- activeTrackId = newTrackId
613
- lastTrackId = newTrackId
614
-
617
+ export function startTrack ( sub : Subscriber ) : number {
618
+ const newTrackId = ++ lastTrackId
615
619
sub . depsTail = undefined
616
620
sub . trackId = newTrackId
617
621
sub . dirtyLevel = DirtyLevels . None
618
-
619
- return prevSub
622
+ return newTrackId
620
623
}
621
624
622
- export function endTrack (
623
- sub : Subscriber ,
624
- prevSub : Subscriber | undefined ,
625
- ) : void {
626
- if ( prevSub !== undefined ) {
627
- activeSub = prevSub
628
- activeTrackId = prevSub . trackId
629
- } else {
630
- activeSub = undefined
631
- activeTrackId = 0
632
- }
633
-
625
+ export function endTrack ( sub : Subscriber ) : void {
634
626
const depsTail = sub . depsTail
635
627
if ( depsTail !== undefined ) {
636
628
if ( depsTail . nextDep !== undefined ) {
0 commit comments