mirror of https://github.com/vuejs/core.git
refactor(runtime-core): safer currentInstance reset
This commit is contained in:
parent
dc91463eb6
commit
7976f7044e
|
@ -3,7 +3,6 @@ import {
|
|||
currentInstance,
|
||||
isInSSRComponentSetup,
|
||||
setCurrentInstance,
|
||||
unsetCurrentInstance,
|
||||
} from './component'
|
||||
import type { ComponentPublicInstance } from './componentPublicInstance'
|
||||
import { ErrorTypeStrings, callWithAsyncErrorHandling } from './errorHandling'
|
||||
|
@ -41,9 +40,9 @@ export function injectHook(
|
|||
// Set currentInstance during hook invocation.
|
||||
// This assumes the hook does not synchronously trigger other hooks, which
|
||||
// can only be false when the user does something really funky.
|
||||
setCurrentInstance(target)
|
||||
const reset = setCurrentInstance(target)
|
||||
const res = callWithAsyncErrorHandling(hook, target, type, args)
|
||||
unsetCurrentInstance()
|
||||
reset()
|
||||
resetTracking()
|
||||
return res
|
||||
})
|
||||
|
|
|
@ -30,7 +30,6 @@ import {
|
|||
currentInstance,
|
||||
isInSSRComponentSetup,
|
||||
setCurrentInstance,
|
||||
unsetCurrentInstance,
|
||||
} from './component'
|
||||
import {
|
||||
ErrorCodes,
|
||||
|
@ -448,14 +447,9 @@ export function instanceWatch(
|
|||
cb = value.handler as Function
|
||||
options = value
|
||||
}
|
||||
const cur = currentInstance
|
||||
setCurrentInstance(this)
|
||||
const reset = setCurrentInstance(this)
|
||||
const res = doWatch(getter, cb.bind(publicThis), options)
|
||||
if (cur) {
|
||||
setCurrentInstance(cur)
|
||||
} else {
|
||||
unsetCurrentInstance()
|
||||
}
|
||||
reset()
|
||||
return res
|
||||
}
|
||||
|
||||
|
|
|
@ -696,8 +696,13 @@ if (__SSR__) {
|
|||
}
|
||||
|
||||
export const setCurrentInstance = (instance: ComponentInternalInstance) => {
|
||||
const prev = currentInstance
|
||||
internalSetCurrentInstance(instance)
|
||||
instance.scope.on()
|
||||
return () => {
|
||||
instance.scope.off()
|
||||
internalSetCurrentInstance(prev)
|
||||
}
|
||||
}
|
||||
|
||||
export const unsetCurrentInstance = () => {
|
||||
|
@ -785,7 +790,7 @@ function setupStatefulComponent(
|
|||
const setupContext = (instance.setupContext =
|
||||
setup.length > 1 ? createSetupContext(instance) : null)
|
||||
|
||||
setCurrentInstance(instance)
|
||||
const reset = setCurrentInstance(instance)
|
||||
pauseTracking()
|
||||
const setupResult = callWithErrorHandling(
|
||||
setup,
|
||||
|
@ -797,7 +802,7 @@ function setupStatefulComponent(
|
|||
],
|
||||
)
|
||||
resetTracking()
|
||||
unsetCurrentInstance()
|
||||
reset()
|
||||
|
||||
if (isPromise(setupResult)) {
|
||||
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
|
||||
|
@ -972,13 +977,13 @@ export function finishComponentSetup(
|
|||
|
||||
// support for 2.x options
|
||||
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
|
||||
setCurrentInstance(instance)
|
||||
const reset = setCurrentInstance(instance)
|
||||
pauseTracking()
|
||||
try {
|
||||
applyOptions(instance)
|
||||
} finally {
|
||||
resetTracking()
|
||||
unsetCurrentInstance()
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@ import {
|
|||
type ConcreteComponent,
|
||||
type Data,
|
||||
setCurrentInstance,
|
||||
unsetCurrentInstance,
|
||||
} from './component'
|
||||
import { isEmitListener } from './componentEmits'
|
||||
import { InternalObjectKey } from './vnode'
|
||||
|
@ -470,7 +469,7 @@ function resolvePropValue(
|
|||
if (key in propsDefaults) {
|
||||
value = propsDefaults[key]
|
||||
} else {
|
||||
setCurrentInstance(instance)
|
||||
const reset = setCurrentInstance(instance)
|
||||
value = propsDefaults[key] = defaultValue.call(
|
||||
__COMPAT__ &&
|
||||
isCompatEnabled(DeprecationTypes.PROPS_DEFAULT_THIS, instance)
|
||||
|
@ -478,7 +477,7 @@ function resolvePropValue(
|
|||
: null,
|
||||
props,
|
||||
)
|
||||
unsetCurrentInstance()
|
||||
reset()
|
||||
}
|
||||
} else {
|
||||
value = defaultValue
|
||||
|
|
Loading…
Reference in New Issue