UNPKG

honion

Version:
104 lines (103 loc) 5.21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _HookMiddleware_mh, _HookMiddleware_type; Object.defineProperty(exports, "__esModule", { value: true }); exports.execHooks = exports.HookMiddleware = exports.HookType = void 0; const middleware_1 = require("./middleware"); const MIDDLEWARE_HOOK_BAG = "@hal-wang/honion/middlewareHooksBag"; var HookType; (function (HookType) { HookType[HookType["BeforeInvoke"] = 0] = "BeforeInvoke"; HookType[HookType["AfterInvoke"] = 1] = "AfterInvoke"; HookType[HookType["BeforeNext"] = 2] = "BeforeNext"; HookType[HookType["Constructor"] = 3] = "Constructor"; HookType[HookType["Error"] = 4] = "Error"; })(HookType = exports.HookType || (exports.HookType = {})); class HookMiddleware extends middleware_1.Middleware { constructor(mh, type) { super(); _HookMiddleware_mh.set(this, void 0); _HookMiddleware_type.set(this, void 0); __classPrivateFieldSet(this, _HookMiddleware_mh, mh, "f"); __classPrivateFieldSet(this, _HookMiddleware_type, type, "f"); } invoke() { var _a; return __awaiter(this, void 0, void 0, function* () { const hooks = (_a = this.ctx.get(MIDDLEWARE_HOOK_BAG)) !== null && _a !== void 0 ? _a : []; hooks.push({ hook: __classPrivateFieldGet(this, _HookMiddleware_mh, "f"), type: __classPrivateFieldGet(this, _HookMiddleware_type, "f") }); this.ctx.set(MIDDLEWARE_HOOK_BAG, hooks); yield this.next(); }); } } exports.HookMiddleware = HookMiddleware; _HookMiddleware_mh = new WeakMap(), _HookMiddleware_type = new WeakMap(); function execHooks(ctx, middleware, type, error) { var _a; return __awaiter(this, void 0, void 0, function* () { if (type == HookType.Constructor) { return yield execConstructorHooks(ctx, middleware); } else if (type == HookType.Error) { return yield execErrorHooks(ctx, middleware, error); } const hooks = (_a = ctx.get(MIDDLEWARE_HOOK_BAG)) !== null && _a !== void 0 ? _a : []; for (const hookItem of hooks.filter((h) => h.type == type)) { const hookResult = yield hookItem.hook(ctx, middleware); if (typeof hookResult == "boolean" && !hookResult) { return false; } } }); } exports.execHooks = execHooks; function execErrorHooks(ctx, middleware, error) { var _a; return __awaiter(this, void 0, void 0, function* () { const hooks = (_a = ctx.get(MIDDLEWARE_HOOK_BAG)) !== null && _a !== void 0 ? _a : []; let result = false; for (const hookItem of hooks.filter((h) => h.type == HookType.Error)) { result = (yield hookItem.hook(ctx, middleware, error)); if (result) break; } return result; }); } function execConstructorHooks(ctx, middleware) { var _a; return __awaiter(this, void 0, void 0, function* () { const hooks = (_a = ctx.get(MIDDLEWARE_HOOK_BAG)) !== null && _a !== void 0 ? _a : []; let result; for (const hookItem of hooks.filter((h) => h.type == HookType.Constructor)) { if (!(middleware instanceof middleware_1.Middleware)) { result = (yield hookItem.hook(ctx, middleware)); if (result) break; } } if (!result) result = new middleware(); return result; }); }