Skip to content

Commit

Permalink
feat(reactivity): sync alien-signals 0.4.10
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 25, 2024
1 parent 1a24410 commit 2b596f9
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 89 deletions.
27 changes: 15 additions & 12 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
checkDirty,
endTrack,
link,
shallowPropagate,
startTrack,
} from './system'
import { warn } from './warning'
Expand Down Expand Up @@ -59,7 +60,6 @@ export class ComputedRefImpl<T = any> implements IComputed {
* @internal
*/
_value: T | undefined = undefined
version = 0

// Dependency
subs: Link | undefined = undefined
Expand Down Expand Up @@ -134,7 +134,12 @@ export class ComputedRefImpl<T = any> implements IComputed {

get value(): T {
if (this._dirty) {
this.update()
if (this.update()) {
const subs = this.subs
if (subs !== undefined) {
shallowPropagate(subs)
}
}
}
if (activeTrackId) {
if (this.lastTrackedId !== activeTrackId) {
Expand All @@ -146,7 +151,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
})
}
this.lastTrackedId = activeTrackId
link(this, activeSub!).version = this.version
link(this, activeSub!)
}
} else if (activeEffectScope !== undefined) {
if (this.lastTrackedId !== activeEffectScope.trackId) {
Expand All @@ -169,20 +174,18 @@ export class ComputedRefImpl<T = any> implements IComputed {
const prevTrackId = activeTrackId
setActiveSub(this, nextTrackId())
startTrack(this)
const oldValue = this._value
let newValue: T
try {
newValue = this.fn(oldValue)
const oldValue = this._value
const newValue = this.fn(oldValue)
if (hasChanged(oldValue, newValue)) {
this._value = newValue
return true
}
return false
} finally {
setActiveSub(prevSub, prevTrackId)
endTrack(this)
}
if (hasChanged(oldValue, newValue)) {
this._value = newValue
this.version++
return true
}
return false
}
}

Expand Down
17 changes: 8 additions & 9 deletions packages/reactivity/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,22 @@ export function setupOnTrigger(target: { new (...args: any[]): any }): void {
}

function setupFlagsHandler(target: Subscriber): void {
// @ts-expect-error
target._flags = target.flags
;(target as any)._flags = target.flags
Object.defineProperty(target, 'flags', {
get() {
// @ts-expect-error
return target._flags
return (target as any)._flags
},
set(value) {
if (
// @ts-expect-error
!(target._flags >> SubscriberFlags.DirtyFlagsIndex) &&
!!(value >> SubscriberFlags.DirtyFlagsIndex)
!(
(target as any)._flags &
(SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty)
) &&
!!(value & (SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty))
) {
onTrigger(this)
}
// @ts-expect-error
target._flags = value
;(target as any)._flags = value
},
})
}
12 changes: 6 additions & 6 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export enum EffectFlags {
/**
* ReactiveEffect only
*/
ALLOW_RECURSE = 1 << 2,
PAUSED = 1 << 3,
NOTIFIED = 1 << 4,
STOP = 1 << 5,
ALLOW_RECURSE = 1 << 5,
PAUSED = 1 << 6,
NOTIFIED = 1 << 7,
STOP = 1 << 8,
}

export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
Expand Down Expand Up @@ -137,10 +137,10 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
setActiveSub(prevSub, prevTrackId)
endTrack(this)
if (
this.flags & SubscriberFlags.CanPropagate &&
this.flags & SubscriberFlags.Recursed &&
this.flags & EffectFlags.ALLOW_RECURSE
) {
this.flags &= ~SubscriberFlags.CanPropagate
this.flags &= ~SubscriberFlags.Recursed
this.notify()
}
}
Expand Down
Loading

0 comments on commit 2b596f9

Please sign in to comment.