Skip to content

Commit 0e5d59f

Browse files
committed
Sync alien-signals 0.3.1
1 parent c7116ca commit 0e5d59f

File tree

4 files changed

+58
-62
lines changed

4 files changed

+58
-62
lines changed

packages/reactivity/src/computed.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
endTrack,
1414
link,
1515
propagate,
16+
setActiveSub,
1617
startTrack,
1718
} from './effect'
1819
import { activeEffectScope } from './effectScope'
@@ -147,10 +148,10 @@ export class ComputedRefImpl<T = any> implements IComputed {
147148
key: 'value',
148149
})
149150
}
150-
link(this, activeSub!)
151+
link(this, activeSub!, activeTrackId)
151152
}
152153
} else if (activeEffectScope !== undefined) {
153-
link(this, activeEffectScope)
154+
link(this, activeEffectScope, Math.abs(activeEffectScope.trackId))
154155
}
155156
return this._value!
156157
}
@@ -164,13 +165,16 @@ export class ComputedRefImpl<T = any> implements IComputed {
164165
}
165166

166167
update(): boolean {
167-
const prevSub = startTrack(this)
168+
const prevSub = activeSub
169+
const prevTrackId = activeTrackId
170+
setActiveSub(this, startTrack(this))
168171
const oldValue = this._value
169172
let newValue: T
170173
try {
171174
newValue = this.fn(oldValue)
172175
} finally {
173-
endTrack(this, prevSub)
176+
setActiveSub(prevSub, prevTrackId)
177+
endTrack(this)
174178
}
175179
if (hasChanged(oldValue, newValue)) {
176180
this._value = newValue

packages/reactivity/src/dep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
8080
key,
8181
})
8282
}
83-
link(dep, activeSub!)
83+
link(dep, activeSub!, activeTrackId)
8484
}
8585
}
8686
}

packages/reactivity/src/effect.ts

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
133133
return this.fn()
134134
}
135135
cleanupEffect(this)
136-
const prevSub = startTrack(this)
136+
const prevSub = activeSub
137+
const prevTrackId = activeTrackId
138+
setActiveSub(this, startTrack(this))
137139

138140
try {
139141
return this.fn()
@@ -144,7 +146,8 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
144146
'this is likely a Vue internal bug.',
145147
)
146148
}
147-
endTrack(this, prevSub)
149+
setActiveSub(prevSub, prevTrackId)
150+
endTrack(this)
148151
if (this.canPropagate && this.allowRecurse) {
149152
this.canPropagate = false
150153
this.notify()
@@ -310,7 +313,7 @@ function cleanupEffect(e: ReactiveEffect) {
310313
}
311314
}
312315

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
314317
export interface IEffect extends Subscriber {
315318
nextNotify: IEffect | undefined
316319
notify(): void
@@ -360,8 +363,16 @@ let queuedEffects: IEffect | undefined = undefined
360363
let queuedEffectsTail: IEffect | undefined = undefined
361364
let linkPool: Link | undefined = undefined
362365

366+
export function setActiveSub(
367+
sub: Subscriber | undefined,
368+
trackId: number,
369+
): void {
370+
activeSub = sub
371+
activeTrackId = trackId
372+
}
373+
363374
export function startBatch(): void {
364-
batchDepth++
375+
++batchDepth
365376
}
366377

367378
export function endBatch(): void {
@@ -387,7 +398,7 @@ export function drainQueuedEffects(): void {
387398
//#endregion System
388399

389400
//#region Dependency
390-
export function link(dep: Dependency, sub: Subscriber): void {
401+
export function link(dep: Dependency, sub: Subscriber, trackId: number): void {
391402
const depsTail = sub.depsTail
392403
const old = depsTail !== undefined ? depsTail.nextDep : sub.deps
393404

@@ -400,12 +411,12 @@ export function link(dep: Dependency, sub: Subscriber): void {
400411
newLink.nextDep = old
401412
newLink.dep = dep
402413
newLink.sub = sub
403-
newLink.trackId = sub.trackId
414+
newLink.trackId = trackId
404415
} else {
405416
newLink = {
406417
dep,
407418
sub,
408-
trackId: sub.trackId,
419+
trackId: trackId,
409420
nextDep: old,
410421
prevSub: undefined,
411422
nextSub: undefined,
@@ -429,7 +440,7 @@ export function link(dep: Dependency, sub: Subscriber): void {
429440
sub.depsTail = newLink
430441
dep.subsTail = newLink
431442
} else {
432-
old.trackId = sub.trackId
443+
old.trackId = trackId
433444
sub.depsTail = old
434445
}
435446
}
@@ -442,28 +453,9 @@ export function propagate(subs: Link): void {
442453
top: do {
443454
const sub = link.sub
444455
const subTrackId = sub.trackId
456+
const linkTrackId = link.trackId
445457

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) {
467459
const subDirtyLevel = sub.dirtyLevel
468460
const notDirty = subDirtyLevel === DirtyLevels.None
469461

@@ -481,7 +473,7 @@ export function propagate(subs: Link): void {
481473
subs.prevSub = link
482474
link = subs
483475
dirtyLevel = DirtyLevels.MaybeDirty
484-
stack++
476+
++stack
485477

486478
continue
487479
} else if ('notify' in sub) {
@@ -493,12 +485,30 @@ export function propagate(subs: Link): void {
493485
queuedEffectsTail = sub
494486
}
495487
}
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+
}
496506
}
497507

498508
link = link.nextSub!
499509
if (link === undefined) {
500510
while (stack > 0) {
501-
stack--
511+
--stack
502512
const prevLink = subs.prevSub!
503513
subs.prevSub = undefined
504514
subs = prevLink.dep.subs!
@@ -531,7 +541,7 @@ export function checkDirty(deps: Link): boolean {
531541
if (dirtyLevel === DirtyLevels.MaybeDirty) {
532542
dep.subs!.prevSub = deps
533543
deps = dep.deps!
534-
stack++
544+
++stack
535545
continue
536546
}
537547
if (dirtyLevel === DirtyLevels.Dirty) {
@@ -541,7 +551,7 @@ export function checkDirty(deps: Link): boolean {
541551
if (stack > 0) {
542552
let sub = deps.sub as IComputed
543553
do {
544-
stack--
554+
--stack
545555
const subSubs = sub.subs!
546556
const prevLink = subSubs.prevSub!
547557
subSubs.prevSub = undefined
@@ -575,7 +585,7 @@ export function checkDirty(deps: Link): boolean {
575585
if (stack > 0) {
576586
let sub = deps.sub as IComputed
577587
do {
578-
stack--
588+
--stack
579589
const subSubs = sub.subs!
580590
const prevLink = subSubs.prevSub!
581591
subSubs.prevSub = undefined
@@ -604,33 +614,15 @@ export function checkDirty(deps: Link): boolean {
604614
} while (true)
605615
}
606616

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
615619
sub.depsTail = undefined
616620
sub.trackId = newTrackId
617621
sub.dirtyLevel = DirtyLevels.None
618-
619-
return prevSub
622+
return newTrackId
620623
}
621624

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 {
634626
const depsTail = sub.depsTail
635627
if (depsTail !== undefined) {
636628
if (depsTail.nextDep !== undefined) {

packages/reactivity/src/ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function trackRef(dep: Dependency) {
216216
key: 'value',
217217
})
218218
}
219-
link(dep, activeSub!)
219+
link(dep, activeSub!, activeTrackId)
220220
}
221221
}
222222
}

0 commit comments

Comments
 (0)