UNPKG

expressmultithread

Version:

Fast, light-weight and low dependency [Express.js](https://www.npmjs.com/package/express) multithreaded router.

64 lines 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CallLoop = void 0; const postMessage_1 = require("../functions/utils/postMessage"); const makeObj_1 = require("../functions/utils/makeObj"); const types_1 = require("../types"); const strings_1 = require("../constants/strings"); class CallLoop { constructor(req, res, callstack) { this.handler = { 2: async (i, _err) => { await this.callstack[i](this.req, this.res); }, 3: async (i, _err) => { await this.callstack[i](this.req, this.res, this.getNext(i + 1)); }, 4: async (i, err) => { if (!err) return this.handle(i + 1); await this.callstack[i](err, this.req, this.res, this.getNext(i + 1)); } }; this.req = req; this.res = res; this.callstack = callstack; } pNext(arg) { (0, postMessage_1.postParent)({ cmd: types_1.ChildCmd.next, id: this.res._id, tid: this.res._tid, arg }); } handle(i = 0, err) { if (i === this.callstack.length || this.callstack[i].length < 2 || this.callstack[i].length > 4) return this.pNext(err ? (0, makeObj_1.makeObj)(err, Object.getOwnPropertyNames(err)) : undefined); this.handler[this.callstack[i].length](i, err) .catch((e) => this.goError(i + 1, e)); } getNext(index) { return (arg) => { if (arg === strings_1.route || arg === strings_1.router) { this.pNext(arg); } else if (arg) { this.goError(index, arg); } else { this.handle(index); } }; } goError(i, err) { for (let j = i; j < this.callstack.length; j++) { if (this.callstack[j].length === 4) return this.handle(j, err); } return this.handle(this.callstack.length, err); } } exports.CallLoop = CallLoop; //# sourceMappingURL=CallLoop.js.map