Skip to content

Commit

Permalink
Sync alien-signals 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 19, 2024
1 parent c7116ca commit 0e5d59f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 62 deletions.
12 changes: 8 additions & 4 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
endTrack,
link,
propagate,
setActiveSub,
startTrack,
} from './effect'
import { activeEffectScope } from './effectScope'
Expand Down Expand Up @@ -147,10 +148,10 @@ export class ComputedRefImpl<T = any> implements IComputed {
key: 'value',
})
}
link(this, activeSub!)
link(this, activeSub!, activeTrackId)
}
} else if (activeEffectScope !== undefined) {
link(this, activeEffectScope)
link(this, activeEffectScope, Math.abs(activeEffectScope.trackId))
}
return this._value!
}
Expand All @@ -164,13 +165,16 @@ export class ComputedRefImpl<T = any> implements IComputed {
}

update(): boolean {
const prevSub = startTrack(this)
const prevSub = activeSub
const prevTrackId = activeTrackId
setActiveSub(this, startTrack(this))
const oldValue = this._value
let newValue: T
try {
newValue = this.fn(oldValue)
} finally {
endTrack(this, prevSub)
setActiveSub(prevSub, prevTrackId)
endTrack(this)
}
if (hasChanged(oldValue, newValue)) {
this._value = newValue
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
key,
})
}
link(dep, activeSub!)
link(dep, activeSub!, activeTrackId)
}
}
}
Expand Down
104 changes: 48 additions & 56 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
return this.fn()
}
cleanupEffect(this)
const prevSub = startTrack(this)
const prevSub = activeSub
const prevTrackId = activeTrackId
setActiveSub(this, startTrack(this))

try {
return this.fn()
Expand All @@ -144,7 +146,8 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
'this is likely a Vue internal bug.',
)
}
endTrack(this, prevSub)
setActiveSub(prevSub, prevTrackId)
endTrack(this)
if (this.canPropagate && this.allowRecurse) {
this.canPropagate = false
this.notify()
Expand Down Expand Up @@ -310,7 +313,7 @@ function cleanupEffect(e: ReactiveEffect) {
}
}

