Skip to content

Commit

Permalink
Sync v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jan 14, 2025
1 parent 9aba1ef commit acab975
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 177 deletions.
4 changes: 2 additions & 2 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ describe('reactivity/computed', () => {

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

Expand Down
24 changes: 16 additions & 8 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
type Link,
type Subscriber,
SubscriberFlags,
endTrack,
isDirty,
endTracking,
link,
processComputedUpdate,
startTrack,
startTracking,
updateDirtyFlag,
} from './system'
import { warn } from './warning'

Expand Down Expand Up @@ -66,7 +66,7 @@ export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
// Subscriber
deps: Link | undefined = undefined
depsTail: Link | undefined = undefined
flags: SubscriberFlags = SubscriberFlags.Dirty
flags: SubscriberFlags = SubscriberFlags.Computed | SubscriberFlags.Dirty

/**
* @internal
Expand All @@ -89,13 +89,21 @@ export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
}
// for backwards compat
get _dirty(): boolean {
return isDirty(this, this.flags)
const flags = this.flags
if (
flags & SubscriberFlags.Dirty ||
(flags & SubscriberFlags.PendingComputed &&
updateDirtyFlag(this, this.flags))
) {
return true
}
return false
}
set _dirty(v: boolean) {
if (v) {
this.flags |= SubscriberFlags.Dirty
} else {
this.flags &= ~(SubscriberFlags.Dirty | SubscriberFlags.CheckRequired)
this.flags &= ~(SubscriberFlags.Dirty | SubscriberFlags.PendingComputed)
}
}

Expand Down Expand Up @@ -148,7 +156,7 @@ export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
update(): boolean {
const prevSub = activeSub
setActiveSub(this)
startTrack(this)
startTracking(this)
try {
const oldValue = this._value
const newValue = this.fn(oldValue)
Expand All @@ -159,7 +167,7 @@ export class ComputedRefImpl<T = any> implements Dependency, Subscriber {
return false
} finally {
setActiveSub(prevSub)
endTrack(this)
endTracking(this)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ function setupFlagsHandler(target: Subscriber): void {
if (
!(
(target as any)._flags &
(SubscriberFlags.CheckRequired | SubscriberFlags.Dirty)
(SubscriberFlags.PendingComputed | SubscriberFlags.Dirty)
) &&
!!(value & (SubscriberFlags.CheckRequired | SubscriberFlags.Dirty))
!!(value & (SubscriberFlags.PendingComputed | SubscriberFlags.Dirty))
) {
onTrigger(this)
}
Expand Down
33 changes: 20 additions & 13 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
type Link,
type Subscriber,
SubscriberFlags,
endTrack,
isDirty,
startTrack,
endTracking,
startTracking,
updateDirtyFlag,
} from './system'
import { warn } from './warning'

Expand Down Expand Up @@ -46,17 +46,17 @@ export enum EffectFlags {
/**
* ReactiveEffect only
*/
ALLOW_RECURSE = 1 << 6,
PAUSED = 1 << 7,
NOTIFIED = 1 << 8,
STOP = 1 << 9,
ALLOW_RECURSE = 1 << 7,
PAUSED = 1 << 8,
NOTIFIED = 1 << 9,
STOP = 1 << 10,
}

export class ReactiveEffect<T = any> implements ReactiveEffectOptions {
// Subscriber
deps: Link | undefined = undefined
depsTail: Link | undefined = undefined
flags: number = 0
flags: number = SubscriberFlags.Effect

/**
* @internal
Expand Down Expand Up @@ -119,7 +119,7 @@ export class ReactiveEffect<T = any> implements ReactiveEffectOptions {
cleanupEffect(this)
const prevSub = activeSub
setActiveSub(this)
startTrack(this)
startTracking(this)

try {
return this.fn()
Expand All @@ -131,7 +131,7 @@ export class ReactiveEffect<T = any> implements ReactiveEffectOptions {
)
}
setActiveSub(prevSub)
endTrack(this)
endTracking(this)
if (
this.flags & SubscriberFlags.Recursed &&
this.flags & EffectFlags.ALLOW_RECURSE
Expand All @@ -144,16 +144,23 @@ export class ReactiveEffect<T = any> implements ReactiveEffectOptions {

stop(): void {
if (this.active) {
startTrack(this)
endTrack(this)
startTracking(this)
endTracking(this)
cleanupEffect(this)
this.onStop && this.onStop()
this.flags |= EffectFlags.STOP
}
}

get dirty(): boolean {
return isDirty(this, this.flags)
const flags = this.flags
if (
flags & SubscriberFlags.Dirty ||
(flags & SubscriberFlags.PendingComputed && updateDirtyFlag(this, flags))
) {
return true
}
return false
}
}

Expand Down
11 changes: 5 additions & 6 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { EffectFlags, type ReactiveEffect } from './effect'
import {
type Link,
type Subscriber,
SubscriberFlags,
endTrack,
startTrack,
endTracking,
startTracking,
} from './system'
import { warn } from './warning'

Expand All @@ -14,7 +13,7 @@ export class EffectScope implements Subscriber {
// Subscriber: In order to collect orphans computeds
deps: Link | undefined = undefined
depsTail: Link | undefined = undefined
flags: number = SubscriberFlags.None
flags: number = 0

/**
* @internal
Expand Down Expand Up @@ -122,8 +121,8 @@ export class EffectScope implements Subscriber {
stop(fromParent?: boolean): void {
if (this.active) {
this.flags |= EffectFlags.STOP
startTrack(this)
endTrack(this)
startTracking(this)
endTracking(this)
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
Expand Down
Loading

0 comments on commit acab975

Please sign in to comment.