Skip to content

Commit 1a24410

Browse files
committed
feat(reactivity): sync alien-signals 0.4.5
1 parent f7d95ce commit 1a24410

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

packages/reactivity/__tests__/computed.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,12 @@ describe('reactivity/computed', () => {
467467
const c2 = computed(() => c1.value) as unknown as ComputedRefImpl
468468

469469
c2.value
470-
expect(c1.flags & SubscriberFlags.Dirtys).toBe(0)
471-
expect(c2.flags & SubscriberFlags.Dirtys).toBe(0)
470+
expect(
471+
c1.flags & (SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty),
472+
).toBe(0)
473+
expect(
474+
c2.flags & (SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty),
475+
).toBe(0)
472476
})
473477

474478
it('should chained computeds dirtyLevel update with first computed effect', () => {

packages/reactivity/src/computed.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
110110
if (v) {
111111
this.flags |= SubscriberFlags.Dirty
112112
} else {
113-
this.flags &= ~SubscriberFlags.Dirtys
113+
this.flags &= ~(SubscriberFlags.Dirty | SubscriberFlags.ToCheckDirty)
114114
}
115115
}
116116

@@ -136,21 +136,22 @@ export class ComputedRefImpl<T = any> implements IComputed {
136136
if (this._dirty) {
137137
this.update()
138138
}
139-
if (activeTrackId !== 0 && this.lastTrackedId !== activeTrackId) {
140-
if (__DEV__) {
141-
onTrack(activeSub!, {
142-
target: this,
143-
type: TrackOpTypes.GET,
144-
key: 'value',
145-
})
139+
if (activeTrackId) {
140+
if (this.lastTrackedId !== activeTrackId) {
141+
if (__DEV__) {
142+
onTrack(activeSub!, {
143+
target: this,
144+
type: TrackOpTypes.GET,
145+
key: 'value',
146+
})
147+
}
148+
this.lastTrackedId = activeTrackId
149+
link(this, activeSub!).version = this.version
150+
}
151+
} else if (activeEffectScope !== undefined) {
152+
if (this.lastTrackedId !== activeEffectScope.trackId) {
153+
link(this, activeEffectScope)
146154
}
147-
this.lastTrackedId = activeTrackId
148-
link(this, activeSub!).version = this.version
149-
} else if (
150-
activeEffectScope !== undefined &&
151-
this.lastTrackedId !== activeEffectScope.trackId
152-
) {
153-
link(this, activeEffectScope)
154155
}
155156
return this._value!
156157
}

packages/reactivity/src/effectScope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ export class EffectScope implements Subscriber {
9393

9494
run<T>(fn: () => T): T | undefined {
9595
if (this.active) {
96-
const currentEffectScope = activeEffectScope
96+
const prevEffectScope = activeEffectScope
9797
try {
9898
activeEffectScope = this
9999
return fn()
100100
} finally {
101-
activeEffectScope = currentEffectScope
101+
activeEffectScope = prevEffectScope
102102
}
103103
} else if (__DEV__) {
104104
warn(`cannot run an inactive effect scope.`)

packages/reactivity/src/ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export function triggerRef(ref: Ref): void {
196196
}
197197

198198
function trackRef(dep: Dependency) {
199-
if (activeTrackId !== 0 && dep.lastTrackedId !== activeTrackId) {
199+
if (activeTrackId && dep.lastTrackedId !== activeTrackId) {
200200
if (__DEV__) {
201201
onTrack(activeSub!, {
202202
target: dep,

packages/reactivity/src/system.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Ported from https://github.com/stackblitz/alien-signals/blob/v0.4.4/src/system.ts
1+
// Ported from https://github.com/stackblitz/alien-signals/blob/v0.4.5/src/system.ts
22

33
export interface IEffect extends Subscriber {
44
nextNotify: IEffect | undefined
@@ -42,7 +42,6 @@ export enum SubscriberFlags {
4242
// 2~5 are using in EffectFlags
4343
ToCheckDirty = 1 << 6,
4444
Dirty = 1 << 7,
45-
Dirtys = SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty,
4645

4746
DirtyFlagsIndex = 6,
4847
}
@@ -278,7 +277,7 @@ export function checkDirty(deps: Link): boolean {
278277
continue
279278
}
280279
} else {
281-
sub.flags &= ~SubscriberFlags.Dirtys
280+
sub.flags &= ~SubscriberFlags.ToCheckDirty
282281
}
283282
deps = prevLink.nextDep!
284283
if (deps !== undefined) {
@@ -297,7 +296,12 @@ export function checkDirty(deps: Link): boolean {
297296
export function startTrack(sub: Subscriber): void {
298297
sub.depsTail = undefined
299298
sub.flags =
300-
(sub.flags & ~(SubscriberFlags.CanPropagate | SubscriberFlags.Dirtys)) |
299+
(sub.flags &
300+
~(
301+
SubscriberFlags.CanPropagate |
302+
SubscriberFlags.ToCheckDirty |
303+
SubscriberFlags.Dirty
304+
)) |
301305
SubscriberFlags.Tracking
302306
}
303307

@@ -347,11 +351,8 @@ function clearTrack(link: Link): void {
347351
linkPool = link
348352

349353
if (dep.subs === undefined && 'deps' in dep) {
350-
if ('notify' in dep) {
351-
dep.flags &= ~SubscriberFlags.Dirtys
352-
} else {
353-
dep.flags |= SubscriberFlags.Dirty
354-
}
354+
// dep is never be IEffect in Vue
355+
dep.flags |= SubscriberFlags.Dirty
355356
const depDeps = dep.deps
356357
if (depDeps !== undefined) {
357358
link = depDeps

0 commit comments

Comments
 (0)