Skip to content

Commit

Permalink
feat(reactivity): sync alien-signals 0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 17, 2024
1 parent f7d95ce commit 1a24410
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
8 changes: 6 additions & 2 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ describe('reactivity/computed', () => {
const c2 = computed(() => c1.value) as unknown as ComputedRefImpl

c2.value
expect(c1.flags & SubscriberFlags.Dirtys).toBe(0)
expect(c2.flags & SubscriberFlags.Dirtys).toBe(0)
expect(
c1.flags & (SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty),
).toBe(0)
expect(
c2.flags & (SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty),
).toBe(0)
})

it('should chained computeds dirtyLevel update with first computed effect', () => {
Expand Down
31 changes: 16 additions & 15 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
if (v) {
this.flags |= SubscriberFlags.Dirty
} else {
this.flags &= ~SubscriberFlags.Dirtys
this.flags &= ~(SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty)
}
}

Expand All @@ -136,21 +136,22 @@ export class ComputedRefImpl<T = any> implements IComputed {
if (this._dirty) {
this.update()
}
if (activeTrackId !== 0 && this.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
if (activeTrackId) {
if (this.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
}
this.lastTrackedId = activeTrackId
link(this, activeSub!).version = this.version
}
} else if (activeEffectScope !== undefined) {
if (this.lastTrackedId !== activeEffectScope.trackId) {
link(this, activeEffectScope)
}
this.lastTrackedId = activeTrackId
link(this, activeSub!).version = this.version
} else if (
activeEffectScope !== undefined &&
this.lastTrackedId !== activeEffectScope.trackId
) {
link(this, activeEffectScope)
}
return this._value!
}
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class EffectScope implements Subscriber {

run<T>(fn: () => T): T | undefined {
if (this.active) {
const currentEffectScope = activeEffectScope
const prevEffectScope = activeEffectScope
try {
activeEffectScope = this
return fn()
} finally {
activeEffectScope = currentEffectScope
activeEffectScope = prevEffectScope
}
} else if (__DEV__) {
warn(`cannot run an inactive effect scope.`)
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 @@ -196,7 +196,7 @@ export function triggerRef(ref: Ref): void {
}

function trackRef(dep: Dependency) {
if (activeTrackId !== 0 && dep.lastTrackedId !== activeTrackId) {
if (activeTrackId && dep.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target: dep,
Expand Down
19 changes: 10 additions & 9 deletions packages/reactivity/src/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Ported from https://github.com/stackblitz/alien-signals/blob/v0.4.4/src/system.ts
// Ported from https://github.com/stackblitz/alien-signals/blob/v0.4.5/src/system.ts

export interface IEffect extends Subscriber {
nextNotify: IEffect | undefined
Expand Down Expand Up @@ -42,7 +42,6 @@ export enum SubscriberFlags {
// 2~5 are using in EffectFlags
ToCheckDirty = 1 << 6,
Dirty = 1 << 7,
Dirtys = SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty,

DirtyFlagsIndex = 6,
}
Expand Down Expand Up @@ -278,7 +277,7 @@ export function checkDirty(deps: Link): boolean {
continue
}
} else {
sub.flags &= ~SubscriberFlags.Dirtys
sub.flags &= ~SubscriberFlags.ToCheckDirty
}
deps = prevLink.nextDep!
if (deps !== undefined) {
Expand All @@ -297,7 +296,12 @@ export function checkDirty(deps: Link): boolean {
export function startTrack(sub: Subscriber): void {
sub.depsTail = undefined
sub.flags =
(sub.flags & ~(SubscriberFlags.CanPropagate | SubscriberFlags.Dirtys)) |
(sub.flags &
~(
SubscriberFlags.CanPropagate |
SubscriberFlags.ToCheckDirty |
SubscriberFlags.Dirty
)) |
SubscriberFlags.Tracking
}

Expand Down Expand Up @@ -347,11 +351,8 @@ function clearTrack(link: Link): void {
linkPool = link

if (dep.subs === undefined && 'deps' in dep) {
if ('notify' in dep) {
dep.flags &= ~SubscriberFlags.Dirtys
} else {
dep.flags |= SubscriberFlags.Dirty
}
// dep is never be IEffect in Vue
dep.flags |= SubscriberFlags.Dirty
const depDeps = dep.deps
if (depDeps !== undefined) {
link = depDeps
Expand Down

0 comments on commit 1a24410

Please sign in to comment.