-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
71 lines (59 loc) · 2.11 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
// circular dependency caused can not get target
if (!c) {
// support @inject(() => Bar)
if (typeof fallback === 'function') {
c = fallback();
}
if (!c) {
throw new Error(`${target.constructor.name} inject ${key} fail, This problem is usually caused by a circular dependency`);
}
}
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`);
}
return exports.getComponent(c, this.ctx, injectType);
},
};
}
}
const InjectType = {
Context: 'Context',
Application: 'Application',
Property: 'Property',
};
exports.InjectType = InjectType;
exports.Context = function(target) {
Reflect.defineMetadata(INJECTED_TAG_CLASS, InjectType.Context, target)
}
exports.Application = function(target) {
Reflect.defineMetadata(INJECTED_TAG_CLASS, InjectType.Application, target)
}
exports.getComponent = function(constructor, ctx, injectType = InjectType.Context) {
const typeNameKey = constructor[typeKey] || Symbol(constructor.name);
const base = injectType === InjectType.Context ? ctx : ctx.app;
let containner = base[containnerKey];
// init containner
if (!containner) {
containner = new Map();
base[containnerKey] = containner;
}
if (!containner.has(typeNameKey)) {
// init component
containner.set(typeNameKey, new constructor(ctx));
constructor[typeKey] = typeNameKey;
}
return containner.get(typeNameKey);
}
exports.INJECTED_TAG_CLASS = INJECTED_TAG_CLASS
exports.INJECTED_TAG_PROP = INJECTED_TAG_PROP