Skip to content

Commit

Permalink
refactor(runtime-core): safer currentInstance reset
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 8, 2024
1 parent dc91463 commit 7976f70
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
5 changes: 2 additions & 3 deletions packages/runtime-core/src/apiLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
currentInstance,
isInSSRComponentSetup,
setCurrentInstance,
unsetCurrentInstance,
} from './component'
import type { ComponentPublicInstance } from './componentPublicInstance'
import { ErrorTypeStrings, callWithAsyncErrorHandling } from './errorHandling'
Expand Down Expand Up @@ -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
})
Expand Down
10 changes: 2 additions & 8 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
currentInstance,
isInSSRComponentSetup,
setCurrentInstance,
unsetCurrentInstance,
} from './component'
import {
ErrorCodes,
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 9 additions & 4 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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,
Expand All @@ -797,7 +802,7 @@ function setupStatefulComponent(
],
)
resetTracking()
unsetCurrentInstance()
reset()

if (isPromise(setupResult)) {
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
Expand Down Expand Up @@ -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()
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
type ConcreteComponent,
type Data,
setCurrentInstance,
unsetCurrentInstance,
} from './component'
import { isEmitListener } from './componentEmits'
import { InternalObjectKey } from './vnode'
Expand Down Expand Up @@ -470,15 +469,15 @@ 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)
? createPropsDefaultThis(instance, props, key)
: null,
props,
)
unsetCurrentInstance()
reset()
}
} else {
value = defaultValue
Expand Down

0 comments on commit 7976f70

Please sign in to comment.