UNPKG

@raulpesilva/re-state

Version:

easy way to create a shared state to the entire application

3 lines (2 loc) 6.02 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ReState={},t.React)}(this,(function(t,e){"use strict";var n=function(t){t()},r=function(){return n};function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function o(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return i(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s=function(){function t(){this._listeners=[]}var e=t.prototype;return e.subscribe=function(t){var e=this;return this._listeners.push(t),function(){var n=e._listeners.indexOf(t);n<0||(e._listeners[n]=e._listeners[e._listeners.length-1],e._listeners.length--)}},e.notify=function(t,e){for(var n,r=o(this._listeners);!(n=r()).done;){(0,n.value)(t,e)}},t}(),u=function(){function t(){this._listeners=new Map}var e=t.prototype;return e.subscribe=function(t,e){var n=this;if(this._listeners.has(t)){var r=this._listeners.get(t);this._listeners.set(t,[].concat(r,[e]))}return this._listeners.has(t)||this._listeners.set(t,[e]),function(){var r=[].concat(n._listeners.get(t)),i=n._listeners.get(t).indexOf(e);i<0||(r[i]=r[r.length-1],r.length--,n._listeners.set(t,r))}},e.notify=function(t){if(this._listeners.has(t))for(var e,n=o(this._listeners.get(t));!(e=n()).done;){(0,e.value)()}},t}();function a(){return a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},a.apply(this,arguments)}var c=function(){function t(){this.__store=new Map,this.__initial_store=new Map,this.__observer=new u,this.__listener=new s}t.convertStoreToObject=function(t){return Object.fromEntries(t.entries())},t.getStoreKeys=function(t){return Array.from(t.keys())};var e=t.prototype;return e.get=function(t){return this.__store.get(t)},e.getMany=function(e){if("function"!=typeof e)throw new TypeError("to select a value in the store it is necessary to pass a function - ex: (store) => store.myKey");return e(t.convertStoreToObject(this.__store))},e._set=function(t,e){var n=this.__store.has(t);if("function"==typeof e){var r=e(this.get(t));this.__store.set(t,r),n||this.setInitialValue(t,r)}else this.__store.set(t,e),n||this.setInitialValue(t,e)},e.set=function(e,n){var i=this,o=a({},t.convertStoreToObject(this.__store));this._set(e,n);var s=a({},t.convertStoreToObject(this.__store));r()((function(){i.notify(e),i.notifySelectors(o,s)}))},e.setWithoutNotify=function(t,e){this._set(t,e)},e.setInitialValue=function(t,e){this.__initial_store.set(t,e)},e.has=function(t){return this.__store.has(t)},e.notifySelectors=function(t,e){this.__listener.notify(t,e)},e.subscribeSelector=function(t){return this.__listener.subscribe(t)},e.subscribe=function(t,e){return this.__observer.subscribe(t,e)},e.notify=function(t){this.__observer.notify(t)},e.reset=function(){var e=this;this.__store=new Map(this.__initial_store.entries());var n=t.getStoreKeys(this.__store),i=r(),o=t.convertStoreToObject(this.__store);i((function(){n.forEach((function(t){e.notify(t),e.notifySelectors(void 0,o)}))}))},t}(),f=new c,l=function(t,e){return f.setInitialValue(t,e)};function h(t){return function(){return f.get(t)}}var _="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?e.useLayoutEffect:e.useEffect;function v(t,n){var r=e.useCallback((function(e){return f.has(t)||f.setWithoutNotify(t,e),f.get(t)}),[t]),i=e.useCallback((function(e){f.set(t,e)}),[t]),o=e.useState(r(n)),s=o[0],u=o[1];return e.useDebugValue(r(n)),_((function(){return f.subscribe(t,(function(){u(f.get(t))}))}),[n,t]),[s,i]}function y(t,e){return f.setWithoutNotify(t,e),function(){return v(t)}}function b(t){return function(e){return f.set(t,e)}}function p(t){return function(){var n=e.useState(f.get(t)),r=n[0],i=n[1];return e.useDebugValue(r),_((function(){return f.subscribe(t,(function(){i(f.get(t))}))}),[t]),r}}v.displayName="useReState";var d=Object.prototype.hasOwnProperty;function g(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function S(t,e){if(g(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(var i=0;i<n.length;i++)if(!d.call(e,n[i])||!g(t[n[i]],e[n[i]]))return!1;return!0}t.createGetReState=h,t.createReState=y,t.createReStateDispatch=b,t.createReStateMethods=function(t,e,n){var r;if(!t)throw new Error("Name is required");if("string"!=typeof t)throw new Error("Name need to be a string");var i=t.charAt(0).toUpperCase()+t.slice(1),o=y(t,e),s=p(t),u=b(t),a=h(t);return n&&l(t,n.value),(r={})["use"+i]=o,r["use"+i+"Select"]=s,r["dispatch"+i]=u,r["get"+i]=a,r["reset"+i]=function(){var t;return u(null!=(t=null==n?void 0:n.value)?t:e)},r},t.createReStateSelect=p,t.onReStateChange=function(t,e){e.forEach((function(e){return f.subscribe(e,t)}))},t.resetReState=function(){return f.reset()},t.setReStateInitialValue=l,t.useReState=v,t.useReStateSelector=function(t,n){void 0===n&&(n=S);var r=e.useState(f.getMany(t)),i=r[0],o=r[1];return e.useDebugValue(i),_((function(){return f.subscribeSelector((function(e,r){var i=t(e),s=t(r);n(i,s)||o(s)}))}),[t]),i},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=re-state.production.min.js.map