You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
49 lines
1.9 KiB
4 weeks ago
|
import { POST_CONSTRUCT_ERROR } from "../constants/error_msgs";
|
||
|
import { TargetTypeEnum } from "../constants/literal_types";
|
||
|
import * as METADATA_KEY from "../constants/metadata_keys";
|
||
|
function _injectProperties(instance, childRequests, resolveRequest) {
|
||
|
var propertyInjectionsRequests = childRequests.filter(function (childRequest) {
|
||
|
return (childRequest.target !== null &&
|
||
|
childRequest.target.type === TargetTypeEnum.ClassProperty);
|
||
|
});
|
||
|
var propertyInjections = propertyInjectionsRequests.map(resolveRequest);
|
||
|
propertyInjectionsRequests.forEach(function (r, index) {
|
||
|
var propertyName = "";
|
||
|
propertyName = r.target.name.value();
|
||
|
var injection = propertyInjections[index];
|
||
|
instance[propertyName] = injection;
|
||
|
});
|
||
|
return instance;
|
||
|
}
|
||
|
function _createInstance(Func, injections) {
|
||
|
return new (Func.bind.apply(Func, [void 0].concat(injections)))();
|
||
|
}
|
||
|
function _postConstruct(constr, result) {
|
||
|
if (Reflect.hasMetadata(METADATA_KEY.POST_CONSTRUCT, constr)) {
|
||
|
var data = Reflect.getMetadata(METADATA_KEY.POST_CONSTRUCT, constr);
|
||
|
try {
|
||
|
result[data.value]();
|
||
|
}
|
||
|
catch (e) {
|
||
|
throw new Error(POST_CONSTRUCT_ERROR(constr.name, e.message));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
function resolveInstance(constr, childRequests, resolveRequest) {
|
||
|
var result = null;
|
||
|
if (childRequests.length > 0) {
|
||
|
var constructorInjectionsRequests = childRequests.filter(function (childRequest) {
|
||
|
return (childRequest.target !== null && childRequest.target.type === TargetTypeEnum.ConstructorArgument);
|
||
|
});
|
||
|
var constructorInjections = constructorInjectionsRequests.map(resolveRequest);
|
||
|
result = _createInstance(constr, constructorInjections);
|
||
|
result = _injectProperties(result, childRequests, resolveRequest);
|
||
|
}
|
||
|
else {
|
||
|
result = new constr();
|
||
|
}
|
||
|
_postConstruct(constr, result);
|
||
|
return result;
|
||
|
}
|
||
|
export { resolveInstance };
|