Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jan 10, 2025
1 parent 6dd0ec4 commit f87d863
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 116 deletions.
47 changes: 13 additions & 34 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
type DebuggerEvent,
type DebuggerOptions,
activeSub,
activeTrackId,
nextTrackId,
setActiveSub,
} from './effect'
import { activeEffectScope } from './effectScope'
Expand All @@ -16,8 +14,8 @@ import {
type IComputed,
type Link,
SubscriberFlags,
checkDirty,
endTrack,
isDirty,
link,
shallowPropagate,
startTrack,
Expand Down Expand Up @@ -64,7 +62,6 @@ export class ComputedRefImpl<T = any> implements IComputed {
// Dependency
subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

// Subscriber
deps: Link | undefined = undefined
Expand Down Expand Up @@ -92,19 +89,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
}
// for backwards compat
get _dirty(): boolean {
const flags = this.flags
if (flags & SubscriberFlags.Dirty) {
return true
} else if (flags & SubscriberFlags.ToCheckDirty) {
if (checkDirty(this.deps!)) {
this.flags |= SubscriberFlags.Dirty
return true
} else {
this.flags &= ~SubscriberFlags.ToCheckDirty
return false
}
}
return false
return isDirty(this, this.flags)
}
set _dirty(v: boolean) {
if (v) {
Expand Down Expand Up @@ -141,22 +126,17 @@ export class ComputedRefImpl<T = any> implements IComputed {
}
}
}
if (activeTrackId) {
if (this.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
}
this.lastTrackedId = activeTrackId
link(this, activeSub!)
if (activeSub !== undefined) {
if (__DEV__) {
onTrack(activeSub!, {
target: this,
type: TrackOpTypes.GET,
key: 'value',
})
}
link(this, activeSub)
} else if (activeEffectScope !== undefined) {
if (this.lastTrackedId !== activeEffectScope.trackId) {
link(this, activeEffectScope)
}
link(this, activeEffectScope)
}
return this._value!
}
Expand All @@ -171,8 +151,7 @@ export class ComputedRefImpl<T = any> implements IComputed {

update(): boolean {
const prevSub = activeSub
const prevTrackId = activeTrackId
setActiveSub(this, nextTrackId())
setActiveSub(this)
startTrack(this)
try {
const oldValue = this._value
Expand All @@ -183,7 +162,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
}
return false
} finally {
setActiveSub(prevSub, prevTrackId)
setActiveSub(prevSub)
endTrack(this)
}
}
Expand Down
24 changes: 10 additions & 14 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared'
import { type TrackOpTypes, TriggerOpTypes } from './constants'
import { onTrack, triggerEventInfos } from './debug'
import { activeSub, activeTrackId } from './effect'
import { activeSub } from './effect'
import {
type Dependency,
type Link,
endBatch,
type Link,

Check failure on line 8 in packages/reactivity/src/dep.ts

View workflow job for this annotation

GitHub Actions / test / lint-and-test-dts

Member 'Link' of the import declaration should be sorted alphabetically
link,
propagate,
startBatch,
Expand All @@ -14,7 +14,6 @@ import {
class Dep implements Dependency {
_subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

constructor(
private map: KeyToDepMap,
Expand Down Expand Up @@ -62,7 +61,7 @@ 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 {
if (activeTrackId > 0) {
if (activeSub !== undefined) {
let depsMap = targetMap.get(target)
if (!depsMap) {
targetMap.set(target, (depsMap = new Map()))
Expand All @@ -71,17 +70,14 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
if (!dep) {
depsMap.set(key, (dep = new Dep(depsMap, key)))
}
if (dep.lastTrackedId !== activeTrackId) {
if (__DEV__) {
onTrack(activeSub!, {
target,
type,
key,
})
}
dep.lastTrackedId = activeTrackId
link(dep, activeSub!)
if (__DEV__) {
onTrack(activeSub!, {
target,
type,
key,
})
}
link(dep, activeSub!)
}
}

Expand Down
48 changes: 13 additions & 35 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
type Link,
type Subscriber,
SubscriberFlags,
checkDirty,
endTrack,
isDirty,
startTrack,
} from './system'
import { warn } from './warning'
Expand Down Expand Up @@ -121,8 +121,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
}
cleanupEffect(this)
const prevSub = activeSub
const prevTrackId = activeTrackId
setActiveSub(this, nextTrackId())
setActiveSub(this)
startTrack(this)

try {
Expand All @@ -134,7 +133,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
'this is likely a Vue internal bug.',
)
}
setActiveSub(prevSub, prevTrackId)
setActiveSub(prevSub)
endTrack(this)
if (
this.flags & SubscriberFlags.Recursed &&
Expand All @@ -157,19 +156,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
}

get dirty(): boolean {
const flags = this.flags
if (flags & SubscriberFlags.Dirty) {
return true
} else if (flags & SubscriberFlags.ToCheckDirty) {
if (checkDirty(this.deps!)) {
this.flags |= SubscriberFlags.Dirty
return true
} else {
this.flags &= ~SubscriberFlags.ToCheckDirty
return false
}
}
return false
return isDirty(this, this.flags)
}
}

Expand Down Expand Up @@ -214,15 +201,14 @@ export function stop(runner: ReactiveEffectRunner): void {
runner.effect.stop()
}

const resetTrackingStack: [sub: typeof activeSub, trackId: number][] = []
const resetTrackingStack: (Subscriber | undefined)[] = []

/**
* Temporarily pauses tracking.
*/
export function pauseTracking(): void {
resetTrackingStack.push([activeSub, activeTrackId])
resetTrackingStack.push(activeSub)
activeSub = undefined
activeTrackId = 0
}

/**
Expand All @@ -233,14 +219,14 @@ export function enableTracking(): void {
if (!isPaused) {
// Add the current active effect to the trackResetStack so it can be
// restored by calling resetTracking.
resetTrackingStack.push([activeSub, activeTrackId])
resetTrackingStack.push(activeSub)
} else {
// Add a placeholder to the trackResetStack so we can it can be popped
// to restore the previous active effect.
resetTrackingStack.push([undefined, 0])
resetTrackingStack.push(undefined)
for (let i = resetTrackingStack.length - 1; i >= 0; i--) {
if (resetTrackingStack[i][0] !== undefined) {
;[activeSub, activeTrackId] = resetTrackingStack[i]
if (resetTrackingStack[i] !== undefined) {
activeSub = resetTrackingStack[i]
break
}
}
Expand All @@ -258,10 +244,9 @@ export function resetTracking(): void {
)
}
if (resetTrackingStack.length) {
;[activeSub, activeTrackId] = resetTrackingStack.pop()!
activeSub = resetTrackingStack.pop()!
} else {
activeSub = undefined
activeTrackId = 0
}
}

Expand Down Expand Up @@ -304,14 +289,7 @@ function cleanupEffect(e: ReactiveEffect) {
}

export let activeSub: Subscriber | undefined = undefined
export let activeTrackId = 0
export let lastTrackId = 0
export const nextTrackId = (): number => ++lastTrackId

export function setActiveSub(
sub: Subscriber | undefined,
trackId: number,
): void {

export function setActiveSub(sub: Subscriber | undefined): void {
activeSub = sub
activeTrackId = trackId
}
4 changes: 1 addition & 3 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EffectFlags, type ReactiveEffect, nextTrackId } from './effect'
import { EffectFlags, type ReactiveEffect } from './effect'
import {
type Link,
type Subscriber,
Expand All @@ -16,8 +16,6 @@ export class EffectScope implements Subscriber {
depsTail: Link | undefined = undefined
flags: number = SubscriberFlags.None

trackId: number = nextTrackId()

/**
* @internal
*/
Expand Down
7 changes: 2 additions & 5 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ComputedRef, WritableComputedRef } from './computed'
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
import { onTrack, triggerEventInfos } from './debug'
import { getDepFromReactive } from './dep'
import { activeSub, activeTrackId } from './effect'
import { activeSub } from './effect'
import {
type Builtin,
type ShallowReactiveMarker,
Expand Down Expand Up @@ -112,7 +112,6 @@ class RefImpl<T = any> implements Dependency {
// Dependency
subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

_value: T
private _rawValue: T
Expand Down Expand Up @@ -196,15 +195,14 @@ export function triggerRef(ref: Ref): void {
}

function trackRef(dep: Dependency) {
if (activeTrackId && dep.lastTrackedId !== activeTrackId) {
if (activeSub !== undefined) {
if (__DEV__) {
onTrack(activeSub!, {
target: dep,
type: TrackOpTypes.GET,
key: 'value',
})
}
dep.lastTrackedId = activeTrackId
link(dep, activeSub!)
}
}
Expand Down Expand Up @@ -301,7 +299,6 @@ class CustomRefImpl<T> implements Dependency {
// Dependency
subs: Link | undefined = undefined
subsTail: Link | undefined = undefined
lastTrackedId = 0

private readonly _get: ReturnType<CustomRefFactory<T>>['get']
private readonly _set: ReturnType<CustomRefFactory<T>>['set']
Expand Down
Loading

0 comments on commit f87d863

Please sign in to comment.