|
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' |
12 | 3 |
|
13 | 4 | export function reflectionIsSupported () {
|
14 | 5 | return (Reflect && Reflect.defineMetadata) !== undefined
|
15 | 6 | }
|
16 | 7 |
|
17 | 8 | export function copyReflectionMetadata (
|
18 |
| - from: VueConstructor, |
19 | 9 | to: VueConstructor,
|
20 |
| - reflectionMap: ReflectionMap |
| 10 | + from: VueClass<Vue> |
21 | 11 | ) {
|
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 | + }) |
25 | 21 | }
|
26 | 22 |
|
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 | + }) |
39 | 39 | }
|
0 commit comments