Skip to content

Commit 0dfda23

Browse files
committed
refactor(reactivity): streamline activeSub management in tracking functions
1 parent c2dad4b commit 0dfda23

File tree

7 files changed

+45
-41
lines changed

7 files changed

+45
-41
lines changed

packages/reactivity/src/computed.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { hasChanged, isFunction } from '@vue/shared'
22
import { ReactiveFlags, TrackOpTypes } from './constants'
33
import { onTrack, setupOnTrigger } from './debug'
4-
import {
5-
type DebuggerEvent,
6-
type DebuggerOptions,
7-
activeSub,
8-
setActiveSub,
9-
} from './effect'
4+
import { type DebuggerEvent, type DebuggerOptions } from './effect'
105
import { activeEffectScope } from './effectScope'
116
import type { Ref } from './ref'
127
import {
138
type Link,
149
type ReactiveNode,
1510
ReactiveFlags as _ReactiveFlags,
11+
activeSub,
1612
checkDirty,
1713
endTracking,
1814
link,
@@ -171,9 +167,7 @@ export class ComputedRefImpl<T = any> implements ReactiveNode {
171167
}
172168

173169
update(): boolean {
174-
const prevSub = activeSub
175-
setActiveSub(this)
176-
startTracking(this)
170+
const prevSub = startTracking(this)
177171
try {
178172
const oldValue = this._value
179173
const newValue = this.fn(oldValue)
@@ -183,8 +177,7 @@ export class ComputedRefImpl<T = any> implements ReactiveNode {
183177
}
184178
return false
185179
} finally {
186-
setActiveSub(prevSub)
187-
endTracking(this)
180+
endTracking(this, prevSub)
188181
}
189182
}
190183
}

packages/reactivity/src/dep.ts