//#region Ported from https://github.com/stackblitz/alien-signals/blob/v0.3.0/src/system.ts
//#region Ported from https://github.com/stackblitz/alien-signals/blob/v0.3.1/src/system.ts
export interface IEffect extends Subscriber {
nextNotify: IEffect | undefined
notify(): void
Expand Down Expand Up @@ -360,8 +363,16 @@ let queuedEffects: IEffect | undefined = undefined
let queuedEffectsTail: IEffect | undefined = undefined
let linkPool: Link | undefined = undefined

export function setActiveSub(
sub: Subscriber | undefined,
trackId: number,
): void {
activeSub = sub
activeTrackId = trackId
}

export function startBatch(): void {
batchDepth++
++batchDepth
}

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

//#region Dependency
export function link(dep: Dependency, sub: Subscriber): void {
export function link(dep: Dependency, sub: Subscriber, trackId: number): void {
const depsTail = sub.depsTail
const old = depsTail !== undefined ? depsTail.nextDep : sub.deps

Expand All @@ -400,12 +411,12 @@ export function link(dep: Dependency, sub: Subscriber): void {
newLink.nextDep = old
newLink.dep = dep
newLink.sub = sub
newLink.trackId = sub.trackId
newLink.trackId = trackId
} else {
newLink = {
dep,
sub,
trackId: sub.trackId,
trackId: trackId,
nextDep: old,
prevSub: undefined,
nextSub: undefined,
Expand All @@ -429,7 +440,7 @@ export function link(dep: Dependency, sub: Subscriber): void {
sub.depsTail = newLink
dep.subsTail = newLink
} else {
old.trackId = sub.trackId
old.trackId = trackId
sub.depsTail = old
}
}
Expand All @@ -442,28 +453,9 @@ export function propagate(subs: Link): void {
top: do {
const sub = link.sub
const subTrackId = sub.trackId
const linkTrackId = link.trackId

if (subTrackId > 0) {
if (subTrackId === link.trackId) {
const subDirtyLevel = sub.dirtyLevel
if (subDirtyLevel < dirtyLevel) {
sub.dirtyLevel = dirtyLevel
if (subDirtyLevel === DirtyLevels.None) {
sub.canPropagate = true

if ('subs' in sub && sub.subs !== undefined) {
subs = sub.subs
subs.prevSub = link
link = subs
dirtyLevel = DirtyLevels.MaybeDirty
stack++

continue
}
}
}
}
} else if (subTrackId === -link.trackId) {
if (subTrackId === -linkTrackId) {
const subDirtyLevel = sub.dirtyLevel
const notDirty = subDirtyLevel === DirtyLevels.None

Expand All @@ -481,7 +473,7 @@ export function propagate(subs: Link): void {
subs.prevSub = link
link = subs
dirtyLevel = DirtyLevels.MaybeDirty
stack++
++stack

continue
} else if ('notify' in sub) {
Expand All @@ -493,12 +485,30 @@ export function propagate(subs: Link): void {
queuedEffectsTail = sub
}
}
} else if (subTrackId === linkTrackId) {
const subDirtyLevel = sub.dirtyLevel
if (subDirtyLevel < dirtyLevel) {
sub.dirtyLevel = dirtyLevel
if (subDirtyLevel === DirtyLevels.None) {
sub.canPropagate = true

if ('subs' in sub && sub.subs !== undefined) {
subs = sub.subs
subs.prevSub = link
link = subs
dirtyLevel = DirtyLevels.MaybeDirty
++stack

continue
}
}
}
}

link = link.nextSub!
if (link === undefined) {
while (stack > 0) {
stack--
--stack
const prevLink = subs.prevSub!
subs.prevSub = undefined
subs = prevLink.dep.subs!
Expand Down Expand Up @@ -531,7 +541,7 @@ export function checkDirty(deps: Link): boolean {
if (dirtyLevel === DirtyLevels.MaybeDirty) {
dep.subs!.prevSub = deps
deps = dep.deps!
stack++
++stack
continue
}
if (dirtyLevel === DirtyLevels.Dirty) {
Expand All @@ -541,7 +551,7 @@ export function checkDirty(deps: Link): boolean {
if (stack > 0) {
let sub = deps.sub as IComputed
do {
stack--
--stack
const subSubs = sub.subs!
const prevLink = subSubs.prevSub!
subSubs.prevSub = undefined
Expand Down Expand Up @@ -575,7 +585,7 @@ export function checkDirty(deps: Link): boolean {
if (stack > 0) {
let sub = deps.sub as IComputed
do {
stack--
--stack
const subSubs = sub.subs!
const prevLink = subSubs.prevSub!
subSubs.prevSub = undefined
Expand Down Expand Up @@ -604,33 +614,15 @@ export function checkDirty(deps: Link): boolean {
} while (true)
}

export function startTrack(sub: Subscriber): Subscriber | undefined {
const newTrackId = lastTrackId + 1
const prevSub = activeSub

activeSub = sub
activeTrackId = newTrackId
lastTrackId = newTrackId

export function startTrack(sub: Subscriber): number {
const newTrackId = ++lastTrackId
sub.depsTail = undefined
sub.trackId = newTrackId
sub.dirtyLevel = DirtyLevels.None

return prevSub
return newTrackId
}

export function endTrack(
sub: Subscriber,
prevSub: Subscriber | undefined,
): void {
if (prevSub !== undefined) {
activeSub = prevSub
activeTrackId = prevSub.trackId
} else {
activeSub = undefined
activeTrackId = 0
}

export function endTrack(sub: Subscriber): void {
const depsTail = sub.depsTail
if (depsTail !== undefined) {
if (depsTail.nextDep !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function trackRef(dep: Dependency) {
key: 'value',
})
}
link(dep, activeSub!)
link(dep, activeSub!, activeTrackId)
}
}
}
Expand Down

0 comments on commit 0e5d59f

Please sign in to comment.