Skip to content

Commit

Permalink
feat(runtime-vapor): add exposed relative property
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Apr 19, 2024
1 parent 37df043 commit 413084d
Show file tree
Hide file tree
Showing 2 changed files with 3,657 additions and 4,188 deletions.
37 changes: 33 additions & 4 deletions packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EffectScope } from '@vue/reactivity'
import { EMPTY_OBJ, NOOP, isFunction } from '@vue/shared'
import { EffectScope, isRef } from '@vue/reactivity'
import { EMPTY_OBJ, isArray, isFunction } from '@vue/shared'
import type { Block } from './apiRender'
import type { DirectiveBinding } from './directives'
import {
Expand Down Expand Up @@ -45,6 +45,30 @@ export type SetupContext<E = EmitsOptions> = E extends any
export function createSetupContext(
instance: ComponentInternalInstance,
): SetupContext {
const expose: SetupContext['expose'] = exposed => {
if (__DEV__) {
if (instance.exposed) {
warn(`expose() should be called only once per setup().`)
}
if (exposed != null) {
let exposedType: string = typeof exposed
if (exposedType === 'object') {
if (isArray(exposed)) {
exposedType = 'array'
} else if (isRef(exposed)) {
exposedType = 'ref'
}
}
if (exposedType !== 'object') {
warn(
`expose() should be passed a plain object, received ${exposedType}.`,
)
}
}
}
instance.exposed = exposed || {}
}

if (__DEV__) {
// We use getters in dev in case libs like test-utils overwrite instance
// properties (overwrites should not be done in prod)
Expand All @@ -58,7 +82,7 @@ export function createSetupContext(
get emit() {
return (event: string, ...args: any[]) => instance.emit(event, ...args)
},
expose: NOOP,
expose,
})
} else {
return {
Expand All @@ -67,7 +91,7 @@ export function createSetupContext(
},
emit: instance.emit,
slots: instance.slots,
expose: NOOP,
expose,
}
}
}
Expand Down Expand Up @@ -114,9 +138,12 @@ export interface ComponentInternalInstance {
attrs: Data
slots: InternalSlots
refs: Data
// exposed properties via expose()
exposed: Record<string, any> | null

attrsProxy?: Data
slotsProxy?: Slots
exposeProxy: Record<string, any> | null

// lifecycle
isMounted: boolean
Expand Down Expand Up @@ -238,6 +265,8 @@ export function createComponentInstance(
attrs: EMPTY_OBJ,
slots: EMPTY_OBJ,
refs: EMPTY_OBJ,
exposed: null,
exposeProxy: null,

// lifecycle
isMounted: false,
Expand Down
Loading

0 comments on commit 413084d

Please sign in to comment.