8000 refactor: simplify · vuejs/vue-class-component@42005ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 42005ad

Browse files
committed
refactor: simplify
1 parent 2e7291e commit 42005ad

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

src/component.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue, { ComponentOptions } from 'vue'
2-
import { copyReflectionMetadata, reflectionIsSupported, ReflectionMap } from './reflect'
2+
import { copyReflectionMetadata, reflectionIsSupported } from './reflect'
33
import { VueClass, DecoratedClass } from './declarations'
44
import { collectDataFromConstructor } from './data'
55
import { hasProto, isPrimitive, warn } from './util'
@@ -24,15 +24,6 @@ export function componentFactory (
2424
Component: VueClass<Vue>,
2525
options: ComponentOptions<Vue> = {}
2626
): VueClass<Vue> {
27-
const reflectionMap: ReflectionMap = {
28-
instance: {},
29-
static: {},
30-
constructor: []
31-
}
32-
33-
if (reflectionIsSupported()) {
34-
reflectionMap.constructor = Reflect.getOwnMetadataKeys(Component)
35-
}
3627
options.name = options.name || (Component as any)._componentTag || (Component as any).name
3728
// prototype props.
3829
const proto = Component.prototype
@@ -41,10 +32,6 @@ export function componentFactory (
4132
return
4233
}
4334

44-
if (reflectionIsSupported()) {
45-
reflectionMap.instance[key] = Reflect.getOwnMetadataKeys(proto, key)
46-
}
47-
4835
// hooks
4936
if ($internalHooks.indexOf(key) > -1) {
5037
options[key] = proto[key]
@@ -84,10 +71,10 @@ export function componentFactory (
8471
: Vue
8572
const Extended = Super.extend(options)
8673

87-
forwardStaticMembersAndCollectReflection(Extended, Component, Super, reflectionMap)
74+
forwardStaticMembers(Extended, Component, Super)
8875

8976
if (reflectionIsSupported()) {
90-
copyReflectionMetadata(Component, Extended, reflectionMap)
77+
copyReflectionMetadata(Extended, Component)
91< 8000 /td>78
}
9279

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

115-
function forwardStaticMembersAndCollectReflection (
102+
function forwardStaticMembers (
116103
Extended: typeof Vue,
117104
Original: typeof Vue,
118-
Super: typeof Vue,
119-
reflectionMap: ReflectionMap
105+
Super: typeof Vue
120106
): void {
121107
// We have to use getOwnPropertyNames since Babel registers methods as non-enumerable
122108
Object.getOwnPropertyNames(Original).forEach(key => {
@@ -125,10 +111,6 @@ function forwardStaticMembersAndCollectReflection (
125111
return
126112
}
127113

128-
if (reflectionIsSupported()) {
129-
reflectionMap.static[key] = Reflect.getOwnMetadataKeys(Original, key)
130-
}
131-
132114
// Some browsers does not allow reconfigure built-in properties
133115
const extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key)
134116
if (extendedDescriptor && !extendedDescriptor.configurable) {

src/reflect.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import { VueConstructor } from 'vue'
2-
3-
export type StringToArrayMap = {
4-
[key: string]: Array<string>
5-
}
6-
7-
export type ReflectionMap = {
8-
constructor: Array<string>,
9-
instance: StringToArrayMap,
10-
static: StringToArrayMap
11-
}
1+
import Vue, { VueConstructor } from 'vue'
2+
import { VueClass } from './declarations'
123

134
export function reflectionIsSupported () {
145
return (Reflect && Reflect.defineMetadata) !== undefined
156
}
167

178
export function copyReflectionMetadata (
18-
from: VueConstructor,
199
to: VueConstructor,
20-
reflectionMap: ReflectionMap
10+
from: VueClass<Vue>
2111
) {
22-
shallowCopy(from.prototype, to.prototype, reflectionMap.instance)
23-
shallowCopy(from, to, reflectionMap.static)
24-
shallowCopy(from, to, {'constructor': reflectionMap.constructor})
12+
forwardMetadata(to, from)
13+
14+
Object.keys(from.prototype).forEach(key => {
15+
forwardMetadata(to.prototype, from.prototype, key)
16+
})
17+
18+
Object.keys(from).forEach(key => {
19+
forwardMetadata(to, from, key)
20+
})
2521
}
2622

27-
function shallowCopy (from: VueConstructor, to: VueConstructor, propertyKeys: StringToArrayMap) {
28-
for (const propertyKey in propertyKeys) {
29-
propertyKeys[propertyKey].forEach((metadataKey) => {
30-
if (propertyKey == 'constructor') {
31-
const metadata = Reflect.getOwnMetadata(metadataKey, from)
32-
Reflect.defineMetadata(metadataKey, metadata, to)
33-
} else {
34-
const metadata = Reflect.getOwnMetadata(metadataKey, from, propertyKey)
35-
Reflect.defineMetadata(metadataKey, metadata, to, propertyKey)
36-
}
37-
})
38-
}
23+
function forwardMetadata(to: object, from: object, propertyKey?: string): void {
24+
const metaKeys = propertyKey
25+
? Reflect.getOwnMetadataKeys(from, propertyKey)
26+
: Reflect.getOwnMetadataKeys(from)
27+
28+
metaKeys.forEach(metaKey => {
29+
const metadata = propertyKey
30+
? Reflect.getOwnMetadata(metaKey, from, propertyKey)
31+
: Reflect.getOwnMetadata(metaKey, from)
32+
33+
if (propertyKey) {
34+
Reflect.defineMetadata(metaKey, metadata, to, propertyKey)
35+
} else {
36+
Reflect.defineMetadata(metaKey, metadata, to)
37+
}
38+
})
3939
}

0 commit comments

Comments
 (0)
0