Skip to content

Commit 7976f70

Browse files
committed
refactor(runtime-core): safer currentInstance reset
1 parent dc91463 commit 7976f70

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

packages/runtime-core/src/apiLifecycle.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
currentInstance,
44
isInSSRComponentSetup,
55
setCurrentInstance,
6-
unsetCurrentInstance,
76
} from './component'
87
import type { ComponentPublicInstance } from './componentPublicInstance'
98
import { ErrorTypeStrings, callWithAsyncErrorHandling } from './errorHandling'
@@ -41,9 +40,9 @@ export function injectHook(
4140
// Set currentInstance during hook invocation.
4241
// This assumes the hook does not synchronously trigger other hooks, which
4342
// can only be false when the user does something really funky.
44-
setCurrentInstance(target)
43+
const reset = setCurrentInstance(target)
4544
const res = callWithAsyncErrorHandling(hook, target, type, args)
46-
unsetCurrentInstance()
45+
reset()
4746
resetTracking()
4847
return res
4948
})

packages/runtime-core/src/apiWatch.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
currentInstance,
3131
isInSSRComponentSetup,
3232
setCurrentInstance,
33-
unsetCurrentInstance,
3433
} from './component'
3534
import {
3635
ErrorCodes,
@@ -448,14 +447,9 @@ export function instanceWatch(
448447
cb = value.handler as Function
449448
options = value
450449
}
451-
const cur = currentInstance
452-
setCurrentInstance(this)
450+
const reset = setCurrentInstance(this)
453451
const res = doWatch(getter, cb.bind(publicThis), options)
454-
if (cur) {
455-
setCurrentInstance(cur)
456-
} else {
457-
unsetCurrentInstance()
458-
}
452+
reset()
459453
return res
460454
}
461455

packages/runtime-core/src/component.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,13 @@ if (__SSR__) {
696696
}
697697

698698
export const setCurrentInstance = (instance: ComponentInternalInstance) => {
699+
const prev = currentInstance
699700
internalSetCurrentInstance(instance)
700701
instance.scope.on()
702+
return () => {
703+
instance.scope.off()
704+
internalSetCurrentInstance(prev)
705+
}
701706
}
702707

703708
export const unsetCurrentInstance = () => {
@@ -785,7 +790,7 @@ function setupStatefulComponent(
785790
const setupContext = (instance.setupContext =
786791
setup.length > 1 ? createSetupContext(instance) : null)
787792

788-
setCurrentInstance(instance)
793+
const reset = setCurrentInstance(instance)
789794
pauseTracking()
790795
const setupResult = callWithErrorHandling(
791796
setup,
@@ -797,7 +802,7 @@ function setupStatefulComponent(
797802
],
798803
)
799804
resetTracking()
800-
unsetCurrentInstance()
805+
reset()
801806

802807
if (isPromise(setupResult)) {
803808
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
@@ -972,13 +977,13 @@ export function finishComponentSetup(
972977

973978
// support for 2.x options
974979
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
975-
setCurrentInstance(instance)
980+
const reset = setCurrentInstance(instance)
976981
pauseTracking()
977982
try {
978983
applyOptions(instance)
979984
} finally {
980985
resetTracking()
981-
unsetCurrentInstance()
986+
reset()
982987
}
983988
}
984989

packages/runtime-core/src/componentProps.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
type ConcreteComponent,
3333
type Data,
3434
setCurrentInstance,
35-
unsetCurrentInstance,
3635
} from './component'
3736
import { isEmitListener } from './componentEmits'
3837
import { InternalObjectKey } from './vnode'
@@ -470,15 +469,15 @@ function resolvePropValue(
470469
if (key in propsDefaults) {
471470
value = propsDefaults[key]
472471
} else {
473-
setCurrentInstance(instance)
472+
const reset = setCurrentInstance(instance)
474473
value = propsDefaults[key] = defaultValue.call(
475474
__COMPAT__ &&
476475
isCompatEnabled(DeprecationTypes.PROPS_DEFAULT_THIS, instance)
477476
? createPropsDefaultThis(instance, props, key)
478477
: null,
479478
props,
480479
)
481-
unsetCurrentInstance()
480+
reset()
482481
}
483482
} else {
484483
value = defaultValue

0 commit comments

Comments
 (0)