UNPKG

@wener/console

Version:
131 lines (130 loc) 4.67 kB
function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _class_call_check(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 _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(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; } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } import { get, set } from '@wener/utils'; import { createStore } from 'zustand'; import { immer } from 'zustand/middleware/immer'; export var DynamicStore = /*#__PURE__*/ function() { "use strict"; function DynamicStore() { var store = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : createStore(immer(function() { return {}; })); _class_call_check(this, DynamicStore); _define_property(this, "store", void 0); this.store = store; } _create_class(DynamicStore, [ { key: "value", get: function get() { return this.store.getState(); } }, { key: "add", value: function add(type, payload) { this.store.setState(function(s) { var _last; var last = get(s, type, []); if (!Array.isArray(last)) { last = [ last ]; } Array.isArray(payload) ? (_last = last).push.apply(_last, _to_consumable_array(payload)) : last.push(payload); set(s, type, last, false); }); } }, { key: "collect", value: function collect(type) { var _get; var found = (_get = get(this.value, type)) !== null && _get !== void 0 ? _get : []; if (!Array.isArray(found)) { found = [ found ]; } return found; } }, { key: "set", value: function set1(key, value) { var merge = (arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}).merge; this.store.setState(function(s) { set(s, key, value, merge); }); } }, { key: "get", value: function get1(key) { return get(this.value, key); } }, { key: "as", value: function as() { return this; } } ]); return DynamicStore; }();