Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 9daf90e

Browse files
committed
refactor(runtime-core): extract getComponentPublicInstance helper
1 parent b7f6c54 commit 9daf90e

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
type ComponentInternalInstance,
44
type ConcreteComponent,
55
type Data,
6-
getExposeProxy,
6+
getComponentPublicInstance,
77
validateComponentName,
88
} from './component'
99
import type {
@@ -358,7 +358,7 @@ export function createAppAPI<HostElement>(
358358
devtoolsInitApp(app, version)
359359
}
360360

361-
return getExposeProxy(vnode.component!) || vnode.component!.proxy
361+
return getComponentPublicInstance(vnode.component!)
362362
} else if (__DEV__) {
363363
warn(
364364
`App has already been mounted.\n` +

packages/runtime-core/src/component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ export function createComponentInstance(
566566
exposed: null,
567567
exposeProxy: null,
568568
withProxy: null,
569+
569570
provides: parent ? parent.provides : Object.create(appContext.provides),
570571
accessCache: null!,
571572
renderCache: [],
@@ -1107,7 +1108,9 @@ export function createSetupContext(
11071108
}
11081109
}
11091110

1110-
export function getExposeProxy(instance: ComponentInternalInstance) {
1111+
export function getComponentPublicInstance(
1112+
instance: ComponentInternalInstance,
1113+
) {
11111114
if (instance.exposed) {
11121115
return (
11131116
instance.exposeProxy ||
@@ -1124,6 +1127,8 @@ export function getExposeProxy(instance: ComponentInternalInstance) {
11241127
},
11251128
}))
11261129
)
1130+
} else {
1131+
return instance.proxy
11271132
}
11281133
}
11291134

packages/runtime-core/src/componentPublicInstance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
type ComponentInternalInstance,
33
type Data,
4-
getExposeProxy,
4+
getComponentPublicInstance,
55
isStatefulComponent,
66
} from './component'
77
import { nextTick, queueJob } from './scheduler'
@@ -256,7 +256,7 @@ const getPublicInstance = (
256256
i: ComponentInternalInstance | null,
257257
): ComponentPublicInstance | ComponentInternalInstance['exposed'] | null => {
258258
if (!i) return null
259-
if (isStatefulComponent(i)) return getExposeProxy(i) || i.proxy
259+
if (isStatefulComponent(i)) return getComponentPublicInstance(i)
260260
return getPublicInstance(i.parent)
261261
}
262262

packages/runtime-core/src/directives.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { warn } from './warning'
1717
import {
1818
type ComponentInternalInstance,
1919
type Data,
20-
getExposeProxy,
20+
getComponentPublicInstance,
2121
} from './component'
2222
import { currentRenderingInstance } from './componentRenderContext'
2323
import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
@@ -27,7 +27,7 @@ import { pauseTracking, resetTracking } from '@vue/reactivity'
2727
import { traverse } from './apiWatch'
2828

2929
export interface DirectiveBinding<V = any> {
30-
instance: ComponentPublicInstance | null
30+
instance: ComponentPublicInstance | Record<string, any> | null
3131
value: V
3232
oldValue: V | null
3333
arg?: string
@@ -92,9 +92,7 @@ export function withDirectives<T extends VNode>(
9292
__DEV__ && warn(`withDirectives can only be used inside render functions.`)
9393
return vnode
9494
}
95-
const instance =
96-
(getExposeProxy(currentRenderingInstance) as ComponentPublicInstance) ||
97-
currentRenderingInstance.proxy
95+
const instance = getComponentPublicInstance(currentRenderingInstance)
9896
const bindings: DirectiveBinding[] = vnode.dirs || (vnode.dirs = [])
9997
for (let i = 0; i < directives.length; i++) {
10098
let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i]

packages/runtime-core/src/rendererTemplateRef.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {
1010
remove,
1111
} from '@vue/shared'
1212
import { isAsyncWrapper } from './apiAsyncComponent'
13-
import { getExposeProxy } from './component'
1413
import { warn } from './warning'
1514
import { isRef } from '@vue/reactivity'
1615
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
1716
import type { SchedulerJob } from './scheduler'
1817
import { queuePostRenderEffect } from './renderer'
18+
import { getComponentPublicInstance } from './component'
1919

2020
/**
2121
* Function for handling a template ref
@@ -48,7 +48,7 @@ export function setRef(
4848

4949
const refValue =
5050
vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
51-
? getExposeProxy(vnode.component!) || vnode.component!.proxy
51+
? getComponentPublicInstance(vnode.component!)
5252
: vnode.el
5353
const value = isUnmount ? null : refValue
5454

0 commit comments

Comments
 (0)