Skip to content

Commit 2b596f9

Browse files
committed
feat(reactivity): sync alien-signals 0.4.10
1 parent 1a24410 commit 2b596f9

File tree

4 files changed

+133
-89
lines changed

4 files changed

+133
-89
lines changed

packages/reactivity/src/computed.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
checkDirty,
2020
endTrack,
2121
link,
22+
shallowPropagate,
2223
startTrack,
2324
} from './system'
2425
import { warn } from './warning'
@@ -59,7 +60,6 @@ export class ComputedRefImpl<T = any> implements IComputed {
5960
* @internal
6061
*/
6162
_value: T | undefined = undefined
62-
version = 0
6363

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

135135
get value(): T {
136136
if (this._dirty) {
137-
this.update()
137+
if (this.update()) {
138+
const subs = this.subs
139+
if (subs !== undefined) {
140+
shallowPropagate(subs)
141+
}
142+
}
138143
}
139144
if (activeTrackId) {
140145
if (this.lastTrackedId !== activeTrackId) {
@@ -146,7 +151,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
146151
})
147152
}
148153
this.lastTrackedId = activeTrackId
149-
link(this, activeSub!).version = this.version
154+
link(this, activeSub!)
150155
}
151156
} else if (activeEffectScope !== undefined) {
152157
if (this.lastTrackedId !== activeEffectScope.trackId) {
@@ -169,20 +174,18 @@ export class ComputedRefImpl<T = any> implements IComputed {
169174
const prevTrackId = activeTrackId
170175
setActiveSub(this, nextTrackId())
171176
startTrack(this)
172-
const oldValue = this._value
173-
let newValue: T
174177
try {
175-
newValue = this.fn(oldValue)
178+
const oldValue = this._value
179+
const newValue = this.fn(oldValue)
180+
if (hasChanged(oldValue, newValue)) {
181+
this._value = newValue
182+
return true
183+
}
184+
return false
176185
} finally {
177186
setActiveSub(prevSub, prevTrackId)
178187
endTrack(this)
179188
}
180-
if (hasChanged(oldValue, newValue)) {
181-
this._value = newValue
182-
this.version++
183-
return true
184-
}
185-
return false
186189
}
187190
}
188191

packages/reactivity/src/debug.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,22 @@ export function setupOnTrigger(target: { new (...args: any[]): any }): void {
6262
}
6363

6464
function setupFlagsHandler(target: Subscriber): void {
65-
// @ts-expect-error
66-
target._flags = target.flags
65+
;(target as any)._flags = target.flags
6766
Object.defineProperty(target, 'flags', {
6867
get() {
69-
// @ts-expect-error
70-
return target._flags
68+
return (target as any)._flags
7169
},
7270
set(value) {
7371
if (
74-
// @ts-expect-error
75-
!(target._flags >> SubscriberFlags.DirtyFlagsIndex) &&
76-
!!(value >> SubscriberFlags.DirtyFlagsIndex)
72+
!(
73+
(target as any)._flags &
74+
(SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty)
75+
) &&
76+
!!(value & (SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty))
7777
) {
7878
onTrigger(this)
7979
}
80-
// @ts-expect-error
81-
target._flags = value
80+
;(target as any)._flags = value
8281
},
8382
})
8483
}

packages/reactivity/src/effect.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export enum EffectFlags {
4747
/**
4848
* ReactiveEffect only
4949
*/
50-
ALLOW_RECURSE = 1 << 2,
51-
PAUSED = 1 << 3,
52-
NOTIFIED = 1 << 4,
53-
STOP = 1 << 5,
50+
ALLOW_RECURSE = 1 << 5,
51+
PAUSED = 1 << 6,
52+
NOTIFIED = 1 << 7,
53+
STOP = 1 << 8,
5454
}
5555

5656
export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
@@ -137,10 +137,10 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
137137
setActiveSub(prevSub, prevTrackId)
138138
endTrack(this)
139139
if (
140-
this.flags & SubscriberFlags.CanPropagate &&
140+
this.flags & SubscriberFlags.Recursed &&
141141
this.flags & EffectFlags.ALLOW_RECURSE
142142
) {
143-
this.flags &= ~SubscriberFlags.CanPropagate
143+
this.flags &= ~SubscriberFlags.Recursed
144144
this.notify()
145145
}
146146
}

0 commit comments

Comments
 (0)