tsioc
Version:
tsioc is AOP, Ioc container, via typescript decorator
271 lines (269 loc) • 10.9 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("../types");
var index_1 = require("../utils/index");
var index_2 = require("./decorators/index");
var index_3 = require("./actions/index");
var index_4 = require("./factories/index");
var ActionFactory_1 = require("./ActionFactory");
var index_5 = require("./decorators/index");
var DefaultLifeScope = /** @class */ (function () {
function DefaultLifeScope(container) {
this.container = container;
this.decorators = [];
this.buildAction();
}
DefaultLifeScope.prototype.addAction = function (action, type) {
var _this = this;
var nodepaths = [];
for (var _i = 2; _i < arguments.length; _i++) {
nodepaths[_i - 2] = arguments[_i];
}
var types = this.toActionType(type);
types.split(',').forEach(function (name) {
var parent = _this.getAtionByName(name);
nodepaths.forEach(function (pathname) {
parent = parent.find(function (act) { return act.name === pathname; });
});
if (parent) {
parent.add(action);
}
});
return this;
};
DefaultLifeScope.prototype.registerDecorator = function (decorator) {
var actions = [];
for (var _i = 1; _i < arguments.length; _i++) {
actions[_i - 1] = arguments[_i];
}
var type = this.getDecoratorType(decorator);
return this.registerCustomDecorator.apply(this, [decorator, type].concat(actions));
};
DefaultLifeScope.prototype.registerCustomDecorator = function (decorator, type) {
var actions = [];
for (var _i = 2; _i < arguments.length; _i++) {
actions[_i - 2] = arguments[_i];
}
var types = this.toActionType(type);
var name = decorator.toString();
if (!this.decorators.some(function (d) { return d.name === name; })) {
this.decorators.push({
name: name,
types: types,
actions: actions
});
}
return this;
};
DefaultLifeScope.prototype.execute = function (type, data) {
var _this = this;
var names = [];
for (var _i = 2; _i < arguments.length; _i++) {
names[_i - 2] = arguments[_i];
}
var types = this.toActionType(type).split(',');
return this.action.filter(function (act) { return types.indexOf(act.name) >= 0; }).forEach(function (act) {
names.forEach(function (name) {
act = act.find(function (itm) { return itm.name === name; });
});
act.execute(_this.container, data);
});
};
DefaultLifeScope.prototype.getClassDecorators = function (match) {
return this.getTypeDecorators(this.toActionType(index_4.DecoratorType.Class), match);
};
DefaultLifeScope.prototype.getMethodDecorators = function (match) {
return this.getTypeDecorators(this.toActionType(index_4.DecoratorType.Method), match);
};
DefaultLifeScope.prototype.getPropertyDecorators = function (match) {
return this.getTypeDecorators(this.toActionType(index_4.DecoratorType.Property), match);
};
DefaultLifeScope.prototype.getParameterDecorators = function (match) {
return this.getTypeDecorators(this.toActionType(index_4.DecoratorType.Parameter), match);
};
DefaultLifeScope.prototype.getDecoratorType = function (decirator) {
return decirator.decoratorType || index_4.DecoratorType.All;
};
/**
* is vaildate dependence type or not. dependence type must with class decorator.
*
* @template T
* @param {Type<T>} target
* @returns {boolean}
* @memberof Container
*/
DefaultLifeScope.prototype.isVaildDependence = function (target) {
if (!target) {
return false;
}
if (!index_1.isClass(target)) {
return false;
}
if (index_1.isAbstractDecoratorClass(target)) {
return false;
}
return this.getClassDecorators().some(function (act) { return index_4.hasOwnClassMetadata(act.name, target); });
};
DefaultLifeScope.prototype.getAtionByName = function (name) {
return this.action.find(function (action) { return action.name === name; });
};
DefaultLifeScope.prototype.getClassAction = function () {
return this.getAtionByName(this.toActionType(index_4.DecoratorType.Class));
};
DefaultLifeScope.prototype.getMethodAction = function () {
return this.getAtionByName(this.toActionType(index_4.DecoratorType.Method));
};
DefaultLifeScope.prototype.getPropertyAction = function () {
return this.getAtionByName(this.toActionType(index_4.DecoratorType.Property));
};
DefaultLifeScope.prototype.getParameterAction = function () {
return this.getAtionByName(this.toActionType(index_4.DecoratorType.Parameter));
};
/**
* get constructor parameters metadata.
*
* @template T
* @param {Type<T>} type
* @returns {IParameter[]}
* @memberof IContainer
*/
DefaultLifeScope.prototype.getConstructorParameters = function (type) {
return this.getParameters(type);
};
/**
* get method params metadata.
*
* @template T
* @param {Type<T>} type
* @param {T} instance
* @param {(string | symbol)} propertyKey
* @returns {IParameter[]}
* @memberof IContainer
*/
DefaultLifeScope.prototype.getMethodParameters = function (type, instance, propertyKey) {
return this.getParameters(type, instance, propertyKey);
};
/**
* get paramerter names.
*
* @template T
* @param {Type<T>} type
* @param {(string | symbol)} propertyKey
* @returns {string[]}
* @memberof DefaultLifeScope
*/
DefaultLifeScope.prototype.getParamerterNames = function (type, propertyKey) {
var metadata = index_4.getOwnParamerterNames(type);
var paramNames = [];
if (metadata && metadata.hasOwnProperty(propertyKey)) {
paramNames = metadata[propertyKey];
}
if (!index_1.isArray(paramNames)) {
paramNames = [];
}
return paramNames;
};
DefaultLifeScope.prototype.isSingletonType = function (type) {
if (index_4.hasOwnClassMetadata(index_2.Singleton, type)) {
return true;
}
return this.getClassDecorators().some(function (surm) {
var metadatas = index_4.getOwnTypeMetadata(surm.name, type) || [];
if (index_1.isArray(metadatas)) {
return metadatas.some(function (m) { return m.singleton === true; });
}
return false;
});
};
DefaultLifeScope.prototype.getMethodMetadatas = function (type, propertyKey) {
var metadatas = [];
this.getMethodDecorators().forEach(function (dec) {
var metas = index_4.getOwnMethodMetadata(dec.name, type);
if (metas.hasOwnProperty(propertyKey)) {
metadatas = metadatas.concat(metas[propertyKey] || []);
}
});
return metadatas;
};
DefaultLifeScope.prototype.filerDecorators = function (express) {
return this.decorators.filter(express);
};
DefaultLifeScope.prototype.getParameters = function (type, instance, propertyKey) {
propertyKey = propertyKey || 'constructor';
var data = {
target: instance,
targetType: type,
propertyKey: propertyKey
};
this.execute(index_4.DecoratorType.Parameter, data, index_3.CoreActions.bindParameterType);
var paramNames = this.getParamerterNames(type, propertyKey);
if (data.execResult.length) {
return data.execResult.map(function (typ, idx) {
return {
type: typ,
name: paramNames[idx]
};
});
}
else {
return paramNames.map(function (name) {
return {
name: name,
type: undefined
};
});
}
};
DefaultLifeScope.prototype.getTypeDecorators = function (decType, match) {
return this.filerDecorators(function (value) {
var flag = (value.types || '').indexOf(decType) >= 0;
if (flag && match) {
flag = match(value);
}
return flag;
});
};
DefaultLifeScope.prototype.buildAction = function () {
var factory = new ActionFactory_1.ActionFactory();
var action = factory.create('');
action.add(factory.create(this.toActionType(index_4.DecoratorType.Class))
.add(factory.create(types_1.IocState.design)
.add(factory.create(types_1.IocState.runtime))))
.add(factory.create(this.toActionType(index_4.DecoratorType.Method)))
.add(factory.create(this.toActionType(index_4.DecoratorType.Property)))
.add(factory.create(this.toActionType(index_4.DecoratorType.Parameter)));
this.action = action;
};
DefaultLifeScope.prototype.toActionType = function (type) {
var types = [];
if (type & index_4.DecoratorType.Class) {
types.push('ClassDecorator');
}
if (type & index_4.DecoratorType.Method) {
types.push('MethodDecorator');
}
if (type & index_4.DecoratorType.Property) {
types.push('PropertyDecorator');
}
if (type & index_4.DecoratorType.Parameter) {
types.push('ParameterDecorator');
}
return types.join(',');
};
DefaultLifeScope = __decorate([
index_5.NonePointcut(),
__metadata("design:paramtypes", [Object])
], DefaultLifeScope);
return DefaultLifeScope;
}());
exports.DefaultLifeScope = DefaultLifeScope;
//# sourceMappingURL=../sourcemaps/core/DefaultLifeScope.js.map