]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(componentProxy): simplify proxy handling (#214)
authorDmitry Sharshakov <d3dx12.xx@gmail.com>
Sat, 12 Oct 2019 23:44:30 +0000 (02:44 +0300)
committerEvan You <yyx990803@gmail.com>
Sat, 12 Oct 2019 23:44:30 +0000 (19:44 -0400)
packages/runtime-core/src/componentProxy.ts

index 01324d2c0eeadf0306139a657b86f200a3eddbeb..332b645d60995b8c1836247f9e01f5e5099e1507 100644 (file)
@@ -36,6 +36,18 @@ export type ComponentPublicInstance<
   ExtractComputedReturns<C> &
   M
 
+const publicPropertiesMap = {
+  $data: 'data',
+  $props: 'propsProxy',
+  $attrs: 'attrs',
+  $slots: 'slots',
+  $refs: 'refs',
+  $parent: 'parent',
+  $root: 'root',
+  $emit: 'emit',
+  $options: 'type'
+}
+
 export const PublicInstanceProxyHandlers = {
   get(target: ComponentInternalInstance, key: string) {
     const { renderContext, data, props, propsProxy } = target
@@ -46,44 +58,23 @@ export const PublicInstanceProxyHandlers = {
     } else if (hasOwn(props, key)) {
       // return the value from propsProxy for ref unwrapping and readonly
       return propsProxy![key]
-    } else {
-      // TODO simplify this?
+    } else if (key === '$el') {
+      return target.vnode.el
+    } else if (hasOwn(publicPropertiesMap, key)) {
+      return target[publicPropertiesMap[key]]
+    }
+    // methods are only exposed when options are supported
+    if (__FEATURE_OPTIONS__) {
       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
-        case '$options':
-          return target.type
-        default:
-          // methods are only exposed when options are supported
-          if (__FEATURE_OPTIONS__) {
-            switch (key) {
-              case '$forceUpdate':
-                return target.update
-              case '$nextTick':
-                return nextTick
-              case '$watch':
-                return instanceWatch.bind(target)
-            }
-          }
-          return target.user[key]
+        case '$forceUpdate':
+          return target.update
+        case '$nextTick':
+          return nextTick
+        case '$watch':
+          return instanceWatch.bind(target)
       }
     }
+    return target.user[key]
   },
   // this trap is only called in browser-compiled render functions that use
   // `with (this) {}`