UNPKG

@virtuous/conductor

Version:
174 lines (139 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 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 Stack = /*#__PURE__*/ function () { function Stack() { _classCallCheck(this, Stack); _defineProperty(this, "stack", new Map()); } _createClass(Stack, [{ key: "add", /** * @param {string} id The key. * @param {Object} entry The entry to add. */ value: function add(id, entry) { if (!id || !entry) { return; } this.stack.set(id, entry); } /** * @returns {Array|null} */ }, { key: "first", value: function first() { if (!this.stack.size) { return null; } return this.stack.entries().next().value; } /** * @param {string} id The key to lookup. * @returns {Object|null} */ }, { key: "get", value: function get(id) { if (!id || !this.stack.has(id)) { return null; } return this.stack.get(id); } /** * @param {number} index The index to find. * @returns {Object|null} */ }, { key: "getByIndex", value: function getByIndex() { var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; if (index === null) { return null; } if (index > this.stack.size - 1) { return null; } return this.stack.get(Array.from(this.stack.keys())[index]); } /** * @returns {Map} */ }, { key: "getAll", value: function getAll() { return this.stack; } /** * @returns {Array|null} */ }, { key: "last", value: function last() { if (!this.stack.size) { return null; } return Array.from(this.stack.entries()).pop(); } /** * Clers the stack of all Routes. */ }, { key: "clear", value: function clear() { this.stack.clear(); } /** * @param {string} id The key to remove. */ }, { key: "remove", value: function remove(id) { this.stack["delete"](id); } /** * @param {Array} The key and value to reset to. */ }, { key: "reset", value: function reset() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.first(), _ref2 = _slicedToArray(_ref, 2), id = _ref2[0], entry = _ref2[1]; this.stack.clear(); this.add(id, entry); } /** * @param {string} id The key to update. * @param {Object} entry The value to update at the given id. */ }, { key: "update", value: function update(id, entry) { if (!id || !entry) { return; } if (!this.stack.has(id)) { return; } this.stack.set(id, entry); } }]); return Stack; }(); var _default = new Stack(); exports["default"] = _default;