Skip to content

Commit 2ca8278

Browse files
committed
wip
1 parent 481df0e commit 2ca8278

File tree

12 files changed

+78
-78
lines changed

12 files changed

+78
-78
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
onScopeDispose,
1010
reactive,
1111
ref,
12+
setCurrentScope,
1213
} from '../src'
1314

1415
describe('reactivity/effect/scope', () => {
@@ -289,8 +290,7 @@ describe('reactivity/effect/scope', () => {
289290

290291
parentScope.run(() => {
291292
const childScope = effectScope(true)
292-
childScope.on()
293-
childScope.off()
293+
setCurrentScope(setCurrentScope(childScope))
294294
expect(getCurrentScope()).toBe(parentScope)
295295
})
296296
})
@@ -299,11 +299,11 @@ describe('reactivity/effect/scope', () => {
299299
const parentScope = effectScope()
300300
parentScope.run(() => {
301301
const childScope = effectScope(true)
302-
childScope.on()
303-
childScope.on()
304-
childScope.off()
305-
childScope.off()
306-
childScope.off()
302+
const prevScope1 = setCurrentScope(childScope)
303+
const prevScope2 = setCurrentScope(childScope)
304+
setCurrentScope(prevScope2)
305+
setCurrentScope(prevScope1)
306+
setCurrentScope(prevScope1)
307307
expect(getCurrentScope()).toBe(parentScope)
308308
})
309309
})

packages/reactivity/src/effectScope.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ export class EffectScope implements Subscriber, Dependency {
2020
subs: Link | undefined = undefined
2121
subsTail: Link | undefined = undefined
2222

23-
/**
24-
* @internal track `on` calls, allow `on` call multiple times
25-
*/
26-
private _on = 0
2723
/**
2824
* @internal
2925
*/
@@ -87,29 +83,6 @@ export class EffectScope implements Subscriber, Dependency {
8783
}
8884
}
8985

90-
prevScope: EffectScope | undefined
91-
/**
92-
* This should only be called on non-detached scopes
93-
* @internal
94-
*/
95-
on(): void {
96-
if (++this._on === 1) {
97-
this.prevScope = activeEffectScope
98-
activeEffectScope = this
99-
}
100-
}
101-
102-
/**
103-
* This should only be called on non-detached scopes
104-
* @internal
105-
*/
106-
off(): void {
107-
if (this._on > 0 && --this._on === 0) {
108-
activeEffectScope = this.prevScope
109-
this.prevScope = undefined
110-
}
111-
}
112-
11386
stop(): void {
11487
if (this.active) {
11588
this.flags |= EffectFlags.STOP
@@ -150,6 +123,14 @@ export function getCurrentScope(): EffectScope | undefined {
150123
return activeEffectScope
151124
}
152125

126+
export function setCurrentScope(
127+
scope: EffectScope | undefined,
128+
): EffectScope | undefined {
129+
const prevScope = activeEffectScope
130+
activeEffectScope = scope
131+
return prevScope
132+
}
133+
153134
/**
154135
* Registers a dispose callback on the current active effect scope. The
155136
* callback will be invoked when the associated effect scope is stopped.

packages/reactivity/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export {
7676
effectScope,
7777
EffectScope,
7878
getCurrentScope,
79+
setCurrentScope,
80+
/**
81+
* @internal
82+
*/
7983
onScopeDispose,
8084
} from './effectScope'
8185
export { reactiveReadArray, shallowReadArray } from './arrayInstrumentations'

packages/runtime-core/src/apiSetupHelpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import {
1212
type ComponentInternalInstance,
1313
type SetupContext,
1414
createSetupContext,
15+
currentInstance,
1516
getCurrentGenericInstance,
1617
setCurrentInstance,
17-
unsetCurrentInstance,
18+
simpleSetCurrentInstance,
1819
} from './component'
1920
import type { EmitFn, EmitsOptions, ObjectEmitsOptions } from './componentEmits'
2021
import type {
@@ -31,7 +32,7 @@ import type {
3132
} from './componentProps'
3233
import { warn } from './warning'
3334
import type { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
34-
import type { Ref } from '@vue/reactivity'
35+
import { type Ref, setCurrentScope } from '@vue/reactivity'
3536

3637
// dev only
3738
const warnRuntimeUsage = (method: string) =>
@@ -511,7 +512,8 @@ export function withAsyncContext(getAwaitable: () => any): [any, () => void] {
511512
)
512513
}
513514
let awaitable = getAwaitable()
514-
unsetCurrentInstance()
515+
currentInstance && setCurrentScope(currentInstance.scope)
516+
simpleSetCurrentInstance(null)
515517
if (isPromise(awaitable)) {
516518
awaitable = awaitable.catch(e => {
517519
setCurrentInstance(ctx)

packages/runtime-core/src/component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
pauseTracking,
99
proxyRefs,
1010
resetTracking,
11+
setCurrentScope,
1112
shallowReadonly,
1213
track,
1314
} from '@vue/reactivity'
@@ -97,7 +98,7 @@ import type { RendererElement } from './renderer'
9798
import {
9899
setCurrentInstance,
99100
setInSSRSetupState,
100-
unsetCurrentInstance,
101+
simpleSetCurrentInstance,
101102
} from './componentCurrentInstance'
102103

103104
export * from './componentCurrentInstance'
@@ -911,6 +912,10 @@ function setupStatefulComponent(
911912
}
912913

913914
if (isAsyncSetup) {
915+
const unsetCurrentInstance = (): void => {
916+
setCurrentScope(undefined)
917+
simpleSetCurrentInstance(null)
918+
}
914919
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
915920
if (isSSR) {
916921
// return the promise so server-renderer can wait on it

packages/runtime-core/src/componentCurrentInstance.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
GenericComponentInstance,
55
} from './component'
66
import { currentRenderingInstance } from './componentRenderContext'
7+
import { setCurrentScope } from '@vue/reactivity'
78

89
/**
910
* @internal
@@ -25,7 +26,10 @@ export let isInSSRComponentSetup = false
2526

2627
export let setInSSRSetupState: (state: boolean) => void
2728

28-
let internalSetCurrentInstance: (
29+
/**
30+
* @internal
31+
*/
32+
export let simpleSetCurrentInstance: (
2933
instance: GenericComponentInstance | null,
3034
) => void
3135

@@ -53,7 +57,7 @@ if (__SSR__) {
5357
else setters[0](v)
5458
}
5559
}
56-
internalSetCurrentInstance = registerGlobalSetter(
60+
simpleSetCurrentInstance = registerGlobalSetter(
5761
`__VUE_INSTANCE_SETTERS__`,
5862
v => (currentInstance = v),
5963
)
@@ -66,7 +70,7 @@ if (__SSR__) {
6670
v => (isInSSRComponentSetup = v),
6771
)
6872
} else {
69-
internalSetCurrentInstance = i => {
73+
simpleSetCurrentInstance = i => {
7074
currentInstance = i
7175
}
7276
setInSSRSetupState = v => {
@@ -76,32 +80,10 @@ if (__SSR__) {
7680

7781
export const setCurrentInstance = (instance: GenericComponentInstance) => {
7882
const prev = currentInstance
79-
internalSetCurrentInstance(instance)
80-
instance.scope.on()
83+
simpleSetCurrentInstance(instance)
84+
const prevScope = setCurrentScope(instance.scope)
8185
return (): void => {
82-
instance.scope.off()
83-
internalSetCurrentInstance(prev)
84-
}
85-
}
86-
87-
export const unsetCurrentInstance = (): void => {
88-
currentInstance && currentInstance.scope.off()
89-
internalSetCurrentInstance(null)
90-
}
91-
92-
/**
93-
* Exposed for vapor only. Vapor never runs during SSR so we don't want to pay
94-
* for the extra overhead
95-
* @internal
96-
*/
97-
export const simpleSetCurrentInstance = (
98-
i: GenericComponentInstance | null,
99-
unset?: GenericComponentInstance | null,
100-
): void => {
101-
currentInstance = i
102-
if (unset) {
103-
unset.scope.off()
104-
} else if (i) {
105-
i.scope.on()
86+
setCurrentScope(prevScope)
87+
simpleSetCurrentInstance(prev)
10688
}
10789
}

packages/runtime-core/src/renderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
ReactiveEffect,
6060
pauseTracking,
6161
resetTracking,
62+
setCurrentScope,
6263
} from '@vue/reactivity'
6364
import { updateProps } from './componentProps'
6465
import { updateSlots } from './componentSlots'
@@ -1594,9 +1595,9 @@ function baseCreateRenderer(
15941595
}
15951596

15961597
// create reactive effect for rendering
1597-
instance.scope.on()
1598+
const prevScope = setCurrentScope(instance.scope)
15981599
const effect = (instance.effect = new ReactiveEffect(componentUpdateFn))
1599-
instance.scope.off()
1600+
setCurrentScope(prevScope)
16001601

16011602
const update = (instance.update = effect.run.bind(effect))
16021603
const job: SchedulerJob = (instance.job = effect.scheduler.bind(effect))

packages/runtime-vapor/__tests__/dom/prop.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ import {
1717
ref,
1818
simpleSetCurrentInstance,
1919
} from '@vue/runtime-dom'
20+
import { setCurrentScope } from '@vue/reactivity'
2021

2122
let removeComponentInstance = NOOP
2223
beforeEach(() => {
2324
const instance = new VaporComponentInstance({}, {}, null)
2425
const prev = currentInstance
2526
simpleSetCurrentInstance(instance)
27+
const prevScope = setCurrentScope(instance.scope)
2628
removeComponentInstance = () => {
29+
setCurrentScope(prevScope)
2730
simpleSetCurrentInstance(prev)
2831
}
2932
})

packages/runtime-vapor/src/component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
pauseTracking,
3434
proxyRefs,
3535
resetTracking,
36+
setCurrentScope,
3637
unref,
3738
} from '@vue/reactivity'
3839
import { EMPTY_OBJ, invokeArrayFns, isFunction, isString } from '@vue/shared'
@@ -193,6 +194,7 @@ export function createComponent(
193194

194195
const prev = currentInstance
195196
simpleSetCurrentInstance(instance)
197+
const prevScope = setCurrentScope(instance.scope)
196198
pauseTracking()
197199

198200
if (__DEV__) {
@@ -260,7 +262,8 @@ export function createComponent(
260262
}
261263

262264
resetTracking()
263-
simpleSetCurrentInstance(prev, instance)
265+
setCurrentScope(prevScope)
266+
simpleSetCurrentInstance(prev)
264267

265268
if (__DEV__) {
266269
popWarningContext()

packages/runtime-vapor/src/componentProps.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '@vue/runtime-dom'
2424
import { normalizeEmitsOptions } from './componentEmits'
2525
import { renderEffect } from './renderEffect'
26+
import { setCurrentScope } from '@vue/reactivity'
2627

2728
export type RawProps = Record<string, () => unknown> & {
2829
// generated by compiler for :[key]="x" or v-bind="x"
@@ -259,8 +260,10 @@ function resolveDefault(
259260
) {
260261
const prev = currentInstance
261262
simpleSetCurrentInstance(instance)
263+
const prevScope = setCurrentScope(instance.scope)
262264
const res = factory.call(null, instance.props)
263-
simpleSetCurrentInstance(prev, instance)
265+
setCurrentScope(prevScope)
266+
simpleSetCurrentInstance(prev)
264267
return res
265268
}
266269

packages/runtime-vapor/src/hmr.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
mountComponent,
1414
unmountComponent,
1515
} from './component'
16+
import { setCurrentScope } from '@vue/reactivity'
1617

1718
export function hmrRerender(instance: VaporComponentInstance): void {
1819
const normalized = normalizeBlock(instance.block)
@@ -21,10 +22,12 @@ export function hmrRerender(instance: VaporComponentInstance): void {
2122
remove(instance.block, parent)
2223
const prev = currentInstance
2324
simpleSetCurrentInstance(instance)
25+
const prevScope = setCurrentScope(instance.scope)
2426
pushWarningContext(instance)
2527
devRender(instance)
2628
popWarningContext()
27-
simpleSetCurrentInstance(prev, instance)
29+
setCurrentScope(prevScope)
30+
simpleSetCurrentInstance(prev)
2831
insert(instance.block, parent, anchor)
2932
}
3033

@@ -38,12 +41,19 @@ export function hmrReload(
3841
unmountComponent(instance, parent)
3942
const prev = currentInstance
4043
simpleSetCurrentInstance(instance.parent)
44+
let prevScope: any
45+
if (instance.parent !== null) {
46+
prevScope = setCurrentScope(instance.parent.scope)
47+
}
4148
const newInstance = createComponent(
4249
newComp,
4350
instance.rawProps,
4451
instance.rawSlots,
4552
instance.isSingleRoot,
4653
)
47-
simpleSetCurrentInstance(prev, instance.parent)
54+
if (instance.parent !== null) {
55+
setCurrentScope(prevScope)
56+
}
57+
simpleSetCurrentInstance(prev)
4858
mountComponent(newInstance, parent, anchor)
4959
}

0 commit comments

Comments
 (0)