UNPKG

@zoranwong/acc-engine.js

Version:

A javascript framework to accelerate your applicatin developement

275 lines (228 loc) 8.62 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _newArrowCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/newArrowCheck")); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _underscore = _interopRequireDefault(require("underscore")); var _Application = _interopRequireDefault(require("../foundation/Application")); var Pipeline = /*#__PURE__*/function () { /**@property {Application} _container*/ /** * @param {Application} container * */ function Pipeline(container) { (0, _classCallCheck2["default"])(this, Pipeline); this._container = null; this._passable = null; this._pipes = []; this._method = 'handle'; this._container = container; } (0, _createClass2["default"])(Pipeline, [{ key: "send", value: function send(passable) { this._passable = passable; return this; } }, { key: "through", value: function through() { for (var _len = arguments.length, pipes = new Array(_len), _key = 0; _key < _len; _key++) { pipes[_key] = arguments[_key]; } this._pipes = _underscore["default"].isArray(pipes) ? pipes : Array.from(arguments); return this; } }, { key: "via", value: function via(method) { this._method = method; return this; } }, { key: "then", value: function () { var _then = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(destination) { var pipeline; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return this._pipes.reverse().reduce(this._carry(), this._prepareDestination(destination)); case 2: pipeline = _context.sent; _context.next = 5; return pipeline(this._passable); case 5: return _context.abrupt("return", _context.sent); case 6: case "end": return _context.stop(); } } }, _callee, this); })); function then(_x) { return _then.apply(this, arguments); } return then; }() }, { key: "thenReturn", value: function () { var _thenReturn = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() { var _this = this; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return this.then(function (passable) { (0, _newArrowCheck2["default"])(this, _this); return passable; }.bind(this)); case 2: return _context2.abrupt("return", _context2.sent); case 3: case "end": return _context2.stop(); } } }, _callee2, this); })); function thenReturn() { return _thenReturn.apply(this, arguments); } return thenReturn; }() }, { key: "_prepareDestination", value: function _prepareDestination(destination) { var _this2 = this; return /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(passable) { return _regenerator["default"].wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: (0, _newArrowCheck2["default"])(this, _this2); _context3.next = 3; return destination(passable); case 3: return _context3.abrupt("return", _context3.sent); case 4: case "end": return _context3.stop(); } } }, _callee3, this); })); return function (_x2) { return _ref.apply(this, arguments); }; }().bind(this); } }, { key: "_carry", value: function _carry() { var _this3 = this; return /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(stack, pipe) { var _this4 = this; return _regenerator["default"].wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: (0, _newArrowCheck2["default"])(this, _this3); if (!(stack instanceof Promise)) { _context5.next = 5; break; } _context5.next = 4; return stack; case 4: stack = _context5.sent; case 5: return _context5.abrupt("return", /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(passable) { return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: (0, _newArrowCheck2["default"])(this, _this4); if (!(_underscore["default"].isFunction(pipe) && !this.isClass(pipe))) { _context4.next = 7; break; } _context4.next = 4; return pipe(passable, stack); case 4: return _context4.abrupt("return", _context4.sent); case 7: if (_underscore["default"].isString(pipe)) { pipe = this.container.get(pipe); } else if (this.isClass(pipe)) { pipe = new pipe(this._container); } case 8: if (!(typeof pipe[this._method] !== 'undefined')) { _context4.next = 14; break; } _context4.next = 11; return pipe[this._method](passable, stack); case 11: return _context4.abrupt("return", _context4.sent); case 14: throw TypeError("middleware has no ".concat(this._method, " method!")); case 15: case "end": return _context4.stop(); } } }, _callee4, this); })); return function (_x5) { return _ref3.apply(this, arguments); }; }().bind(this)); case 6: case "end": return _context5.stop(); } } }, _callee5, this); })); return function (_x3, _x4) { return _ref2.apply(this, arguments); }; }().bind(this); } }, { key: "isClass", value: function isClass(obj) { try { return this.container.isClass(obj); } catch (e) { return false; } } /** * @return {Application} * */ }, { key: "container", get: function get() { return this._container; } }]); return Pipeline; }(); exports["default"] = Pipeline;