react-global-state-hooks 1.0.28 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,7 +44,7 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
44
44
  * @param {StateConfigCallbackParam<TState, TMetadata>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
45
45
  * @param {StateConfigCallbackParam<TState, TMetadata>} config.localStorageKey - The key to use to store the state in the localStorage (optional) (default: null) if not null the state will be stored in the localStorage
46
46
  * */
47
- constructor(state: TState, { onInit: onInitConfig, ...config }?: GlobalStoreConfig<TState, TMetadata, TStateSetter>, setterConfig?: TStateSetter | null);
47
+ constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>, setterConfig?: TStateSetter | null);
48
48
  protected setLocalStorageValue: () => void;
49
49
  protected getLocalStorageValue: () => string;
50
50
  /**
@@ -64,4 +64,3 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
64
64
  invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
65
65
  }) => void;
66
66
  }
67
- //# sourceMappingURL=GlobalStore.d.ts.map
@@ -22,4 +22,3 @@ export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends TGlobalSto
22
22
  encrypt?: boolean | ((value: string) => string);
23
23
  decrypt?: boolean | ((value: string) => string);
24
24
  };
25
- //# sourceMappingURL=GlobalStore.types.d.ts.map
package/lib/bundle.js ADDED
@@ -0,0 +1,2 @@
1
+ /*! For license information please see bundle.js.LICENSE.txt */
2
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["react","react-dom"],e):"object"==typeof exports?exports["react-global-state-hooks"]=e(require("react"),require("react-dom")):t["react-global-state-hooks"]=e(t.react,t["react-dom"])}(this,((t,e)=>{return r={774:(t,e,r)=>{"use strict";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function o(){o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},a="function"==typeof Symbol?Symbol:{},u=a.iterator||"@@iterator",c=a.asyncIterator||"@@asyncIterator",l=a.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function s(t,e,r,n){var o=e&&e.prototype instanceof y?e:y,a=Object.create(o.prototype),u=new C(n||[]);return i(a,"_invoke",{value:j(t,r,u)}),a}function p(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var v={};function y(){}function d(){}function h(){}var g={};f(g,u,(function(){return this}));var b=Object.getPrototypeOf,m=b&&b(b(E([])));m&&m!==e&&r.call(m,u)&&(g=m);var S=h.prototype=y.prototype=Object.create(g);function w(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function O(t,e){function o(i,a,u,c){var l=p(t[i],t,a);if("throw"!==l.type){var f=l.arg,s=f.value;return s&&"object"==n(s)&&r.call(s,"__await")?e.resolve(s.__await).then((function(t){o("next",t,u,c)}),(function(t){o("throw",t,u,c)})):e.resolve(s).then((function(t){f.value=t,u(f)}),(function(t){return o("throw",t,u,c)}))}c(l.arg)}var a;i(this,"_invoke",{value:function(t,r){function n(){return new e((function(e,n){o(t,r,e,n)}))}return a=a?a.then(n,n):n()}})}function j(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=x(a,r);if(u){if(u===v)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=p(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===v)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function x(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),v;var o=p(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,v;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,v):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,v)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function C(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function E(t){if(t){var e=t[u];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:k}}function k(){return{value:void 0,done:!0}}return d.prototype=h,i(S,"constructor",{value:h,configurable:!0}),i(h,"constructor",{value:d,configurable:!0}),d.displayName=f(h,l,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,f(t,l,"GeneratorFunction")),t.prototype=Object.create(S),t},t.awrap=function(t){return{__await:t}},w(O.prototype),f(O.prototype,c,(function(){return this})),t.AsyncIterator=O,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new O(s(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},w(S),f(S,l,"Generator"),f(S,u,(function(){return this})),f(S,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=E,C.prototype={constructor:C,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(_),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(u&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,v):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),v},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),_(r),v}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;_(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),v}},t}function i(t,e){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},i(t,e)}function a(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function u(t){return u=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},u(t)}Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0;var c,l=(c=r(111))&&c.__esModule?c:{default:c},f=r(719),s=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&i(t,e)}(p,t);var e,r,c,s=(r=p,c=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,e=u(r);if(c){var o=u(this).constructor;t=Reflect.construct(e,arguments,o)}else t=e.apply(this,arguments);return function(t,e){if(e&&("object"===n(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return a(t)}(this,t)});function p(t,e){var r,n,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,p);var u=null!=e?e:{},c=u.onInit,v=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r}(u,["onInit"]),y=void 0===(null==v?void 0:v.decrypt)?null===(n=null==v?void 0:v.encrypt)||void 0===n||n:null==v?void 0:v.decrypt;(r=s.call(this,t,Object.assign({metadata:null,encrypt:!0,decrypt:y},null!=v?v:{}),i)).setLocalStorageValue=function(){var t=r.config.localStorageKey,e=(0,f.formatToStore)(r.getStateClone(),{stringify:!0}),n=r.config.encrypt;n&&(e=("function"==typeof n?n:function(t){return btoa(t)})(e)),localStorage.setItem(t,e)},r.getLocalStorageValue=function(){var t=r.config.localStorageKey,e=localStorage.getItem(t),n=r.config.decrypt;return n&&e&&(e=("function"==typeof n?n:function(t){return atob(t)})(e)),e},r.onInit=function(t){var e,n,i,u,c=t.setState;return e=a(r),n=void 0,i=void 0,u=o().mark((function t(){var e,r,n;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this.config.localStorageKey){t.next=3;break}return t.abrupt("return");case 3:if(null!==(e=this.getLocalStorageValue())){t.next=7;break}return this.setLocalStorageValue(),t.abrupt("return");case 7:r=JSON.parse(e),n=(0,f.formatFromStore)(r),c(n);case 10:case"end":return t.stop()}}),t,this)})),new(i||(i=Promise))((function(t,r){function o(t){try{c(u.next(t))}catch(t){r(t)}}function a(t){try{c(u.throw(t))}catch(t){r(t)}}function c(e){var r;e.done?t(e.value):(r=e.value,r instanceof i?r:new i((function(t){t(r)}))).then(o,a)}c((u=u.apply(e,n||[])).next())}))},r.onStateChanged=function(){r.setLocalStorageValue()},r.setState=function(t){var e=t.invokerSetState,n=t.state;r.state=n,(l.default.unstable_batchedUpdates||function(t){return t()})((function(){null==e||e(n),r.subscribers.forEach((function(t){t!==e&&t(n)}))}))};var d=r.getConfigCallbackParam({});return r.onInit(d),null==c||c(d),r}return e=p,Object.defineProperty(e,"prototype",{writable:!1}),e}(f.GlobalStore);e.GlobalStore=s},530:(t,e)=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0})},991:(t,e,r)=>{"use strict";var n=Object.create?function(t,e,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(e,r);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,o)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]},o=function(t,e){for(var r in t)"default"===r||Object.prototype.hasOwnProperty.call(e,r)||n(e,t,r)};Object.defineProperty(e,"__esModule",{value:!0}),o(r(774),e),o(r(530),e)},719:function(t,e,r){var n;n=t=>{return e={774:(t,e,r)=>{"use strict";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function i(t){var e=function(t,e){if("object"!==n(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,"string");if("object"!==n(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"===n(e)?e:String(e)}Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=e.formatToStore=e.formatFromStore=void 0;var a=r(684);Object.defineProperty(e,"formatFromStore",{enumerable:!0,get:function(){return a.formatFromStore}}),Object.defineProperty(e,"formatToStore",{enumerable:!0,get:function(){return a.formatToStore}});var u=r(684),c=r(156),l=function(){function t(e){var r=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.state=e,this.setterConfig=a,this.subscribers=new Set,this.config={metadata:null},this.onInit=null,this.onStateChanged=null,this.onSubscribed=null,this.computePreventStateChange=null,this.onInitializeStore=function(){var t=r.onInit,e=r.config.onInit;if(t||e){var n=r.getConfigCallbackParam({});null==t||t(n),null==e||e(n)}},this.getStateClone=function(){return(0,u.clone)(r.state)},this.getMetadataClone=function(){var t,e;return(0,u.clone)(null!==(e=null===(t=r.config)||void 0===t?void 0:t.metadata)&&void 0!==e?e:null)},this.setState=function(t){var e=t.invokerSetState,n=t.state;r.state=n,null==e||e(n),r.subscribers.forEach((function(t){t!==e&&t(n)}))},this.setMetadata=function(t){var e,n="function"==typeof t?t(r.getMetadataClone()):t;r.config=Object.assign(Object.assign({},null!==(e=r.config)&&void 0!==e?e:{}),{metadata:n})},this.getConfigCallbackParam=function(t){var e=t.invokerSetState;return{setMetadata:r.setMetadata,getMetadata:r.getMetadataClone,getState:r.getStateClone,setState:r.getSetStateWrapper({invokerSetState:e}),actions:r.getStoreActionsMap({})}},this.getHook=function(){return function(){var t,e=function(t){if(Array.isArray(t))return t}(t=(0,c.useState)((function(){return r.state})))||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],c=!0,l=!1;try{for(i=(r=r.call(t)).next;!(c=(n=i.call(r)).done)&&(u.push(n.value),2!==u.length);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(l)throw o}}return u}}(t)||function(t,e){if(t){if("string"==typeof t)return o(t,2);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(t,2):void 0}}(t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(),n=e[0],i=e[1];return(0,c.useEffect)((function(){r.subscribers.add(i);var t=r.onSubscribed,e=r.config.onSubscribed;if(t||e){var n=r.getConfigCallbackParam({invokerSetState:i});null==t||t(n),null==e||e(n)}return function(){r.subscribers.delete(i)}}),[]),[n,r.getStateOrchestrator(i),r.getMetadataClone()]}},this.getHookDecoupled=function(){var t=r.getStateClone,e=r.getMetadataClone;return[t,r.getStateOrchestrator(),e]},this.getSetStateWrapper=function(){var t=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}).invokerSetState;return function(e){r.computeSetState({invokerSetState:t,setter:e})}},this.hasStateCallbacks=function(){var t=r.computePreventStateChange,e=r.onStateChanged,n=r.config,o=n.computePreventStateChange,i=n.onStateChanged;return!!(t||o||e||i)},this.computeSetState=function(t){var e=t.setter,n=t.invokerSetState,o="function"==typeof e,i=r.getStateClone(),a=o?e(i):e,u=r.hasStateCallbacks(),c=u&&r.getStoreActionsMap({}),l=u&&r.getSetStateWrapper({invokerSetState:n}),f={setMetadata:r.setMetadata,getMetadata:r.getMetadataClone,setState:l,getState:r.getStateClone,actions:c,previousState:i,state:a},s=r.computePreventStateChange,p=r.config.computePreventStateChange;if(!s&&!p||!(null==s?void 0:s(f))&&!(null==p?void 0:p(f))){r.setState({invokerSetState:n,state:a});var v=r.onStateChanged,y=r.config.onStateChanged;(v||y)&&(null==v||v(f),null==y||y(f))}},this.getStoreActionsMap=function(t){var e=t.invokerSetState;if(!r.setterConfig)return null;var n=r.setterConfig,o=r.setMetadata,a=n,u=Object.keys(a),c=r.getSetStateWrapper({invokerSetState:e}),l=r.getStateClone,f=r.getMetadataClone,s=u.reduce((function(t,e){return Object.assign(Object.assign({},t),(r={},u=function(){for(var t=a[e],r=arguments.length,n=new Array(r),i=0;i<r;i++)n[i]=arguments[i];var u=t.apply(s,n);return"function"!=typeof u&&function(t){throw new Error("[WRONG CONFIGURATION!]: Every key inside the storeActionsConfig must be a higher order function that returns a function \n[".concat(t,"]: key is not a valid function, try something like this: \n{\n\n ").concat(t,": (param) => ({ setState, getState, setMetadata, getMetadata }) => {\n\n setState((state) => ({ ...state, ...param }))\n\n }\n\n}\n"))}(e),u.call(s,{setState:c,getState:l,setMetadata:o,getMetadata:f})},(n=i(n=e))in r?Object.defineProperty(r,n,{value:u,enumerable:!0,configurable:!0,writable:!0}):r[n]=u,r));var r,n,u}),{});return s},this.config=Object.assign({metadata:null},null!=n?n:{}),this.onInitializeStore()}var e,r;return e=t,(r=[{key:"getStateOrchestrator",value:function(t){return this.setterConfig?this.getStoreActionsMap({invokerSetState:t}):this.getSetStateWrapper({invokerSetState:t})}}])&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,i(n.key),n)}}(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),t}();e.GlobalStore=l},530:(t,e)=>{"use strict";Object.defineProperty(e,"__esModule",{value:!0})},991:(t,e,r)=>{"use strict";var n=Object.create?function(t,e,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(e,r);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,o)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]},o=function(t,e){for(var r in t)"default"===r||Object.prototype.hasOwnProperty.call(e,r)||n(e,t,r)};Object.defineProperty(e,"__esModule",{value:!0}),o(r(530),e),o(r(774),e)},684:function(t){t.exports=(()=>{"use strict";var t={991:(t,e,r)=>{var n=Object.create?function(t,e,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(e,r);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,o)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]};Object.defineProperty(e,"__esModule",{value:!0}),function(t,e){for(var r in t)"default"===r||Object.prototype.hasOwnProperty.call(e,r)||n(e,t,r)}(r(729),e)},729:(t,e)=>{function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function o(t,e,r){return(e=function(t){var e=function(t,e){if("object"!==n(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,"string");if("object"!==n(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"===n(e)?e:String(e)}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}Object.defineProperty(e,"__esModule",{value:!0}),e.formatToStore=e.formatFromStore=e.isPrimitive=e.isRegex=e.isDate=e.isString=e.isBoolean=e.isNumber=e.isNil=e.clone=void 0,e.clone=function(t){if((0,e.isPrimitive)(t)||(0,e.isDate)(t))return t;if(Array.isArray(t))return t.map((function(t){return(0,e.clone)(t)}));if(t instanceof Map){var r=Array.from(t.entries());return new Map(r.map((function(t){return(0,e.clone)(t)})))}if(t instanceof Set){var n=Array.from(t.values());return new Set(n.map((function(t){return(0,e.clone)(t)})))}return t instanceof RegExp?new RegExp(t.toString()):t instanceof Error?new Error(t.message):Object.keys(t).reduce((function(r,n){var i=t[n];return Object.assign(Object.assign({},r),o({},n,(0,e.clone)(i)))}),{})},e.isNil=function(t){return null==t},e.isNumber=function(t){return"number"==typeof t},e.isBoolean=function(t){return"boolean"==typeof t},e.isString=function(t){return"string"==typeof t},e.isDate=function(t){return t instanceof Date},e.isRegex=function(t){return t instanceof RegExp},e.isPrimitive=function(t){return(0,e.isNil)(t)||(0,e.isNumber)(t)||(0,e.isBoolean)(t)||(0,e.isString)(t)||"symbol"===n(t)},e.formatFromStore=function(t){return function(t){var n,i;if((0,e.isPrimitive)(t))return t;if("date"===(null==t?void 0:t.$t))return new Date(t.$v);if("map"===(null==t?void 0:t.$t)){var a=(null!==(n=t.$v)&&void 0!==n?n:[]).map((function(t){var n,o=function(t){if(Array.isArray(t))return t}(n=t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],c=!0,l=!1;try{for(i=(r=r.call(t)).next;!(c=(n=i.call(r)).done)&&(u.push(n.value),2!==u.length);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(l)throw o}}return u}}(n)||function(t,e){if(t){if("string"==typeof t)return r(t,2);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)?r(t,2):void 0}}(n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(),i=o[0],a=o[1];return[i,(0,e.formatFromStore)(a)]}));return new Map(a)}if("set"===(null==t?void 0:t.$t)){var u=null!==(i=t.$v)&&void 0!==i?i:[].map((function(t){return(0,e.formatFromStore)(t)}));return new Set(u)}return"regex"===(null==t?void 0:t.$t)?new RegExp(t.$v):"error"===(null==t?void 0:t.$t)?new Error(t.$v):Array.isArray(t)?t.map((function(t){return(0,e.formatFromStore)(t)})):Object.keys(t).reduce((function(r,n){var i=t[n];return Object.assign(Object.assign({},r),o({},n,(0,e.formatFromStore)(i)))}),{})}((0,e.clone)(t))},e.formatToStore=function(t){var r,n=(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{stringify:!1}).stringify,i=(r=(0,e.clone)(t),(0,e.isPrimitive)(r)?r:Array.isArray(r)?r.map((function(t){return(0,e.formatToStore)(t)})):r instanceof Map?{$t:"map",$v:Array.from(r.entries()).map((function(t){return(0,e.formatToStore)(t)}))}:r instanceof Set?{$t:"set",$v:Array.from(r.values()).map((function(t){return(0,e.formatToStore)(t)}))}:(0,e.isDate)(r)?{$t:"date",$v:r.toISOString()}:(0,e.isRegex)(r)?{$t:"regex",$v:r.toString()}:r instanceof Error?{$t:"error",$v:r.message}:Object.keys(r).reduce((function(t,n){var i=r[n];return Object.assign(Object.assign({},t),o({},n,(0,e.formatToStore)(i)))}),{}));return n?JSON.stringify(i):i}}},e={};return function r(n){var o=e[n];if(void 0!==o)return o.exports;var i=e[n]={exports:{}};return t[n](i,i.exports,r),i.exports}(991)})()},156:e=>{"use strict";e.exports=t}},r={},function t(n){var o=r[n];if(void 0!==o)return o.exports;var i=r[n]={exports:{}};return e[n].call(i.exports,i,i.exports,t),i.exports}(991);var e,r},t.exports=n(r(156))},156:e=>{"use strict";e.exports=t},111:t=>{"use strict";t.exports=e}},n={},function t(e){var o=n[e];if(void 0!==o)return o.exports;var i=n[e]={exports:{}};return r[e].call(i.exports,i,i.exports,t),i.exports}(991);var r,n}));
@@ -0,0 +1 @@
1
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './GlobalStore';
2
+ export * from './GlobalStore.types';
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "react-global-state-hooks",
3
- "version": "1.0.28",
3
+ "version": "1.1.2",
4
4
  "description": "This is a package to easily handling global-state across your react components No-redux, No-context.",
