refactor(componentProxy): simplify proxy handling (#214)

This commit is contained in:
Dmitry Sharshakov 2019-10-13 02:44:30 +03:00 committed by Evan You
parent 5d98c51b74
commit 78d1821e24

View File

@ -36,6 +36,18 @@ export type ComponentPublicInstance<
ExtractComputedReturns<C> & ExtractComputedReturns<C> &
M M
const publicPropertiesMap = {
$data: 'data',
$props: 'propsProxy',
$attrs: 'attrs',
$slots: 'slots',
$refs: 'refs',
$parent: 'parent',
$root: 'root',
$emit: 'emit',
$options: 'type'
}
export const PublicInstanceProxyHandlers = { export const PublicInstanceProxyHandlers = {
get(target: ComponentInternalInstance, key: string) { get(target: ComponentInternalInstance, key: string) {
const { renderContext, data, props, propsProxy } = target const { renderContext, data, props, propsProxy } = target
@ -46,30 +58,11 @@ export const PublicInstanceProxyHandlers = {
} else if (hasOwn(props, key)) { } else if (hasOwn(props, key)) {
// return the value from propsProxy for ref unwrapping and readonly // return the value from propsProxy for ref unwrapping and readonly
return propsProxy![key] return propsProxy![key]
} else { } else if (key === '$el') {
// TODO simplify this?
switch (key) {
case '$data':
return data
case '$props':
return propsProxy
case '$attrs':
return target.attrs
case '$slots':
return target.slots
case '$refs':
return target.refs
case '$parent':
return target.parent
case '$root':
return target.root
case '$emit':
return target.emit
case '$el':
return target.vnode.el return target.vnode.el
case '$options': } else if (hasOwn(publicPropertiesMap, key)) {
return target.type return target[publicPropertiesMap[key]]
default: }
// methods are only exposed when options are supported // methods are only exposed when options are supported
if (__FEATURE_OPTIONS__) { if (__FEATURE_OPTIONS__) {
switch (key) { switch (key) {
@ -82,8 +75,6 @@ export const PublicInstanceProxyHandlers = {
} }
} }
return target.user[key] return target.user[key]
}
}
}, },
// this trap is only called in browser-compiled render functions that use // this trap is only called in browser-compiled render functions that use
// `with (this) {}` // `with (this) {}`