Skip to content
Permalink
Browse files
refactor: simplify
  • Loading branch information
ktsn committed Oct 9, 2018
1 parent 2e7291e commit 42005ad10001d504c830566f19e44220d14fd039
Showing with 33 additions and 51 deletions.
  1. +5 −23 src/component.ts
  2. +28 −28 src/reflect.ts
@@ -1,5 +1,5 @@
import Vue, { ComponentOptions } from 'vue'
import { copyReflectionMetadata, reflectionIsSupported, ReflectionMap } from './reflect'
import { copyReflectionMetadata, reflectionIsSupported } from './reflect'
import { VueClass, DecoratedClass } from './declarations'
import { collectDataFromConstructor } from './data'
import { hasProto, isPrimitive, warn } from './util'
@@ -24,15 +24,6 @@ export function componentFactory (
Component: VueClass<Vue>,
options: ComponentOptions<Vue> = {}
): VueClass<Vue> {
const reflectionMap: ReflectionMap = {
instance: {},
static: {},
constructor: []
}

if (reflectionIsSupported()) {
reflectionMap.constructor = Reflect.getOwnMetadataKeys(Component)
}
options.name = options.name || (Component as any)._componentTag || (Component as any).name
// prototype props.
const proto = Component.prototype
@@ -41,10 +32,6 @@ export function componentFactory (
return
}

if (reflectionIsSupported()) {
reflectionMap.instance[key] = Reflect.getOwnMetadataKeys(proto, key)
}

// hooks
if ($internalHooks.indexOf(key) > -1) {
options[key] = proto[key]
@@ -84,10 +71,10 @@ export function componentFactory (
: Vue
const Extended = Super.extend(options)

forwardStaticMembersAndCollectReflection(Extended, Component, Super, reflectionMap)
forwardStaticMembers(Extended, Component, Super)

if (reflectionIsSupported()) {
copyReflectionMetadata(Component, Extended, reflectionMap)
copyReflectionMetadata(Extended, Component)
}

return Extended
@@ -112,11 +99,10 @@ const reservedPropertyNames = [
'filter'
]

function forwardStaticMembersAndCollectReflection (
function forwardStaticMembers (
Extended: typeof Vue,
Original: typeof Vue,
Super: typeof Vue,
reflectionMap: ReflectionMap
Super: typeof Vue
): void {
// We have to use getOwnPropertyNames since Babel registers methods as non-enumerable
Object.getOwnPropertyNames(Original).forEach(key => {
@@ -125,10 +111,6 @@ function forwardStaticMembersAndCollectReflection (
return
}

if (reflectionIsSupported()) {
reflectionMap.static[key] = Reflect.getOwnMetadataKeys(Original, key)
}

// Some browsers does not allow reconfigure built-in properties
const extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key)
if (extendedDescriptor && !extendedDescriptor.configurable) {
@@ -1,39 +1,39 @@
import { VueConstructor } from 'vue'

export type StringToArrayMap = {
[key: string]: Array<string>
}

export type ReflectionMap = {
constructor: Array<string>,
instance: StringToArrayMap,
static: StringToArrayMap
}
import Vue, { VueConstructor } from 'vue'
import { VueClass } from './declarations'

export function reflectionIsSupported () {
return (Reflect && Reflect.defineMetadata) !== undefined
}

export function copyReflectionMetadata (
from: VueConstructor,
to: VueConstructor,
reflectionMap: ReflectionMap
from: VueClass<Vue>
) {
shallowCopy(from.prototype, to.prototype, reflectionMap.instance)
shallowCopy(from, to, reflectionMap.static)
shallowCopy(from, to, {'constructor': reflectionMap.constructor})
forwardMetadata(to, from)

Object.keys(from.prototype).forEach(key => {
forwardMetadata(to.prototype, from.prototype, key)
})

Object.keys(from).forEach(key => {
forwardMetadata(to, from, key)
})
}

function shallowCopy (from: VueConstructor, to: VueConstructor, propertyKeys: StringToArrayMap) {
for (const propertyKey in propertyKeys) {
propertyKeys[propertyKey].forEach((metadataKey) => {
if (propertyKey == 'constructor') {
const metadata = Reflect.getOwnMetadata(metadataKey, from)
Reflect.defineMetadata(metadataKey, metadata, to)
} else {
const metadata = Reflect.getOwnMetadata(metadataKey, from, propertyKey)
Reflect.defineMetadata(metadataKey, metadata, to, propertyKey)
}
})
}
function forwardMetadata(to: object, from: object, propertyKey?: string): void {
const metaKeys = propertyKey
? Reflect.getOwnMetadataKeys(from, propertyKey)
: Reflect.getOwnMetadataKeys(from)

metaKeys.forEach(metaKey => {
const metadata = propertyKey
? Reflect.getOwnMetadata(metaKey, from, propertyKey)
: Reflect.getOwnMetadata(metaKey, from)

if (propertyKey) {
Reflect.defineMetadata(metaKey, metadata, to, propertyKey)
} else {
Reflect.defineMetadata(metaKey, metadata, to)
}
})
}

0 comments on commit 42005ad

Please sign in to comment.