Skip to content

Commit f87d863

Browse files
committed
1 parent 6dd0ec4 commit f87d863

File tree

6 files changed

+80
-116
lines changed

6 files changed

+80
-116
lines changed

packages/reactivity/src/computed.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
type DebuggerEvent,
66
type DebuggerOptions,
77
activeSub,
8-
activeTrackId,
9-
nextTrackId,
108
setActiveSub,
119
} from './effect'
1210
import { activeEffectScope } from './effectScope'
@@ -16,8 +14,8 @@ import {
1614
type IComputed,
1715
type Link,
1816
SubscriberFlags,
19-
checkDirty,
2017
endTrack,
18+
isDirty,
2119
link,
2220
shallowPropagate,
2321
startTrack,
@@ -64,7 +62,6 @@ export class ComputedRefImpl<T = any> implements IComputed {
6462
// Dependency
6563
subs: Link | undefined = undefined
6664
subsTail: Link | undefined = undefined
67-
lastTrackedId = 0
6865

6966
// Subscriber
7067
deps: Link | undefined = undefined
@@ -92,19 +89,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
9289
}
9390
// for backwards compat
9491
get _dirty(): boolean {
95-
const flags = this.flags
96-
if (flags & SubscriberFlags.Dirty) {
97-
return true
98-
} else if (flags & SubscriberFlags.ToCheckDirty) {
99-
if (checkDirty(this.deps!)) {
100-
this.flags |= SubscriberFlags.Dirty
101-
return true
102-
} else {
103-
this.flags &= ~SubscriberFlags.ToCheckDirty
104-
return false
105-
}
106-
}
107-
return false
92+
return isDirty(this, this.flags)
10893
}
10994
set _dirty(v: boolean) {
11095
if (v) {
@@ -141,22 +126,17 @@ export class ComputedRefImpl<T = any> implements IComputed {
141126
}
142127
}
143128
}
144-
if (activeTrackId) {
145-
if (this.lastTrackedId !== activeTrackId) {
146-
if (__DEV__) {
147-
onTrack(activeSub!, {
148-
target: this,
149-
type: TrackOpTypes.GET,
150-
key: 'value',
151-
})
152-
}
153-
this.lastTrackedId = activeTrackId
154-
link(this, activeSub!)
129+
if (activeSub !== undefined) {
130+
if (__DEV__) {
131+
onTrack(activeSub!, {
132+
target: this,
133+
type: TrackOpTypes.GET,
134+
key: 'value',
135+
})
155136
}
137+
link(this, activeSub)
156138
} else if (activeEffectScope !== undefined) {
157-
if (this.lastTrackedId !== activeEffectScope.trackId) {
158-
link(this, activeEffectScope)
159-
}
139+
link(this, activeEffectScope)
160140
}
161141
return this._value!
162142
}
@@ -171,8 +151,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
171151

172152
update(): boolean {
173153
const prevSub = activeSub
174-
const prevTrackId = activeTrackId
175-
setActiveSub(this, nextTrackId())
154+
setActiveSub(this)
176155
startTrack(this)
177156
try {
178157
const oldValue = this._value
@@ -183,7 +162,7 @@ export class ComputedRefImpl<T = any> implements IComputed {
183162
}
184163
return false
185164
} finally {
186-
setActiveSub(prevSub, prevTrackId)
165+
setActiveSub(prevSub)
187166
endTrack(this)
188167
}
189168
}

packages/reactivity/src/dep.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared'
22
import { type TrackOpTypes, TriggerOpTypes } from './constants'
33
import { onTrack, triggerEventInfos } from './debug'
4-
import { activeSub, activeTrackId } from './effect'
4+
import { activeSub } from './effect'
55
import {
66
type Dependency,
7-
type Link,
87
endBatch,
8+
type Link,
99
link,
1010
propagate,
1111
startBatch,
@@ -14,7 +14,6 @@ import {
1414
class Dep implements Dependency {
1515
_subs: Link | undefined = undefined
1616
subsTail: Link | undefined = undefined
17-
lastTrackedId = 0
1817

1918
constructor(
2019
private map: KeyToDepMap,
@@ -62,7 +61,7 @@ export const ARRAY_ITERATE_KEY: unique symbol = Symbol(
6261
* @param key - Identifier of the reactive property to track.
6362
*/
6463
export function track(target: object, type: TrackOpTypes, key: unknown): void {
65-
if (activeTrackId > 0) {
64+
if (activeSub !== undefined) {
6665
let depsMap = targetMap.get(target)
6766
if (!depsMap) {
6867
targetMap.set(target, (depsMap = new Map()))
@@ -71,17 +70,14 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void {
7170
if (!dep) {
7271
depsMap.set(key, (dep = new Dep(depsMap, key)))
7372
}
74-
if (dep.lastTrackedId !== activeTrackId) {
75-
if (__DEV__) {
76-
onTrack(activeSub!, {
77-
target,
78-
type,
79-
key,
80-
})
81-
}
82-
dep.lastTrackedId = activeTrackId
83-
link(dep, activeSub!)
73+
if (__DEV__) {
74+
onTrack(activeSub!, {
75+
target,
76+
type,
77+
key,
78+
})
8479
}
80+
link(dep, activeSub!)
8581
}
8682
}
8783

packages/reactivity/src/effect.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
type Link,
88
type Subscriber,
99
SubscriberFlags,
10-
checkDirty,
1110
endTrack,
11+
isDirty,
1212
startTrack,
1313
} from './system'
1414
import { warn } from './warning'
@@ -121,8 +121,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
121121
}
122122
cleanupEffect(this)
123123
const prevSub = activeSub
124-
const prevTrackId = activeTrackId
125-
setActiveSub(this, nextTrackId())
124+
setActiveSub(this)
126125
startTrack(this)
127126

128127
try {
@@ -134,7 +133,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
134133
'this is likely a Vue internal bug.',
135134
)
136135
}
137-
setActiveSub(prevSub, prevTrackId)
136+
setActiveSub(prevSub)
138137
endTrack(this)
139138
if (
140139
this.flags & SubscriberFlags.Recursed &&
@@ -157,19 +156,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
157156
}
158157

159158
get dirty(): boolean {
160-
const flags = this.flags
161-
if (flags & SubscriberFlags.Dirty) {
162-
return true
163-
} else if (flags & SubscriberFlags.ToCheckDirty) {
164-
if (checkDirty(this.deps!)) {
165-
this.flags |= SubscriberFlags.Dirty
166-
return true
167-
} else {
168-
this.flags &= ~SubscriberFlags.ToCheckDirty
169-
return false
170-
}
171-
}
172-
return false
159+
return isDirty(this, this.flags)
173160
}
174161
}
175162

@@ -214,15 +201,14 @@ export function stop(runner: ReactiveEffectRunner): void {
214201
runner.effect.stop()
215202
}
216203

217-
const resetTrackingStack: [sub: typeof activeSub, trackId: number][] = []
204+
const resetTrackingStack: (Subscriber | undefined)[] = []
218205

219206
/**
220207
* Temporarily pauses tracking.
221208
*/
222209
export function pauseTracking(): void {
223-
resetTrackingStack.push([activeSub, activeTrackId])
210+
resetTrackingStack.push(activeSub)
224211
activeSub = undefined
225-
activeTrackId = 0
226212
}
227213

228214
/**
@@ -233,14 +219,14 @@ export function enableTracking(): void {
233219
if (!isPaused) {
234220
// Add the current active effect to the trackResetStack so it can be
235221
// restored by calling resetTracking.
236-
resetTrackingStack.push([activeSub, activeTrackId])
222+
resetTrackingStack.push(activeSub)
237223
} else {
238224
// Add a placeholder to the trackResetStack so we can it can be popped
239225
// to restore the previous active effect.
240-
resetTrackingStack.push([undefined, 0])
226+
resetTrackingStack.push(undefined)
241227
for (let i = resetTrackingStack.length - 1; i >= 0; i--) {
242-
if (resetTrackingStack[i][0] !== undefined) {
243-
;[activeSub, activeTrackId] = resetTrackingStack[i]
228+
if (resetTrackingStack[i] !== undefined) {
229+
activeSub = resetTrackingStack[i]
244230
break
245231
}
246232
}
@@ -258,10 +244,9 @@ export function resetTracking(): void {
258244
)
259245
}
260246
if (resetTrackingStack.length) {
261-
;[activeSub, activeTrackId] = resetTrackingStack.pop()!
247+
activeSub = resetTrackingStack.pop()!
262248
} else {
263249
activeSub = undefined
264-
activeTrackId = 0
265250
}
266251
}
267252

@@ -304,14 +289,7 @@ function cleanupEffect(e: ReactiveEffect) {
304289
}
305290

306291
export let activeSub: Subscriber | undefined = undefined
307-
export let activeTrackId = 0
308-
export let lastTrackId = 0
309-
export const nextTrackId = (): number => ++lastTrackId
310-
311-
export function setActiveSub(
312-
sub: Subscriber | undefined,
313-
trackId: number,
314-
): void {
292+
293+
export function setActiveSub(sub: Subscriber | undefined): void {
315294
activeSub = sub
316-
activeTrackId = trackId
317295
}

packages/reactivity/src/effectScope.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EffectFlags, type ReactiveEffect, nextTrackId } from './effect'
1+
import { EffectFlags, type ReactiveEffect } from './effect'
22
import {
33
type Link,
44
type Subscriber,
@@ -16,8 +16,6 @@ export class EffectScope implements Subscriber {
1616
depsTail: Link | undefined = undefined
1717
flags: number = SubscriberFlags.None
1818

19-
trackId: number = nextTrackId()
20-
2119
/**
2220
* @internal
2321
*/

packages/reactivity/src/ref.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ComputedRef, WritableComputedRef } from './computed'
99
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
1010
import { onTrack, triggerEventInfos } from './debug'
1111
import { getDepFromReactive } from './dep'
12-
import { activeSub, activeTrackId } from './effect'
12+
import { activeSub } from './effect'
1313
import {
1414
type Builtin,
1515
type ShallowReactiveMarker,
@@ -112,7 +112,6 @@ class RefImpl<T = any> implements Dependency {
112112
// Dependency
113113
subs: Link | undefined = undefined
114114
subsTail: Link | undefined = undefined
115-
lastTrackedId = 0
116115

117116
_value: T
118117
private _rawValue: T
@@ -196,15 +195,14 @@ export function triggerRef(ref: Ref): void {
196195
}
197196

198197
function trackRef(dep: Dependency) {
199-
if (activeTrackId && dep.lastTrackedId !== activeTrackId) {
198+
if (activeSub !== undefined) {
200199
if (__DEV__) {
201200
onTrack(activeSub!, {
202201
target: dep,
203202
type: TrackOpTypes.GET,
204203
key: 'value',
205204
})
206205
}
207-
dep.lastTrackedId = activeTrackId
208206
link(dep, activeSub!)
209207
}
210208
}
@@ -301,7 +299,6 @@ class CustomRefImpl<T> implements Dependency {
301299
// Dependency
302300
subs: Link | undefined = undefined
303301
subsTail: Link | undefined = undefined
304-
lastTrackedId = 0
305302

306303
private readonly _get: ReturnType<CustomRefFactory<T>>['get']
307304
private readonly _set: ReturnType<CustomRefFactory<T>>['set']

0 commit comments

Comments
 (0)