-
Notifications
You must be signed in to change notification settings - Fork 433
Added reflection support #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a13fc5a
cdd6366
07eff38
49e9c90
614b008
a362138
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import 'reflect-metadata' | ||
| import Vue, { ComponentOptions } from 'vue' | ||
| import { copyReflectionMetadata, ReflectionMap } from './reflect' | ||
| import { VueClass, DecoratedClass } from './declarations' | ||
| import { collectDataFromConstructor } from './data' | ||
| import { hasProto, isPrimitive, warn } from './util' | ||
|
|
@@ -23,13 +25,19 @@ export function componentFactory ( | |
| Component: VueClass<Vue>, | ||
| options: ComponentOptions<Vue> = {} | ||
| ): VueClass<Vue> { | ||
| const reflectionMap: ReflectionMap = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need such temporary object and to process for it in different functions? Why don't we just directly assign the original metadata to component properties?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I don't mean that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because forwarding code will be duplicated. If you okay with that, I'll do it in the way you are asking. Also, we don't have a new class object until almost the end of function.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, then we can extract the logic as a function. I believe we can remove something like: function forwardMetadata(to: typeof Vue, from: typeof Vue, propertyKey: string): void {
Reflect.getOwnMetadataKeys(from, propertyKey).forEach(metaKey => {
const metadata = Reflect.getOwnMetadata(metaKey, from, propertyKey)
Reflect.defineMetadata(metaKey, metadata, to, propertyKey)
})
}Then we just call it inline: Object.getOwnPropertyNames(Original).forEach(key => {
// `prototype` should not be overwritten
if (key === 'prototype') {
return
}
- reflectionMap.static[key] = Reflect.getOwnMetadataKeys(Original, key);
+ forwardMetadata(Extended, Original, key)
// Some browsers does not allow reconfigure built-in properties
const extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key)
if (extendedDescriptor && !extendedDescriptor.configurable) { forwardStaticMembersAndCollectReflection(Extended, Component, Super, reflectionMap)
- copyReflectionMetadata(Component, Extended, reflectionMap)
+ Object.getOwnPropertyNames(proto).forEach(key => {
+ forwardMetadata(Extended.prototype, proto, key)
+ }) |
||
| instance: {}, | ||
| static: {} | ||
| }; | ||
|
|
||
| options.name = options.name || (Component as any)._componentTag || (Component as any).name | ||
| // prototype props. | ||
| const proto = Component.prototype | ||
| Object.getOwnPropertyNames(proto).forEach(function (key) { | ||
| if (key === 'constructor') { | ||
| return | ||
| } | ||
| reflectionMap.instance[key] = Reflect.getOwnMetadataKeys(proto, key); | ||
| // hooks | ||
| if ($internalHooks.indexOf(key) > -1) { | ||
| options[key] = proto[key] | ||
|
|
@@ -69,7 +77,8 @@ export function componentFactory ( | |
| : Vue | ||
| const Extended = Super.extend(options) | ||
|
|
||
| forwardStaticMembers(Extended, Component, Super) | ||
| forwardStaticMembersAndCollectReflection(Extended, Component, Super, reflectionMap) | ||
| copyReflectionMetadata(Component, Extended, reflectionMap) | ||
|
||
|
|
||
| return Extended | ||
| } | ||
|
|
@@ -93,14 +102,21 @@ const reservedPropertyNames = [ | |
| 'filter' | ||
| ] | ||
|
|
||
| function forwardStaticMembers (Extended: typeof Vue, Original: typeof Vue, Super: typeof Vue): void { | ||
| function forwardStaticMembersAndCollectReflection ( | ||
| Extended: typeof Vue, | ||
| Original: typeof Vue, | ||
| Super: typeof Vue, | ||
| reflectionMap: ReflectionMap | ||
| ): void { | ||
| // We have to use getOwnPropertyNames since Babel registers methods as non-enumerable | ||
| Object.getOwnPropertyNames(Original).forEach(key => { | ||
| // `prototype` should not be overwritten | ||
| if (key === 'prototype') { | ||
| return | ||
| } | ||
|
|
||
| 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) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { VueConstructor } from 'vue' | ||
| import 'reflect-metadata' | ||
|
|
||
| export type StringToArrayMap = { | ||
| [key: string]: Array<string> | ||
| } | ||
|
|
||
| export type ReflectionMap = { | ||
| instance: StringToArrayMap, | ||
| static: StringToArrayMap | ||
| } | ||
|
|
||
| export function copyReflectionMetadata( | ||
| from: VueConstructor, | ||
| to: VueConstructor, | ||
| reflectionMap: ReflectionMap | ||
| ) { | ||
| shallowCopy(from.prototype, to.prototype, reflectionMap.instance); | ||
| shallowCopy(from, to, reflectionMap.static); | ||
| } | ||
|
|
||
| function shallowCopy(from: VueConstructor, to: VueConstructor, propertyKeys: StringToArrayMap) { | ||
| for (const propertyKey in propertyKeys) { | ||
| propertyKeys[propertyKey].forEach((metadataKey) => { | ||
| const metadata = Reflect.getOwnMetadata(metadataKey, from, propertyKey) | ||
| Reflect.defineMetadata(metadataKey, metadata, to, propertyKey) | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of adding dependency that some users may not need as it bloats the lib size. Can we remove the dep and make this feature be optional?
I think only forwarding metadata when Reflect metadata is supported would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's reasonable, I'll fix it.