Skip to content
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

feat:Decorator add RelectMetaInfo #4

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
})
});