UNPKG

type-autofac

Version:

tsioc is AOP, Ioc container, via typescript decorator

216 lines (214 loc) 10.5 kB
"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 index_1 = require("../../core/index"); var index_2 = require("../joinpoints/index"); var index_3 = require("../joinpoints/index"); var index_4 = require("../../utils/index"); var ProxyMethod = /** @class */ (function () { function ProxyMethod(container) { this.container = container; } Object.defineProperty(ProxyMethod.prototype, "aspectMgr", { get: function () { if (!this._aspectMgr) { this._aspectMgr = this.container.get(index_4.symbols.IAdvisor); } return this._aspectMgr; }, enumerable: true, configurable: true }); Object.defineProperty(ProxyMethod.prototype, "liefScope", { get: function () { if (!this._liefScope) { this._liefScope = this.container.getLifeScope(); } return this._liefScope; }, enumerable: true, configurable: true }); ProxyMethod.prototype.proceed = function (target, targetType, pointcut, provJoinpoint) { var aspectMgr = this.aspectMgr; var fullName = pointcut.fullName; var methodName = pointcut.name; var advices = aspectMgr.getAdvices(fullName); if (advices && pointcut) { if (pointcut.descriptor && (pointcut.descriptor.get || pointcut.descriptor.set)) { if (pointcut.descriptor.get) { var getMethod = pointcut.descriptor.get.bind(target); pointcut.descriptor.get = this.proxy(getMethod, advices, target, targetType, pointcut, provJoinpoint); } if (pointcut.descriptor.set) { var setMethod = pointcut.descriptor.set.bind(target); pointcut.descriptor.set = this.proxy(setMethod, advices, target, targetType, pointcut, provJoinpoint); } Object.defineProperty(target, methodName, pointcut.descriptor); } else if (index_4.isFunction(target[methodName])) { var propertyMethod = target[methodName].bind(target); target[methodName] = this.proxy(propertyMethod, advices, target, targetType, pointcut, provJoinpoint); } } }; ProxyMethod.prototype.proxy = function (propertyMethod, advices, target, targetType, pointcut, provJoinpoint) { var _this = this; var aspectMgr = this.aspectMgr; var fullName = pointcut.fullName; var methodName = pointcut.name; var liefScope = this.liefScope; var container = this.container; return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var joinPoint = _this.container.resolve(index_3.Joinpoint, index_1.Provider.create('options', { name: methodName, fullName: fullName, provJoinpoint: provJoinpoint, annotations: provJoinpoint ? null : liefScope.getMethodMetadatas(targetType, methodName), params: liefScope.getMethodParameters(targetType, target, methodName), args: args, target: target, targetType: targetType })); var val; var adviceAction = function (advicer, state, returnValue, throwError) { joinPoint.state = state; joinPoint.advicer = advicer; joinPoint.returning = returnValue; joinPoint.throwing = throwError; var providers = []; providers.push(index_1.Provider.createExtends(index_3.Joinpoint, joinPoint, function (inst, provider) { inst._cache_JoinPoint = provider.resolve(container); })); var metadata = advicer.advice; if (!index_4.isUndefined(returnValue) && metadata.args) { providers.push(index_1.Provider.create(metadata.args, args)); } if (metadata.annotationArgName) { providers.push(index_1.Provider.create(metadata.annotationArgName, function () { var curj = joinPoint; var annotations = curj.annotations; while (!annotations && joinPoint.provJoinpoint) { curj = joinPoint.provJoinpoint; if (curj && curj.annotations) { annotations = curj.annotations; break; } } if (index_4.isArray(annotations)) { if (metadata.annotation) { var d_1 = metadata.annotation; d_1 = /^@/.test(d_1) ? d_1 : "@" + d_1; return annotations.filter(function (a) { return a.decorator === d_1; }); } return annotations; } else { return []; } })); } if (!index_4.isUndefined(returnValue) && metadata.returning) { providers.push(index_1.Provider.create(metadata.returning, returnValue)); } if (throwError && metadata.throwing) { providers.push(index_1.Provider.create(metadata.throwing, throwError)); } return (_a = _this.container).syncInvoke.apply(_a, [advicer.aspectType, advicer.advice.propertyKey, null].concat(providers)); var _a; }; var asBefore = function (propertyKeys, state) { propertyKeys.forEach(function (propertyKey) { var canModify = ['Around', 'Pointcut'].indexOf(propertyKey) >= 0; advices[propertyKey].forEach(function (advicer) { var retargs = adviceAction(advicer, state); if (canModify && index_4.isArray(retargs)) { args = retargs; } }); }); return args; }; var asResult = function (propertyKeys, state, val, throwError) { if (index_4.isPromise(val)) { propertyKeys.forEach(function (propertyKey) { var hasReturn = ['Around', 'AfterReturning'].indexOf(propertyKey) >= 0; advices[propertyKey].forEach(function (advicer) { val = val.then(function (value) { var retval = adviceAction(advicer, state, hasReturn ? value : undefined, throwError); if (index_4.isPromise(retval)) { return retval.then(function (val) { if (hasReturn && !index_4.isUndefined(val)) { return val; } else { return value; } }); } else { if (hasReturn && !index_4.isUndefined(retval)) { return retval; } else { return value; } } }); }); }); } else { propertyKeys.forEach(function (propertyKey) { var hasReturn = ['Around', 'AfterReturning'].indexOf(propertyKey) >= 0; advices[propertyKey].forEach(function (advicer) { var retval = adviceAction(advicer, state, hasReturn ? val : undefined, throwError); if (hasReturn && !index_4.isUndefined(retval)) { val = retval; } }); }); } return val; }; args = asBefore(['Around', 'Before'], index_2.JoinpointState.Before); args = asBefore(['Pointcut'], index_2.JoinpointState.Pointcut); var exeErr; try { val = propertyMethod.apply(void 0, args); } catch (err) { exeErr = err; } asResult(['Around', 'After'], index_2.JoinpointState.After, val); if (exeErr) { asResult(['Around', 'AfterThrowing'], index_2.JoinpointState.AfterThrowing, val, exeErr); } else { val = asResult(['Around', 'AfterReturning'], index_2.JoinpointState.AfterReturning, val); return val; } }; }; ProxyMethod = __decorate([ index_1.NonePointcut() // @Singleton(symbols.IProxyMethod) , __metadata("design:paramtypes", [Object]) ], ProxyMethod); return ProxyMethod; }()); exports.ProxyMethod = ProxyMethod; //# sourceMappingURL=../../sourcemaps/aop/access/ProxyMethod.js.map