5
- "main": "lib/GlobalStore.js",
5
+ "main": "lib/bundle.js",
6
+ "types": "lib/index.d.ts",
6
7
  "files": [
7
8
  "lib"
8
9
  ],
@@ -10,7 +11,7 @@
10
11
  "test:debug": "node --inspect-brk node_modules/.bin/jest --watch --runInBand",
11
12
  "test:quick": "jest --maxWorkers=4 -c --no-watchman -u",
12
13
  "test:coverage": "jest --maxWorkers=4 -c --colors --no-watchman --verbose --coverage",
13
- "build": "tsc",
14
+ "build": "webpack --config webpack.config.js",
14
15
  "prepare": "npm run build",
15
16
  "version": "npm run format && git add -A src",
16
17
  "postversion": "git push && git push --tags"
@@ -36,12 +37,18 @@
36
37
  },
37
38
  "homepage": "https://github.com/johnny-quesada-developer/react-global-state-hooks#readme",
38
39
  "devDependencies": {
40
+ "@babel/core": "^7.21.3",
41
+ "@babel/plugin-transform-modules-commonjs": "^7.21.2",
42
+ "@babel/preset-env": "^7.20.2",
43
+ "@babel/preset-react": "^7.18.6",
44
+ "@babel/preset-typescript": "^7.21.0",
39
45
  "@types/jest": "^26.0.17",
40
46
  "@types/react": "^17.0.0",
41
47
  "@types/react-dom": "^17.0.0",
42
48
  "@types/react-test-renderer": "^17.0.0",
43
49
  "@typescript-eslint/eslint-plugin": "^4.9.0",
44
50
  "@typescript-eslint/parser": "^4.9.0",
51
+ "babel-loader": "^9.1.2",
45
52
  "eslint": "^7.15.0",
46
53
  "eslint-config-airbnb": "^18.2.1",
47
54
  "eslint-plugin-import": "^2.22.1",
@@ -49,20 +56,20 @@
49
56
  "eslint-plugin-react": "^7.21.5",
50
57
  "eslint-plugin-react-hooks": "^4.2.0",
51
58
  "jest": "^26.6.3",
52
- "json-storage-formatter": "^1.0.6",
53
59
  "react": "^17.0.0",
54
60
  "react-dom": "^17.0.0",
55
- "react-native-global-state-hooks": "^2.1.16",
56
61
  "react-test-renderer": "^17.0.1",
57
62
  "ts-jest": "^26.4.4",
58
- "typescript": "^4.1.2"
63
+ "ts-loader": "^9.4.2",
64
+ "typescript": "^4.1.2",
65
+ "webpack": "^5.76.3",
66
+ "webpack-cli": "^5.0.1"
59
67
  },
