UNPKG

ts-aspect

Version:

Simplistic library for Aspect Oriented Programming in TypeScript

108 lines (107 loc) 4.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.proxyFunc = exports.asyncProxyFunc = void 0; const advice_enum_1 = require("./advice.enum"); async function asyncProxyFunc(target, methodName, methodContainer, ...args) { var _a, _b; const { originalMethod, adviceAspectMap } = methodContainer; const aspectCtx = { target: target, methodName: methodName, functionParams: args, returnValue: null, error: null, }; applyPreExecutionAspects(aspectCtx, adviceAspectMap); try { aspectCtx.returnValue = await originalMethod.apply(target, args); } catch (error) { if (adviceAspectMap.has(advice_enum_1.Advice.TryCatch)) { (_a = adviceAspectMap.get(advice_enum_1.Advice.TryCatch)) === null || _a === void 0 ? void 0 : _a.forEach(aspect => { aspectCtx.error = error; aspect.execute(aspectCtx); }); } else { throw error; } } finally { if (adviceAspectMap.has(advice_enum_1.Advice.TryFinally)) { (_b = adviceAspectMap.get(advice_enum_1.Advice.TryFinally)) === null || _b === void 0 ? void 0 : _b.forEach(aspect => { aspect.execute(aspectCtx); }); } } applyPostExecutionAspects(aspectCtx, adviceAspectMap); return aspectCtx.returnValue; } exports.asyncProxyFunc = asyncProxyFunc; function proxyFunc(target, methodName, methodContainer, ...args) { var _a, _b; const { originalMethod, adviceAspectMap } = methodContainer; const aspectCtx = { target: target, methodName: methodName, functionParams: args, returnValue: null, error: null, }; applyPreExecutionAspects(aspectCtx, adviceAspectMap); try { aspectCtx.returnValue = originalMethod.apply(target, args); } catch (error) { if (adviceAspectMap.has(advice_enum_1.Advice.TryCatch)) { (_a = adviceAspectMap.get(advice_enum_1.Advice.TryCatch)) === null || _a === void 0 ? void 0 : _a.forEach(aspect => { aspectCtx.error = error; aspect.execute(aspectCtx); }); } else { throw error; } } finally { if (adviceAspectMap.has(advice_enum_1.Advice.TryFinally)) { (_b = adviceAspectMap.get(advice_enum_1.Advice.TryFinally)) === null || _b === void 0 ? void 0 : _b.forEach(aspect => { aspect.execute(aspectCtx); }); } } applyPostExecutionAspects(aspectCtx, adviceAspectMap); return aspectCtx.returnValue; } exports.proxyFunc = proxyFunc; function applyPreExecutionAspects(aspectCtx, adviceAspectMap) { var _a, _b; if (adviceAspectMap.has(advice_enum_1.Advice.Before)) { (_a = adviceAspectMap.get(advice_enum_1.Advice.Before)) === null || _a === void 0 ? void 0 : _a.forEach(aspect => { aspect.execute(aspectCtx); }); } if (adviceAspectMap.has(advice_enum_1.Advice.Around)) { (_b = adviceAspectMap.get(advice_enum_1.Advice.Around)) === null || _b === void 0 ? void 0 : _b.forEach(aspect => { aspect.execute(aspectCtx); }); } } function applyPostExecutionAspects(aspectCtx, adviceAspectMap) { var _a, _b, _c; if (adviceAspectMap.has(advice_enum_1.Advice.Around)) { (_a = adviceAspectMap.get(advice_enum_1.Advice.Around)) === null || _a === void 0 ? void 0 : _a.forEach(aspect => { aspect.execute(aspectCtx); }); } if (adviceAspectMap.has(advice_enum_1.Advice.After)) { (_b = adviceAspectMap.get(advice_enum_1.Advice.After)) === null || _b === void 0 ? void 0 : _b.forEach(aspect => { aspect.execute(aspectCtx); }); } if (adviceAspectMap.has(advice_enum_1.Advice.AfterReturn)) { (_c = adviceAspectMap.get(advice_enum_1.Advice.AfterReturn)) === null || _c === void 0 ? void 0 : _c.forEach(aspect => { aspectCtx.returnValue = aspect.execute(aspectCtx); }); } }