-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathreflect.ts
42 lines (35 loc) · 1.35 KB
/
reflect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Vue, { VueConstructor } from 'vue'
import { VueClass } from './declarations'
// The rational behind the verbose Reflect-feature check below is the fact that there are polyfills
// which add an implementation for Reflect.defineMetadata but not for Reflect.getOwnMetadataKeys.
// Without this check consumers will encounter hard to track down runtime errors.
export function reflectionIsSupported () {
return typeof Reflect !== 'undefined' && Reflect.defineMetadata && Reflect.getOwnMetadataKeys
}
export function copyReflectionMetadata (
to: VueConstructor,
from: VueClass<Vue>
) {
forwardMetadata(to, from)
Object.getOwnPropertyNames(from.prototype).forEach(key => {
forwardMetadata(to.prototype, from.prototype, key)
})
Object.getOwnPropertyNames(from).forEach(key => {
forwardMetadata(to, from, key)
})
}
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)
}
})
}