60
68
  "peerDependencies": {
61
- "json-storage-formatter": ">=1.0.6",
62
69
  "react": ">=17.0.0",
63
70
  "react-dom": ">=17.0.0"
64
71
  },
65
72
  "dependencies": {
66
- "react-native-global-state-hooks": ">=2.1.16"
73
+ "react-native-global-state-hooks": "^3.0.1"
67
74
  }
68
75
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,WAAW,IAAI,eAAe,EAC/B,MAAM,iCAAiC,CAAC;AAEzC,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,iBAAiB,EACjB,wBAAwB,EACxB,WAAW,EACZ,MAAM,qBAAqB,CAAC;AAE7B,qBAAa,WAAW,CACtB,MAAM,EACN,SAAS,GAAG,IAAI,EAChB,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GACnB,IAAI,GAAG,WAAW,CAAC,MAAM,CAAC,CAC9B,SAAQ,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC;IACxD;;;SAGK;IACE,OAAO,EAAE,MAAM,MAAM;QAC1B,MAAM;QACN,YAAY,SAAS,WAAW,CAAC,MAAM,CAAC,GACpC,WAAW,CAAC,MAAM,CAAC,GACnB,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC;QAC3D,SAAS;KACV,CAAC;IAEF;;;SAGK;IACE,gBAAgB,EAAE,MAAM;QAC7B,MAAM,MAAM;QACZ,YAAY,SAAS,WAAW,CAAC,MAAM,CAAC,GACpC,WAAW,CAAC,MAAM,CAAC,GACnB,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC;QAC3D,MAAM,SAAS;KAChB,CAAC;IAEF;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IAErE;;;;;;;;;;;SAWK;gBAEH,KAAK,EAAE,MAAM,EACb,EACE,MAAM,EAAE,YAAY,EACpB,GAAG,MAAM,EACV,GAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAM,EAC1D,YAAY,GAAE,YAAY,GAAG,IAAW;IAsB1C,SAAS,CAAC,oBAAoB,aAoB5B;IAEF,SAAS,CAAC,oBAAoB,eAkB5B;IAEF;;;OAGG;IACH,SAAS,CAAC,MAAM,kBAEb,yBAAyB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,mBAgB1D;IAEF,SAAS,CAAC,cAAc,aAEtB;IAEF;;;;;SAKK;IACL,SAAS,CAAC,QAAQ;eAIT,MAAM;0BACK,MAAM,QAAQ,CAAC,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;eAoB9D;CACH"}
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalStore = void 0;
4
- const tslib_1 = require("tslib");
5
- const react_dom_1 = tslib_1.__importDefault(require("react-dom"));
6
- const react_native_global_state_hooks_1 = require("react-native-global-state-hooks");
7
- class GlobalStore extends react_native_global_state_hooks_1.GlobalStore {
8
- /**
9
- * Create a new instance of the GlobalStore
10
- * @param {TState} state - The initial state
11
- * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
12
- * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
13
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
14
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
15
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
16
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
17
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
18
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.localStorageKey - The key to use to store the state in the localStorage (optional) (default: null) if not null the state will be stored in the localStorage
19
- * */
20
- constructor(state, _a = {}, setterConfig) {
21
- var _b;
22
- var { onInit: onInitConfig } = _a, config = tslib_1.__rest(_a, ["onInit"]);
23
- if (setterConfig === void 0) { setterConfig = null; }
24
- const decrypt = (config === null || config === void 0 ? void 0 : config.decrypt) === undefined ? (_b = config === null || config === void 0 ? void 0 : config.encrypt) !== null && _b !== void 0 ? _b : true : config === null || config === void 0 ? void 0 : config.decrypt;
25
- super(state, Object.assign({ metadata: null, encrypt: true, decrypt }, (config !== null && config !== void 0 ? config : {})), setterConfig);
26
- this.setLocalStorageValue = () => {
27
- const { localStorageKey } = this.config;
28
- let stateToStore = (0, react_native_global_state_hooks_1.formatToStore)(this.getStateClone(), {
29
- stringify: true,
30
- });
31
- const { encrypt } = this.config;
32
- if (encrypt) {
33
- const isEncryptCallback = typeof encrypt === 'function';
34
- const encryptCallback = (isEncryptCallback ? encrypt : (value) => btoa(value));
35
- stateToStore = encryptCallback(stateToStore);
36
- }
37
- localStorage.setItem(localStorageKey, stateToStore);
38
- };
39
- this.getLocalStorageValue = () => {
40
- const { localStorageKey } = this.config;
41
- let storedState = localStorage.getItem(localStorageKey);
42
- const { decrypt } = this.config;
43
- if (decrypt && storedState) {
44
- const isDecryptCallback = typeof decrypt === 'function';
45
- const decryptCallback = (isDecryptCallback ? decrypt : (value) => atob(value));
46
- storedState = decryptCallback(storedState);
47
- }
48
- return storedState;
49
- };
50
- /**
51
- * This method will be called once the store is created after the constructor,
52
- * this method is different from the onInit of the confg property and it won't be overriden
53
- */
54
- this.onInit = ({ setState, }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
55
- const { localStorageKey } = this.config;
56
- if (!localStorageKey)
57
- return;
58
- const storedState = this.getLocalStorageValue();
59
- if (storedState === null) {
60
- this.setLocalStorageValue();
61
- return;
62
- }
63
- const jsonParsed = JSON.parse(storedState);
64
- const state = (0, react_native_global_state_hooks_1.formatFromStore)(jsonParsed);
65
- setState(state);
66
- });
67
- this.onStateChanged = () => {
68
- this.setLocalStorageValue();
69
- };
70
- /**
71
- * set the state and update all the subscribers,
72
- * In react web ReacDom allows to batch the state updates, this method will use the unstable_batchedUpdates method if it exists
73
- * @param {StateSetter<TState>} setter - The setter function or the value to set
74
- * @param {React.Dispatch<React.SetStateAction<TState>>} invokerSetState - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
75
- * */
76
- this.setState = ({ invokerSetState, state, }) => {
77
- // update the state
78
- this.state = state;
79
- const unstable_batchedUpdates = react_dom_1.default.unstable_batchedUpdates ||
80
- ((callback) => callback());
81
- unstable_batchedUpdates(() => {
82
- // execute first the callback of the component that invoked the state change
83
- invokerSetState === null || invokerSetState === void 0 ? void 0 : invokerSetState(state);
84
- // update all the subscribers
85
- this.subscribers.forEach((setState) => {
86
- if (setState === invokerSetState)
87
- return;
88
- setState(state);
89
- });
90
- });
91
- };
92
- const parameters = this.getConfigCallbackParam({});
93
- this.onInit(parameters);
94
- onInitConfig === null || onInitConfig === void 0 ? void 0 : onInitConfig(parameters);
95
- }
96
- }
97
- exports.GlobalStore = GlobalStore;
@@ -1 +0,0 @@
1
- {"version":3,"file":"GlobalStore.types.d.ts","sourceRoot":"","sources":["../src/GlobalStore.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,gBAAgB,MAAM,uDAAuD,CAAC;AAE1F,MAAM,MAAM,WAAW,CAAC,MAAM,IAAI,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AAEvE,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI,gBAAgB,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;AAEzE,MAAM,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,IAAI,gBAAgB,CAAC,UAAU,CAC5E,MAAM,EACN,SAAS,CACV,CAAC;AAEF,MAAM,WAAW,sBAAsB,CAAC,MAAM,EAAE,SAAS,CACvD,SAAQ,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC;CAAG;AAEvE,MAAM,MAAM,sBAAsB,CAChC,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAE7E,MAAM,MAAM,wBAAwB,CAClC,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAE/E,MAAM,MAAM,iBAAiB,CAC3B,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAExE;;;;;;;;;KASK;AACL,MAAM,MAAM,iBAAiB,CAC3B,MAAM,EACN,SAAS,EACT,YAAY,SACR,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GAC1D,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,IAC7E,gBAAgB,CAAC,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG;IACxE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;IAChD,OAAO,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;CACjD,CAAC"}
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });