vue3-yuanma/packages/runtime-core/src/componentProxy.ts

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-05-29 13:43:46 +08:00
import { ComponentInstance } from './component'
2019-09-04 11:04:11 +08:00
import { nextTick } from './scheduler'
2019-09-04 23:36:27 +08:00
import { instanceWatch } from './apiWatch'
import { EMPTY_OBJ } from '@vue/shared'
2019-05-29 13:43:46 +08:00
export const RenderProxyHandlers = {
get(target: ComponentInstance, key: string) {
const { renderContext, data, props, propsProxy } = target
if (data !== EMPTY_OBJ && data.hasOwnProperty(key)) {
2019-06-19 17:08:42 +08:00
return data[key]
} else if (renderContext.hasOwnProperty(key)) {
return renderContext[key]
2019-05-29 13:43:46 +08:00
} else if (props.hasOwnProperty(key)) {
// return the value from propsProxy for ref unwrapping and readonly
return (propsProxy as any)[key]
2019-05-29 13:43:46 +08:00
} else {
switch (key) {
2019-06-19 17:08:42 +08:00
case '$data':
return data
2019-05-29 13:43:46 +08:00
case '$props':
return propsProxy
2019-05-29 13:43:46 +08:00
case '$attrs':
return target.attrs
case '$slots':
return target.slots
case '$refs':
return target.refs
2019-06-03 13:44:45 +08:00
case '$parent':
return target.parent
case '$root':
return target.root
2019-06-19 16:43:34 +08:00
case '$emit':
return target.emit
2019-09-04 11:04:11 +08:00
case '$el':
return target.vnode.el
case '$options':
return target.type
2019-05-29 13:43:46 +08:00
default:
2019-09-04 11:04:11 +08:00
// methods are only exposed when options are supported
if (__FEATURE_OPTIONS__) {
switch (key) {
case '$forceUpdate':
return target.update
case '$nextTick':
return nextTick
case '$watch':
2019-09-04 23:36:27 +08:00
return instanceWatch.bind(target)
2019-09-04 11:04:11 +08:00
}
}
2019-08-21 21:50:20 +08:00
return target.user[key]
2019-05-29 13:43:46 +08:00
}
}
},
set(target: ComponentInstance, key: string, value: any): boolean {
const { data, renderContext } = target
if (data !== EMPTY_OBJ && data.hasOwnProperty(key)) {
2019-06-19 17:08:42 +08:00
data[key] = value
} else if (renderContext.hasOwnProperty(key)) {
renderContext[key] = value
2019-08-21 21:50:20 +08:00
} else if (key[0] === '$' && key.slice(1) in target) {
// TODO warn attempt of mutating public property
return false
} else if (key in target.props) {
// TODO warn attempt of mutating prop
return false
2019-05-29 13:43:46 +08:00
} else {
2019-08-21 21:50:20 +08:00
target.user[key] = value
2019-05-29 13:43:46 +08:00
}
return true
2019-05-29 13:43:46 +08:00
}
}