@artus/injection
Advanced tools
Comparing version
@@ -14,10 +14,14 @@ import { ContainerType, Identifier, InjectableMetadata, InjectableDefinition, HandlerFunction } from './types'; | ||
getByTag(tag: string): unknown[]; | ||
getByTagAsync(tag: string): Promise<unknown[]>; | ||
registerHandler(name: string, handler: HandlerFunction): void; | ||
getHandler(name: string): HandlerFunction | undefined; | ||
protected getValue(md: InjectableMetadata): any; | ||
protected getValueAsync(md: InjectableMetadata): Promise<any>; | ||
protected getMetadata(id: Identifier): InjectableMetadata | undefined; | ||
private resolveParams; | ||
private resolveParamsAsync; | ||
private handleProps; | ||
private handlePropsAsync; | ||
private handleTag; | ||
private resolveHandler; | ||
} |
@@ -27,3 +27,3 @@ "use strict"; | ||
} | ||
const instance = this.getValue(md); | ||
const instance = await this.getValueAsync(md); | ||
await ((_a = instance[md.initMethod]) === null || _a === void 0 ? void 0 : _a.call(instance)); | ||
@@ -88,2 +88,6 @@ return instance; | ||
} | ||
getByTagAsync(tag) { | ||
const clazzes = this.getInjectableByTag(tag); | ||
return Promise.all(clazzes.map(clazz => this.getAsync(clazz))); | ||
} | ||
registerHandler(name, handler) { | ||
@@ -109,2 +113,16 @@ this.handlerMap.set(name, handler); | ||
} | ||
async getValueAsync(md) { | ||
var _a; | ||
if (!(0, util_1.isUndefined)(md.value)) { | ||
return md.value; | ||
} | ||
const clazz = md.type; | ||
const params = await this.resolveParamsAsync(clazz, md.constructorArgs); | ||
const value = new clazz(...params); | ||
await this.handlePropsAsync(value, (_a = md.properties) !== null && _a !== void 0 ? _a : []); | ||
if (md.scope === types_1.ScopeEnum.SINGLETON) { | ||
md.value = value; | ||
} | ||
return value; | ||
} | ||
getMetadata(id) { | ||
@@ -136,2 +154,21 @@ const md = this.registry.get(id); | ||
} | ||
async resolveParamsAsync(clazz, args) { | ||
var _a; | ||
const params = []; | ||
if (!args || !args.length) { | ||
args = ((_a = (0, util_1.getParamMetadata)(clazz)) !== null && _a !== void 0 ? _a : []).map((ele, index) => ({ | ||
id: ele, | ||
index, | ||
})); | ||
} | ||
await Promise.all(args.map(async (arg) => { | ||
if ((0, util_1.isPrimitiveFunction)(arg.id)) { | ||
return; | ||
} | ||
params[arg.index] = arg.handler | ||
? await this.resolveHandler(arg.handler, arg.id) | ||
: await this.getAsync(arg.id); | ||
})); | ||
return params; | ||
} | ||
handleProps(instance, props) { | ||
@@ -144,2 +181,9 @@ props.forEach(prop => { | ||
} | ||
async handlePropsAsync(instance, props) { | ||
await Promise.all(props.map(async (prop) => { | ||
instance[prop.propertyName] = prop.handler | ||
? await this.resolveHandler(prop.handler, prop.id, instance) | ||
: await this.getAsync(prop.id); | ||
})); | ||
} | ||
handleTag(target) { | ||
@@ -146,0 +190,0 @@ let tags = Reflect.getOwnMetadata(constant_1.CLASS_TAG, target); |
@@ -35,3 +35,3 @@ "use strict"; | ||
} | ||
const instance = this.getValue(md); | ||
const instance = await this.getValueAsync(md); | ||
await ((_b = instance[md.initMethod]) === null || _b === void 0 ? void 0 : _b.call(instance)); | ||
@@ -54,3 +54,3 @@ if (md.scope === types_1.ScopeEnum.EXECUTION) { | ||
id: md.type, | ||
value | ||
value, | ||
}); | ||
@@ -60,3 +60,3 @@ } | ||
id: md.id, | ||
value | ||
value, | ||
}); | ||
@@ -63,0 +63,0 @@ } |
{ | ||
"name": "@artus/injection", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A IoC Implemention for Artus.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
32422
6.04%773
6.62%