refactor(runtime-core): extract getComponentPublicInstance helper

This commit is contained in:
Evan You 2024-06-10 17:37:32 +08:00
parent b7f6c54f67
commit 9daf90e29f
No known key found for this signature in database
GPG Key ID: B9D421896CA450FB
5 changed files with 15 additions and 12 deletions

View File

@ -3,7 +3,7 @@ import {
type ComponentInternalInstance, type ComponentInternalInstance,
type ConcreteComponent, type ConcreteComponent,
type Data, type Data,
getExposeProxy, getComponentPublicInstance,
validateComponentName, validateComponentName,
} from './component' } from './component'
import type { import type {
@ -358,7 +358,7 @@ export function createAppAPI<HostElement>(
devtoolsInitApp(app, version) devtoolsInitApp(app, version)
} }
return getExposeProxy(vnode.component!) || vnode.component!.proxy return getComponentPublicInstance(vnode.component!)
} else if (__DEV__) { } else if (__DEV__) {
warn( warn(
`App has already been mounted.\n` + `App has already been mounted.\n` +

View File

@ -566,6 +566,7 @@ export function createComponentInstance(
exposed: null, exposed: null,
exposeProxy: null, exposeProxy: null,
withProxy: null, withProxy: null,
provides: parent ? parent.provides : Object.create(appContext.provides), provides: parent ? parent.provides : Object.create(appContext.provides),
accessCache: null!, accessCache: null!,
renderCache: [], renderCache: [],
@ -1107,7 +1108,9 @@ export function createSetupContext(
} }
} }
export function getExposeProxy(instance: ComponentInternalInstance) { export function getComponentPublicInstance(
instance: ComponentInternalInstance,
) {
if (instance.exposed) { if (instance.exposed) {
return ( return (
instance.exposeProxy || instance.exposeProxy ||
@ -1124,6 +1127,8 @@ export function getExposeProxy(instance: ComponentInternalInstance) {
}, },
})) }))
) )
} else {
return instance.proxy
} }
} }

View File

@ -1,7 +1,7 @@
import { import {
type ComponentInternalInstance, type ComponentInternalInstance,
type Data, type Data,
getExposeProxy, getComponentPublicInstance,
isStatefulComponent, isStatefulComponent,
} from './component' } from './component'
import { nextTick, queueJob } from './scheduler' import { nextTick, queueJob } from './scheduler'
@ -256,7 +256,7 @@ const getPublicInstance = (
i: ComponentInternalInstance | null, i: ComponentInternalInstance | null,
): ComponentPublicInstance | ComponentInternalInstance['exposed'] | null => { ): ComponentPublicInstance | ComponentInternalInstance['exposed'] | null => {
if (!i) return null if (!i) return null
if (isStatefulComponent(i)) return getExposeProxy(i) || i.proxy if (isStatefulComponent(i)) return getComponentPublicInstance(i)
return getPublicInstance(i.parent) return getPublicInstance(i.parent)
} }

View File

@ -17,7 +17,7 @@ import { warn } from './warning'
import { import {
type ComponentInternalInstance, type ComponentInternalInstance,
type Data, type Data,
getExposeProxy, getComponentPublicInstance,
} from './component' } from './component'
import { currentRenderingInstance } from './componentRenderContext' import { currentRenderingInstance } from './componentRenderContext'
import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling' import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
@ -27,7 +27,7 @@ import { pauseTracking, resetTracking } from '@vue/reactivity'
import { traverse } from './apiWatch' import { traverse } from './apiWatch'
export interface DirectiveBinding<V = any> { export interface DirectiveBinding<V = any> {
instance: ComponentPublicInstance | null instance: ComponentPublicInstance | Record<string, any> | null
value: V value: V
oldValue: V | null oldValue: V | null
arg?: string arg?: string
@ -92,9 +92,7 @@ export function withDirectives<T extends VNode>(
__DEV__ && warn(`withDirectives can only be used inside render functions.`) __DEV__ && warn(`withDirectives can only be used inside render functions.`)
return vnode return vnode
} }
const instance = const instance = getComponentPublicInstance(currentRenderingInstance)
(getExposeProxy(currentRenderingInstance) as ComponentPublicInstance) ||
currentRenderingInstance.proxy
const bindings: DirectiveBinding[] = vnode.dirs || (vnode.dirs = []) const bindings: DirectiveBinding[] = vnode.dirs || (vnode.dirs = [])
for (let i = 0; i < directives.length; i++) { for (let i = 0; i < directives.length; i++) {
let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i] let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i]

View File

@ -10,12 +10,12 @@ import {
remove, remove,
} from '@vue/shared' } from '@vue/shared'
import { isAsyncWrapper } from './apiAsyncComponent' import { isAsyncWrapper } from './apiAsyncComponent'
import { getExposeProxy } from './component'
import { warn } from './warning' import { warn } from './warning'
import { isRef } from '@vue/reactivity' import { isRef } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling' import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import type { SchedulerJob } from './scheduler' import type { SchedulerJob } from './scheduler'
import { queuePostRenderEffect } from './renderer' import { queuePostRenderEffect } from './renderer'
import { getComponentPublicInstance } from './component'
/** /**
* Function for handling a template ref * Function for handling a template ref
@ -48,7 +48,7 @@ export function setRef(
const refValue = const refValue =
vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
? getExposeProxy(vnode.component!) || vnode.component!.proxy ? getComponentPublicInstance(vnode.component!)
: vnode.el : vnode.el
const value = isUnmount ? null : refValue const value = isUnmount ? null : refValue