Skip to content

Commit

Permalink
feat:Decorator add RelectMetaInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
厚发 committed Sep 29, 2020
1 parent a6354cd commit 78c288f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -3,6 +3,7 @@ export function inject(fallback?: () => any): any;
export enum InjectType {
Context = 'Context',
Application = 'Application',
Property = 'Property',
}

export function Context(constructor: Function): any;
Expand Down
9 changes: 6 additions & 3 deletions index.js
@@ -1,10 +1,12 @@
require('reflect-metadata');
const { INJECTED_TAG_CLASS, INJECTED_TAG_PROP } = require('./lib/const')

const typeKey = Symbol('typeName');
const containnerKey = Symbol('containner');

exports.inject = function(fallback) {
return (target, key) => {
Reflect.defineMetadata(INJECTED_TAG_PROP, InjectType.Property, target, key)
return {
get() {
let c = Reflect.getMetadata('design:type', target, key);
Expand All @@ -20,7 +22,7 @@ exports.inject = function(fallback) {
}
}

const injectType = c[containnerKey];
const injectType = Reflect.getMetadata(INJECTED_TAG_CLASS, c);
if (!injectType || !InjectType[injectType]) {
throw new Error(`Inject ${c.name} component must decorator by @Context or @Application`);
}
Expand All @@ -33,16 +35,17 @@ exports.inject = function(fallback) {
const InjectType = {
Context: 'Context',
Application: 'Application',
Property: 'Property',
};

exports.InjectType = InjectType;

exports.Context = function(target) {
target[containnerKey] = InjectType.Context;
Reflect.defineMetadata(INJECTED_TAG_CLASS, InjectType.Context, target)
}

exports.Application = function(target) {
target[containnerKey] = InjectType.Application;
Reflect.defineMetadata(INJECTED_TAG_CLASS, InjectType.Application, target)
}

exports.getComponent = function(constructor, ctx, injectType = InjectType.Context) {
Expand Down
7 changes: 7 additions & 0 deletions lib/const/index.js
@@ -0,0 +1,7 @@
const INJECTED_TAG_CLASS = Symbol('injection:class');
const INJECTED_TAG_PROP = Symbol('injection:prop');

module.exports = {
INJECTED_TAG_CLASS,
INJECTED_TAG_PROP,
};
18 changes: 17 additions & 1 deletion test/index.test.ts
@@ -1,5 +1,8 @@
import { getComponent, inject, Context, Application } from '..';
import 'should';
import 'reflect-metadata';
import { getComponent, inject, Context, Application, InjectType } from '..';
const { INJECTED_TAG_CLASS, INJECTED_TAG_PROP } = require('../lib/const');


@Context
class A { foo() { return 'bar'; }}
Expand Down Expand Up @@ -81,4 +84,17 @@ describe('egg-di', () => {
new O({ name: 'haha' }).start();
}).should.throw(/circular dependency/);
});

it('injected class for context should be realized', () => {
Reflect.getMetadata(INJECTED_TAG_CLASS, A).should.equal(InjectType.Context);
})

it('injected class for application should be realized', () => {
Reflect.getMetadata(INJECTED_TAG_CLASS, A2).should.equal(InjectType.Application);
})

it('injected property should be realized', () => {
const ctx = { app: {} };
Reflect.getMetadata(INJECTED_TAG_PROP, new B(ctx), 'a').should.equal(InjectType.Property);
})
});

0 comments on commit 78c288f

Please sign in to comment.