Skip to content

Commit

Permalink
refactor: remove namespace usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 13, 2024
1 parent 80a4c84 commit 571ba05
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 330 deletions.
26 changes: 15 additions & 11 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { ReactiveFlags, TrackOpTypes } from './constants'
import { onTrack, setupDirtyLevelHandler } from './debug'
import type { DebuggerEvent, DebuggerOptions } from './effect'
import {
Dependency,
type Dependency,
DirtyLevels,
type IComputed,
type Link,
Subscriber,
System,
activeSub,
activeTrackId,
endTrack,
link,
propagate,
resolveMaybeDirty,
startTrack,
} from './effect'
import type { Ref } from './ref'
import { warn } from './warning'
Expand Down Expand Up @@ -85,7 +90,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
get _dirty(): boolean {
let dirtyLevel = this.dirtyLevel
if (dirtyLevel === DirtyLevels.MaybeDirty) {
Subscriber.resolveMaybeDirty(this)
resolveMaybeDirty(this)
dirtyLevel = this.dirtyLevel
}
return dirtyLevel >= DirtyLevels.Dirty
Expand Down Expand Up @@ -123,21 +128,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
if (this._dirty) {
this.update()
}
const activeTrackId = System.activeTrackId
if (activeTrackId !== 0) {
const subsTail = this.subsTail
if (subsTail === undefined || subsTail.trackId !== activeTrackId) {
if (__DEV__) {
onTrack(System.activeSub!, {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
}
Dependency.link(this, System.activeSub!)
link(this, activeSub!)
}
} else if (activeEffectScope !== undefined) {
Dependency.link(this, activeEffectScope)
link(this, activeEffectScope)
}
return this._value!
}
Expand All @@ -151,19 +155,19 @@ export class ComputedRefImpl<T = any> implements IComputed {
}

update(): void {
const prevSub = Subscriber.startTrack(this)
const prevSub = startTrack(this)
const oldValue = this._value
let newValue: T
try {
newValue = this.fn(oldValue)
} finally {
Subscriber.endTrack(this, prevSub)
endTrack(this, prevSub)
}
if (hasChanged(oldValue, newValue)) {
this._value = newValue
const subs = this.subs
if (subs !== undefined) {
Dependency.propagate(subs)
propagate(subs)
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared'
import { type TrackOpTypes, TriggerOpTypes } from './constants'
import { onTrack, triggerEventInfos } from './debug'
import { Dependency, type Link, System, endBatch, startBatch } from './effect'
import {
type Dependency,
type Link,
activeSub,
activeTrackId,
endBatch,
link,
propagate,
startBatch,
} from './effect'

class Dep implements Dependency {
_subs: Link | undefined = undefined
Expand Down Expand Up @@ -53,7 +62,6 @@ export const ARRAY_ITERATE_KEY: unique symbol = Symbol(
* @param key - Identifier of the reactive property to track.
*/
export function track(target: object, type: TrackOpTypes, key: unknown): void {
const activeTrackId = System.activeTrackId
if (activeTrackId > 0) {
let depsMap = targetMap.get(target)
if (!depsMap) {
Expand All @@ -66,13 +74,13 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
const subsTail = dep.subsTail
if (subsTail === undefined || subsTail.trackId !== activeTrackId) {
if (__DEV__) {
onTrack(System.activeSub!, {
onTrack(activeSub!, {
target,
type,
key,
})
}
Dependency.link(dep, System.activeSub!)
link(dep, activeSub!)
}
}
}
Expand Down Expand Up @@ -111,7 +119,7 @@ export function trigger(
oldTarget,
})
}
Dependency.propagate(dep.subs)
propagate(dep.subs)
if (__DEV__) {
triggerEventInfos.pop()
}
Expand Down
Loading

0 comments on commit 571ba05

Please sign in to comment.