react-global-state-hooks 14.1.0-beta.2 → 14.1.0-beta.5

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.
package/GlobalStore.js CHANGED
@@ -1 +1 @@
1
- var t,o;t=this,o=(t,o,e,r)=>(()=>{"use strict";var a={70:(t,o)=>{Object.defineProperty(o,"__esModule",{value:!0}),o.default=function(t){try{return{result:t(),error:null}}catch(t){return{result:null,error:t}}}},266:o=>{o.exports=t},330:t=>{t.exports=o},413:t=>{t.exports=e},773:t=>{t.exports=r}},l={};function n(t){var o=l[t];if(void 0!==o)return o.exports;var e=l[t]={exports:{}};return a[t](e,e.exports,n),e.exports}var i={};return(()=>{var t=i;function o(t){return o="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},o(t)}function e(t,e){if(e&&("object"==o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function r(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(r=function(){return!!t})()}function a(t){return a=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},a(t)}function l(t,o){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,o){return t.__proto__=o,t},l(t,o)}var u=function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var c=n(266),s=u(n(70)),f=u(n(330)),d=u(n(413)),g=u(n(773)),v=function(t){function o(t){var l,n,i,u,c,v=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{metadata:{}};return function(t,o){if(!(t instanceof o))throw new TypeError("Cannot call a class as a function")}(this,o),i=this,c=[t,v],u=a(u=o),(l=e(i,r()?Reflect.construct(u,c||[],a(i).constructor):u.apply(i,c))).localStorage=null,l.isGlobalLocalStorageAvailable=function(){var t;return Boolean((null===(t=l.localStorage)||void 0===t?void 0:t.key)&&(null===globalThis||void 0===globalThis?void 0:globalThis.localStorage))},l._onInitialize=function(t){var o=t.getState,e=l.localStorage;if(e&&l.isGlobalLocalStorageAvailable()){var r=e.versioning,a=(0,s.default)(function(){var t,o,a=null===(t=e.adapter)||void 0===t?void 0:t.getItem;return a?{s:a(e.key),v:null!==(o=null==r?void 0:r.version)&&void 0!==o?o:-1}:l.getLocalStorageItem()}),n=a.result,i=a.error;if(i)return l.handleLocalStorageError(i),void l.updateStateWithValidation(l.getState());if(n){var u=n.v===(null==r?void 0:r.version),c=null==r?void 0:r.migrator;if(u||!c||e.adapter)l.updateStateWithValidation(n.s);else{var f=(0,s.default)(function(){return c({legacy:n.s,initial:l.getState()})}),d=f.result,g=f.error;g?(l.handleLocalStorageError(g),l.updateStateWithValidation(l.getState())):l.updateStateWithValidation(d)}}else l.updateStateWithValidation(o())}},l.trySetLocalStorageItem=function(t){var o=l.localStorage;if(o){var e=(0,s.default)(function(){var e,r=null===(e=o.adapter)||void 0===e?void 0:e.setItem;if(!r)return l.setLocalStorageItem(t);r(o.key,t)}),r=e.error;r&&l.handleLocalStorageError(r)}},l.updateStateWithValidation=function(t){var o=l.localStorage;if(o){var e=(0,s.default)(function(){return o.validator({restored:t,initial:l.getState()})}),r=e.result,a=e.error;if(a)return l.handleLocalStorageError(a),void l.trySetLocalStorageItem(l.getState());l.setState(r),l.trySetLocalStorageItem(r)}},l._onChange=function(t){var o=t.state,e=l.localStorage;if(e&&l.isGlobalLocalStorageAvailable()){var r=(0,s.default)(function(){var t,r=null===(t=e.adapter)||void 0===t?void 0:t.setItem;if(r)return r(e.key,o);l.setLocalStorageItem(o)}),a=r.error;a&&l.handleLocalStorageError(a)}},l.onInitialize=null,l.onChange=null,l.onInit=function(t){var o;null===(o=l._onInitialize)||void 0===o||o.call(l,t)},l.onStateChanged=function(t){var o;null===(o=l._onChange)||void 0===o||o.call(l,t)},l.getLocalStorageItem=function(){var t=globalThis.localStorage.getItem(l.localStorage.key);return(0,g.default)(t)?null:(0,f.default)(t)},l.setLocalStorageItem=function(t){var o,e,r,a={s:t,v:null!==(r=null===(e=null===(o=l.localStorage)||void 0===o?void 0:o.versioning)||void 0===e?void 0:e.version)&&void 0!==r?r:-1},n=(0,d.default)(a);globalThis.localStorage.setItem(l.localStorage.key,n)},l.handleLocalStorageError=function(t){var o;if(null===(o=l.localStorage)||void 0===o?void 0:o.onError)return l.localStorage.onError(t)},l.localStorage=null!==(n=v.localStorage)&&void 0!==n?n:null,l.constructor!==o?e(l):(l.initialize(),l)}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(o&&o.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),o&&l(t,o)}(o,t),n=o,Object.defineProperty(n,"prototype",{writable:!1}),n;var n}(c.GlobalStoreAbstract);t.GlobalStore=v,t.default=v})(),i})(),"object"==typeof exports&&"object"==typeof module?module.exports=o(require("react-hooks-global-states/GlobalStoreAbstract"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("json-storage-formatter/isNil")):"function"==typeof define&&define.amd?define(["react-hooks-global-states/GlobalStoreAbstract","json-storage-formatter/formatFromStore","json-storage-formatter/formatToStore","json-storage-formatter/isNil"],o):"object"==typeof exports?exports["react-global-state-hooks"]=o(require("react-hooks-global-states/GlobalStoreAbstract"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("json-storage-formatter/isNil")):t["react-global-state-hooks"]=o(t["react-hooks-global-states/GlobalStoreAbstract"],t["json-storage-formatter/formatFromStore"],t["json-storage-formatter/formatToStore"],t["json-storage-formatter/isNil"]);
1
+ var t,o;t=this,o=(t,o,e,r)=>(()=>{"use strict";var a={70:(t,o)=>{Object.defineProperty(o,"__esModule",{value:!0}),o.default=function(t){try{return{result:t(),error:null}}catch(t){return{result:null,error:t}}}},266:o=>{o.exports=t},330:t=>{t.exports=o},413:t=>{t.exports=e},773:t=>{t.exports=r}},l={};function n(t){var o=l[t];if(void 0!==o)return o.exports;var e=l[t]={exports:{}};return a[t](e,e.exports,n),e.exports}var i={};return(()=>{var t=i;function o(t){return o="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},o(t)}function e(t,e){if(e&&("object"==o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function r(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(r=function(){return!!t})()}function a(t){return a=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},a(t)}function l(t,o){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,o){return t.__proto__=o,t},l(t,o)}var u=function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var c=n(266),s=u(n(70)),f=u(n(330)),d=u(n(413)),g=u(n(773)),v=function(t){function o(t){var l,n,i,u,c,v=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{metadata:{}};return function(t,o){if(!(t instanceof o))throw new TypeError("Cannot call a class as a function")}(this,o),i=this,c=[t,v],u=a(u=o),(l=e(i,r()?Reflect.construct(u,c||[],a(i).constructor):u.apply(i,c))).localStorage=null,l.isGlobalLocalStorageAvailable=function(){var t;return Boolean((null===(t=l.localStorage)||void 0===t?void 0:t.key)&&(null===globalThis||void 0===globalThis?void 0:globalThis.localStorage))},l._onInitialize=function(t){var o=t.getState,e=l.localStorage;if(e&&l.isGlobalLocalStorageAvailable()){var r=e.versioning,a=(0,s.default)(function(){var t,o,a=null===(t=e.adapter)||void 0===t?void 0:t.getItem;return a?{s:a(e.key),v:null!==(o=null==r?void 0:r.version)&&void 0!==o?o:-1}:l.getLocalStorageItem()}),n=a.result,i=a.error;if(i)return l.handleLocalStorageError(i),void l.updateStateWithValidation(l.getState());if(n){var u=n.v===(null==r?void 0:r.version),c=null==r?void 0:r.migrator;if(u||!c||e.adapter)l.updateStateWithValidation(n.s);else{var f=(0,s.default)(function(){return c({legacy:n.s,initial:l.getState()})}),d=f.result,g=f.error;g?(l.handleLocalStorageError(g),l.updateStateWithValidation(l.getState())):l.updateStateWithValidation(d)}}else l.updateStateWithValidation(o())}},l.trySetLocalStorageItem=function(t){var o=l.localStorage;if(o){var e=(0,s.default)(function(){var e,r=null===(e=o.adapter)||void 0===e?void 0:e.setItem;if(!r)return l.setLocalStorageItem(t);r(o.key,t)}),r=e.error;r&&l.handleLocalStorageError(r)}},l.updateStateWithValidation=function(t){var o=l.localStorage;if(o){var e=(0,s.default)(function(){return o.validator({restored:t,initial:l.getState()})}),r=e.result,a=e.error;if(a)return l.handleLocalStorageError(a),void l.trySetLocalStorageItem(l.getState());void 0!==r?(l.setState(r),l.trySetLocalStorageItem(r)):l.trySetLocalStorageItem(l.getState())}},l._onChange=function(t){var o=t.state,e=l.localStorage;if(e&&l.isGlobalLocalStorageAvailable()){var r=(0,s.default)(function(){var t,r=null===(t=e.adapter)||void 0===t?void 0:t.setItem;if(r)return r(e.key,o);l.setLocalStorageItem(o)}),a=r.error;a&&l.handleLocalStorageError(a)}},l.onInitialize=null,l.onChange=null,l.onInit=function(t){var o;null===(o=l._onInitialize)||void 0===o||o.call(l,t)},l.onStateChanged=function(t){var o;null===(o=l._onChange)||void 0===o||o.call(l,t)},l.getLocalStorageItem=function(){var t=globalThis.localStorage.getItem(l.localStorage.key);return(0,g.default)(t)?null:(0,f.default)(t)},l.setLocalStorageItem=function(t){var o,e,r,a={s:t,v:null!==(r=null===(e=null===(o=l.localStorage)||void 0===o?void 0:o.versioning)||void 0===e?void 0:e.version)&&void 0!==r?r:-1},n=(0,d.default)(a);globalThis.localStorage.setItem(l.localStorage.key,n)},l.handleLocalStorageError=function(t){var o;if(null===(o=l.localStorage)||void 0===o?void 0:o.onError)return l.localStorage.onError(t)},l.localStorage=null!==(n=v.localStorage)&&void 0!==n?n:null,l.constructor!==o?e(l):(l.initialize(),l)}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(o&&o.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),o&&l(t,o)}(o,t),n=o,Object.defineProperty(n,"prototype",{writable:!1}),n;var n}(c.GlobalStoreAbstract);t.GlobalStore=v,t.default=v})(),i})(),"object"==typeof exports&&"object"==typeof module?module.exports=o(require("react-hooks-global-states/GlobalStoreAbstract"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("json-storage-formatter/isNil")):"function"==typeof define&&define.amd?define(["react-hooks-global-states/GlobalStoreAbstract","json-storage-formatter/formatFromStore","json-storage-formatter/formatToStore","json-storage-formatter/isNil"],o):"object"==typeof exports?exports["react-global-state-hooks"]=o(require("react-hooks-global-states/GlobalStoreAbstract"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("json-storage-formatter/isNil")):t["react-global-state-hooks"]=o(t["react-hooks-global-states/GlobalStoreAbstract"],t["json-storage-formatter/formatFromStore"],t["json-storage-formatter/formatToStore"],t["json-storage-formatter/isNil"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-global-state-hooks",
3
- "version": "14.1.0-beta.2",
3
+ "version": "14.1.0-beta.5",
4
4
  "description": "This is a package to easily handling global-state across your react components",
5
5
  "main": "./bundle.js",
6
6
  "types": "./index.d.ts",
@@ -150,7 +150,7 @@
150
150
  },
151
151
  "dependencies": {
152
152
  "json-storage-formatter": "^3.0.2",
153
- "react-hooks-global-states": "14.1.0-beta.3"
153
+ "react-hooks-global-states": "14.1.0-beta.6"
154
154
  },
155
155
  "peerDependencies": {
156
156
  "json-storage-formatter": "^3.0.2",
package/types.d.ts CHANGED
@@ -9,7 +9,10 @@ export type LocalStorageConfig<State> = {
9
9
  key: string;
10
10
  /**
11
11
  * @description Validator function to ensure the integrity of the restored state.
12
- * Receives the restored value and the initial state, must return a valid `State`.
12
+ * Receives the restored value and the initial state... If the function returns a value then
13
+ * that value is used as the new state. If it returns `void` (undefined) then the initial state is used
14
+ * and the localStorage is updated accordingly.
15
+ *
13
16
  * Executes after every initialization from localStorage, including after migration.
14
17
  *
15
18
  * @example
@@ -26,7 +29,7 @@ export type LocalStorageConfig<State> = {
26
29
  validator: (args: {
27
30
  restored: unknown;
28
31
  initial: State;
29
- }) => State;
32
+ }) => State | void;
30
33
  /**
31
34
  * @description Error callback invoked when an exception occurs during any persistence phase.
32
35
  * Use this to log or report issues without throwing.