@malagu/core
Version:
70 lines • 3.38 kB
JavaScript
;
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AopProxyFactoryImpl = void 0;
const utils_1 = require("../utils");
const container_1 = require("../container");
const aop_protocol_1 = require("./aop-protocol");
const annotation_1 = require("../annotation");
let AopProxyFactoryImpl = class AopProxyFactoryImpl {
getAdvices(id, tagValues) {
const container = container_1.ContainerProvider.provide();
const advices = [];
for (const tagValue of tagValues) {
if (container.isBoundTagged(id, aop_protocol_1.AOP_TAG, tagValue)) {
advices.push(...container.getAllTagged(id, aop_protocol_1.AOP_TAG, tagValue));
}
}
return advices;
}
create(config) {
const { metadata: { sysTags } } = config;
const proxy = new Proxy(config.target, {
get: (target, method, receiver) => {
if ((0, utils_1.isResolveMode)()) {
return target;
}
const func = target[method];
if (typeof func === 'function') {
return async (...args) => {
try {
const beforeAdvices = this.getAdvices(aop_protocol_1.MethodBeforeAdvice, sysTags);
for (const advice of beforeAdvices) {
await advice.before(method, args, target);
}
const returnValue = await func.apply(target, args);
const afterReturningAdvices = this.getAdvices(aop_protocol_1.AfterReturningAdvice, sysTags);
for (const advice of afterReturningAdvices) {
await advice.afterReturning(returnValue, method, args, target);
}
return returnValue;
}
catch (error) {
const afterThrowsAdvices = this.getAdvices(aop_protocol_1.AfterThrowsAdvice, sysTags);
for (const advice of afterThrowsAdvices) {
await advice.afterThrows(error, method, args, target);
}
throw error;
}
};
}
return func;
}
});
return {
getProxy() {
return proxy;
}
};
}
};
AopProxyFactoryImpl = __decorate([
(0, annotation_1.Component)({ id: aop_protocol_1.AopProxyFactory, proxy: false })
], AopProxyFactoryImpl);
exports.AopProxyFactoryImpl = AopProxyFactoryImpl;
//# sourceMappingURL=aop-proxy-factory.js.map