Skip to content

Commit 413084d

Browse files
committed
feat(runtime-vapor): add exposed relative property
1 parent 37df043 commit 413084d

File tree

2 files changed

+3657
-4188
lines changed

2 files changed

+3657
-4188
lines changed

Diff for: packages/runtime-vapor/src/component.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { EffectScope } from '@vue/reactivity'
2-
import { EMPTY_OBJ, NOOP, isFunction } from '@vue/shared'
1+
import { EffectScope, isRef } from '@vue/reactivity'
2+
import { EMPTY_OBJ, isArray, isFunction } from '@vue/shared'
33
import type { Block } from './apiRender'
44
import type { DirectiveBinding } from './directives'
55
import {
@@ -45,6 +45,30 @@ export type SetupContext<E = EmitsOptions> = E extends any
4545
export function createSetupContext(
4646
instance: ComponentInternalInstance,
4747
): SetupContext {
48+
const expose: SetupContext['expose'] = exposed => {
49+
if (__DEV__) {
50+
if (instance.exposed) {
51+
warn(`expose() should be called only once per setup().`)
52+
}
53+
if (exposed != null) {
54+
let exposedType: string = typeof exposed
55+
if (exposedType === 'object') {
56+
if (isArray(exposed)) {
57+
exposedType = 'array'
58+
} else if (isRef(exposed)) {
59+
exposedType = 'ref'
60+
}
61+
}
62+
if (exposedType !== 'object') {
63+
warn(
64+
`expose() should be passed a plain object, received ${exposedType}.`,
65+
)
66+
}
67+
}
68+
}
69+
instance.exposed = exposed || {}
70+
}
71+
4872
if (__DEV__) {
4973
// We use getters in dev in case libs like test-utils overwrite instance
5074
// properties (overwrites should not be done in prod)
@@ -58,7 +82,7 @@ export function createSetupContext(
5882
get emit() {
5983
return (event: string, ...args: any[]) => instance.emit(event, ...args)
6084
},
61-
expose: NOOP,
85+
expose,
6286
})
6387
} else {
6488
return {
@@ -67,7 +91,7 @@ export function createSetupContext(
6791
},
6892
emit: instance.emit,
6993
slots: instance.slots,
70-
expose: NOOP,
94+
expose,
7195
}
7296
}
7397
}
@@ -114,9 +138,12 @@ export interface ComponentInternalInstance {
114138
attrs: Data
115139
slots: InternalSlots
116140
refs: Data
141+
// exposed properties via expose()
142+
exposed: Record<string, any> | null
117143

118144
attrsProxy?: Data
119145
slotsProxy?: Slots
146+
exposeProxy: Record<string, any> | null
120147

121148
// lifecycle
122149
isMounted: boolean
@@ -238,6 +265,8 @@ export function createComponentInstance(
238265
attrs: EMPTY_OBJ,
239266
slots: EMPTY_OBJ,
240267
refs: EMPTY_OBJ,
268+
exposed: null,
269+
exposeProxy: null,
241270

242271
// lifecycle
243272
isMounted: false,

0 commit comments

Comments
 (0)