UNPKG

mutable-store

Version:

a mutable state management library for javascript

157 lines (156 loc) 5.41 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Store = void 0; exports.getProps = getProps; exports.default = createMutableStore; function getProps(obj) { var e_1, _a; var props = new Set(); var builtins = [ Object.prototype, Array.prototype, Function.prototype, String.prototype, Number.prototype, Boolean.prototype, Symbol.prototype, Date.prototype, RegExp.prototype, Map.prototype, Set.prototype, WeakMap.prototype, WeakSet.prototype, Promise.prototype, Error.prototype, ]; while (obj && !builtins.includes(obj)) { try { for (var _b = (e_1 = void 0, __values(Reflect.ownKeys(obj))), _c = _b.next(); !_c.done; _c = _b.next()) { var key = _c.value; if (key !== "constructor") { props.add(key); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } obj = Object.getPrototypeOf(obj); } return Array.from(props); } function makePropsReadOnly(obj, keys) { var e_2, _a; try { for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) { var key = keys_1_1.value; if (Object.prototype.hasOwnProperty.call(obj, key)) { Object.defineProperty(obj, key, { writable: false, configurable: false, enumerable: true }); } else { if (process.env.NODE_ENV !== 'production') { // eslint-disable-next-line no-console console.warn("Property \"".concat(String(key), "\" does not exist on the object.")); } } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1); } finally { if (e_2) throw e_2.error; } } return obj; } function createMutableStore(mutableState) { if (mutableState === null || typeof mutableState !== "object") { throw Error("mutableState must be an object"); } var props = getProps(mutableState); if (props.includes("subscribe")) { throw Error("subscribe is a reserved keyword for the store, please use another name for your property"); } var subscriptions = new Set(); var internalStoreUnSubs = new Set(); var getInternalStores = function () { return props .filter(function (item) { return mutableState[item].___thisIsAMutableStore___; }) .map(function (item) { return mutableState[item]; }); }; mutableState.___thisIsAMutableStore___ = true; mutableState.___version___ = 1; var subscribe = function (fn) { subscriptions.add(fn); return function () { subscriptions.delete(fn); }; }; mutableState.subscribe = subscribe; function callSubs() { try { subscriptions.forEach(function (fn) { return fn(); }); } catch (e) { if (process.env.NODE_ENV !== 'production') { // eslint-disable-next-line no-console console.error(e); } } } // auto subscribe to internal stores function autoSubscribeToInternalStores() { getInternalStores().forEach(function (item) { return internalStoreUnSubs.add(item.subscribe(function () { callSubs(); })); }); } autoSubscribeToInternalStores(); props.forEach(function (prop) { if (typeof mutableState[prop] === "function" && prop.toString().startsWith("set_")) { var originalMethod_1 = mutableState[prop]; mutableState[prop] = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var result = originalMethod_1.apply(mutableState, args); setTimeout(function () { callSubs(); autoSubscribeToInternalStores(); }, 0); return result; }; } }); Object.preventExtensions(mutableState); makePropsReadOnly(mutableState, props.filter(function (item) { var _a; return typeof mutableState[item] === "function" || ((_a = mutableState[item]) === null || _a === void 0 ? void 0 : _a.___thisIsAMutableStore___); })); return mutableState; } exports.Store = createMutableStore;