Lines changed: 1 addition & 1 deletion
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 } from './effect'
54
import {
65
type Link,
76
ReactiveFlags,
87
type ReactiveNode,
8+
activeSub,
99
endBatch,
1010
link,
1111
propagate,

packages/reactivity/src/effect.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
type Link,
77
ReactiveFlags,
88
type ReactiveNode,
9+
activeSub,
910
checkDirty,
1011
endTracking,
1112
link,
13+
setActiveSub,
1214
startTracking,
1315
unlink,
1416
} from './system'
@@ -113,10 +115,7 @@ export class ReactiveEffect<T = any>
113115
return this.fn()
114116
}
115117
cleanup(this)
116-
const prevSub = activeSub
117-
setActiveSub(this)
118-
startTracking(this)
119-
118+
const prevSub = startTracking(this)
120119
try {
121120
return this.fn()
122121
} finally {
@@ -126,8 +125,7 @@ export class ReactiveEffect<T = any>
126125
'this is likely a Vue internal bug.',
127126
)
128127
}
129-
setActiveSub(prevSub)
130-
endTracking(this)
128+
endTracking(this, prevSub)
131129
const flags = this.flags
132130
if (
133131
(flags & (ReactiveFlags.Recursed | EffectFlags.ALLOW_RECURSE)) ===
@@ -234,7 +232,7 @@ const resetTrackingStack: (ReactiveNode | undefined)[] = []
234232
*/
235233
export function pauseTracking(): void {
236234
resetTrackingStack.push(activeSub)
237-
activeSub = undefined
235+
setActiveSub(undefined)
238236
}
239237

240238
/**
@@ -252,7 +250,7 @@ export function enableTracking(): void {
252250
resetTrackingStack.push(undefined)
253251
for (let i = resetTrackingStack.length - 1; i >= 0; i--) {
254252
if (resetTrackingStack[i] !== undefined) {
255-
activeSub = resetTrackingStack[i]
253+
setActiveSub(resetTrackingStack[i])
256254
break
257255
}
258256
}
@@ -270,9 +268,9 @@ export function resetTracking(): void {
270268
)
271269
}
272270
if (resetTrackingStack.length) {
273-
activeSub = resetTrackingStack.pop()!
271+
setActiveSub(resetTrackingStack.pop()!)
274272
} else {
275-
activeSub = undefined
273+
setActiveSub(undefined)
276274
}
277275
}
278276

@@ -313,17 +311,10 @@ export function onEffectCleanup(fn: () => void, failSilently = false): void {
313311

314312
function cleanupEffect(fn: () => void) {
315313
// run cleanup without active effect
316-
const prevSub = activeSub
317-
activeSub = undefined
314+
const prevSub = setActiveSub(undefined)
318315
try {
319316
fn()
320317
} finally {
321-
activeSub = prevSub
318+
setActiveSub(prevSub)
322319
}
323320
}
324-
325-
export let activeSub: ReactiveNode | undefined = undefined
326-
327-
export function setActiveSub(sub: ReactiveNode | undefined): void {
328-
activeSub = sub
329-
}

packages/reactivity/src/effectScope.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ export function getCurrentScope(): EffectScope | undefined {
113113
export function setCurrentScope(
114114
scope: EffectScope | undefined,
115115
): EffectScope | undefined {
116-
const prevScope = activeEffectScope
117-
activeEffectScope = scope
118-
return prevScope
116+
try {
117+
return activeEffectScope
118+
} finally {
119+
activeEffectScope = scope
120+
}
119121
}
120122

121123
/**

packages/reactivity/src/ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ 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 } from './effect'
1312
import {
1413
type Builtin,
1514
type ShallowReactiveMarker,
@@ -23,6 +22,7 @@ import {
2322
type Link,
2423
type ReactiveNode,
2524
ReactiveFlags as _ReactiveFlags,
25+
activeSub,
2626
batchDepth,
2727
link,
2828
processEffectNotifications,

packages/reactivity/src/system.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,21 @@ export const enum ReactiveFlags {
3939
const notifyBuffer: (Effect | undefined)[] = []
4040

4141
export let batchDepth = 0
42+
export let activeSub: ReactiveNode | undefined = undefined
43+
4244
let notifyIndex = 0
4345
let notifyBufferLength = 0
4446

47+
export function setActiveSub(
48+
sub: ReactiveNode | undefined,
49+
): ReactiveNode | undefined {
50+
try {
51+
return activeSub
52+
} finally {
53+
activeSub = sub
54+
}
55+
}
56+
4557
export function startBatch(): void {
4658
++batchDepth
4759
}
@@ -209,21 +221,26 @@ export function propagate(link: Link): void {
209221
} while (true)
210222
}
211223

212-
export function startTracking(sub: ReactiveNode): void {
224+
export function startTracking(sub: ReactiveNode): ReactiveNode | undefined {
213225
sub.depsTail = undefined
214226
sub.flags =
215227
(sub.flags &
216228
~(ReactiveFlags.Recursed | ReactiveFlags.Dirty | ReactiveFlags.Pending)) |
217229
ReactiveFlags.RecursedCheck
230+
return setActiveSub(sub)
218231
}
219232

220-
export function endTracking(sub: ReactiveNode): void {
233+
export function endTracking(
234+
sub: ReactiveNode,
235+
prevSub: ReactiveNode | undefined,
236+
): void {
221237
const depsTail = sub.depsTail
222238
let toRemove = depsTail !== undefined ? depsTail.nextDep : sub.deps
223239
while (toRemove !== undefined) {
224240
toRemove = unlink(toRemove, sub)
225241
}
226242
sub.flags &= ~ReactiveFlags.RecursedCheck
243+
activeSub = prevSub
227244
}
228245

229246
export function processEffectNotifications(): void {

packages/runtime-core/src/componentCurrentInstance.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ export const setCurrentInstance = (
8484
? instance.scope
8585
: undefined,
8686
): [GenericComponentInstance | null, EffectScope | undefined] => {
87-
const prev = currentInstance
88-
simpleSetCurrentInstance(instance)
89-
const prevScope = setCurrentScope(scope)
90-
return [prev, prevScope]
87+
try {
88+
return [currentInstance, setCurrentScope(scope)]
89+
} finally {
90+
simpleSetCurrentInstance(instance)
91+
}
9192
}

0 commit comments

Comments
 (0)