@primitivesocial/ps-kernel
Version:
Middleware system with Vue Router and Vuex
76 lines (56 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _KernelHelper = require("./KernelHelper");
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var Kernel =
/*#__PURE__*/
function () {
// array of defined middlewares in the Vue router file
// "to" object in the beforeEach() Vue router hook
// next() function in the beforeEach() Vue router hook
//Vuex store instance
function Kernel(objTo, fnNext, store) {
_classCallCheck(this, Kernel);
_defineProperty(this, "middlewares", void 0);
_defineProperty(this, "to", void 0);
_defineProperty(this, "next", void 0);
_defineProperty(this, "store", void 0);
this.to = objTo;
this.next = fnNext;
this.store = store;
this.middlewares = objTo.meta && objTo.meta.middleware ? objTo.meta.middleware : [];
}
_createClass(Kernel, [{
key: "run",
value: function run() {
if (_typeof(this.middlewares) !== 'object') {
this.next();
}
if (!this.middlewares.length) {
this.next();
}
for (var index in this.middlewares) {
var store = this.store;
var route = this.middlewares[index]({
store: store,
redirect: _KernelHelper.redirect,
next: _KernelHelper.next
});
if (route) {
this.next(route);
break;
}
}
this.next();
}
}]);
return Kernel;
}();
exports["default"] = Kernel;