react-native-global-state-hooks
Version:
This is a package to easily handling global-state across your react-native-components No-redux... The library now includes @react-native-async-storage/async-storage to persist your state across sessions... if you want to keep using the old version without
1 lines • 10.9 kB
JavaScript
var t,e;t=this,e=(t,e,r,o,n)=>(()=>{"use strict";var a={70:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0});var o,n=(o=r(313))&&o.__esModule?o:{default:o};e.default=function(t,e){try{var r=t();return(0,n.default)(r)?r.then(function(t){return{result:t,error:null}}).catch(function(t){return{result:null!=e?e:null,error:t}}):{result:r,error:null}}catch(t){return{result:null!=e?e:null,error:t}}}},114:e=>{e.exports=t},313:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(t){return Boolean(t&&"function"==typeof t.then)}},330:t=>{t.exports=e},413:t=>{t.exports=r},536:t=>{t.exports=o},773:t=>{t.exports=n}},i={};function u(t){var e=i[t];if(void 0!==e)return e.exports;var r=i[t]={exports:{}};return a[t](r,r.exports,u),r.exports}var s={};return(()=>{var t=s;function e(t){return e="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},e(t)}function r(){var t,e,n="function"==typeof Symbol?Symbol:{},a=n.iterator||"@@iterator",i=n.toStringTag||"@@toStringTag";function u(r,n,a,i){var u=n&&n.prototype instanceof c?n:c,f=Object.create(u.prototype);return o(f,"_invoke",function(r,o,n){var a,i,u,c=0,f=n||[],l=!1,d={p:0,n:0,v:t,a:v,f:v.bind(t,4),d:function(e,r){return a=e,i=0,u=t,d.n=r,s}};function v(r,o){for(i=r,u=o,e=0;!l&&c&&!n&&e<f.length;e++){var n,a=f[e],v=d.p,h=a[2];r>3?(n=h===o)&&(u=a[(i=a[4])?5:(i=3,3)],a[4]=a[5]=t):a[0]<=v&&((n=r<2&&v<a[1])?(i=0,d.v=o,d.n=a[1]):v<h&&(n=r<3||a[0]>o||o>h)&&(a[4]=r,a[5]=o,d.n=h,i=0))}if(n||r>1)return s;throw l=!0,o}return function(n,f,h){if(c>1)throw TypeError("Generator is already running");for(l&&1===f&&v(f,h),i=f,u=h;(e=i<2?t:u)||!l;){a||(i?i<3?(i>1&&(d.n=-1),v(i,u)):d.n=u:d.v=u);try{if(c=2,a){if(i||(n="next"),e=a[n]){if(!(e=e.call(a,u)))throw TypeError("iterator result is not an object");if(!e.done)return e;u=e.value,i<2&&(i=0)}else 1===i&&(e=a.return)&&e.call(a),i<2&&(u=TypeError("The iterator does not provide a '"+n+"' method"),i=1);a=t}else if((e=(l=d.n<0)?u:r.call(o,d))!==s)break}catch(e){a=t,i=1,u=e}finally{c=1}}return{value:e,done:l}}}(r,a,i),!0),f}var s={};function c(){}function f(){}function l(){}e=Object.getPrototypeOf;var d=[][a]?e(e([][a]())):(o(e={},a,function(){return this}),e),v=l.prototype=c.prototype=Object.create(d);function h(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,o(t,i,"GeneratorFunction")),t.prototype=Object.create(v),t}return f.prototype=l,o(v,"constructor",l),o(l,"constructor",f),f.displayName="GeneratorFunction",o(l,i,"GeneratorFunction"),o(v),o(v,i,"Generator"),o(v,a,function(){return this}),o(v,"toString",function(){return"[object Generator]"}),(r=function(){return{w:u,m:h}})()}function o(t,e,r,n){var a=Object.defineProperty;try{a({},"",{})}catch(t){a=0}o=function(t,e,r,n){if(e)a?a(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r;else{var i=function(e,r){o(t,e,function(t){return this._invoke(e,r,t)})};i("next",0),i("throw",1),i("return",2)}},o(t,e,r,n)}function n(t,r){if(r&&("object"==e(r)||"function"==typeof r))return r;if(void 0!==r)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 a(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(a=function(){return!!t})()}function i(t){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},i(t)}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}var f=function(t,e,r,o){return new(r||(r=Promise))(function(n,a){function i(t){try{s(o.next(t))}catch(t){a(t)}}function u(t){try{s(o.throw(t))}catch(t){a(t)}}function s(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r(function(t){t(e)})).then(i,u)}s((o=o.apply(t,e||[])).next())})},l=function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var d=l(u(536)),v=l(u(70)),h=l(u(773)),y=l(u(330)),p=l(u(413)),g=l(u(114)),S=function(t){function e(t){var o,u,s,c,l,d,S,m=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{metadata:{}};return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),l=this,S=[t,m],d=i(d=e),(o=n(l,a()?Reflect.construct(d,S||[],i(l).constructor):d.apply(l,S))).asyncStorage=null,o.getMetadata=function(){return o.metadata},o.isPersistStorageAvailable=function(){var t;return Boolean(null===(t=o.asyncStorage)||void 0===t?void 0:t.key)},o.trySetStorageItem=function(t){return f(o,void 0,void 0,r().m(function e(){var o,n,a,i=this;return r().w(function(e){for(;;)switch(e.n){case 0:if(o=this.asyncStorage){e.n=1;break}return e.a(2);case 1:return e.n=2,(0,v.default)(function(){var e,r=null===(e=o.adapter)||void 0===e?void 0:e.setItem;return r?r(o.key,t):i.setStorageItem(t)});case 2:if(n=e.v,a=n.error){e.n=3;break}return e.a(2);case 3:this.handleStorageError(a);case 4:return e.a(2)}},e,this)}))},o.updateStateWithValidation=function(t){return f(o,void 0,void 0,r().m(function e(){var o,n,a,i,u=this;return r().w(function(e){for(;;)switch(e.n){case 0:if(o=this.asyncStorage){e.n=1;break}return e.a(2);case 1:if(n=(0,v.default)(function(){var e;return null===(e=o.validator)||void 0===e?void 0:e.call(o,{restored:t,initial:u.getState()})}),a=n.result,!(i=n.error)){e.n=3;break}return this.handleStorageError(i),e.n=2,this.trySetStorageItem(this.getState());case 2:case 4:case 6:case 8:return e.a(2);case 3:if(void 0!==a){e.n=7;break}if(void 0!==t){e.n=5;break}return e.n=4,this.trySetStorageItem(this.getState());case 5:return this.setState(t,{forceUpdate:!0}),e.n=6,this.trySetStorageItem(t);case 7:return this.setState(a,{forceUpdate:!0}),e.n=8,this.trySetStorageItem(a)}},e,this)}))},o.onInit=function(){return f(o,void 0,void 0,r().m(function t(){var e,o,n,a,i,u,s,c=this;return r().w(function(t){for(;;)switch(t.n){case 0:if((e=this.asyncStorage)&&this.isPersistStorageAvailable()){t.n=1;break}return t.a(2);case 1:return this.metadata.isAsyncStorageReady=!1,o=e.versioning,t.n=2,(0,v.default)(function(){return f(c,void 0,void 0,r().m(function t(){var n,a,i,u,s;return r().w(function(t){for(;;)switch(t.n){case 0:if(i=null===(n=e.adapter)||void 0===n?void 0:n.getItem){t.n=1;break}return t.a(2,this.getStorageItem());case 1:return t.n=2,i(e.key);case 2:return u=t.v,s=null!==(a=null==o?void 0:o.version)&&void 0!==a?a:-1,t.a(2,{s:u,v:s})}},t,this)}))});case 2:return n=t.v,a=n.result,i=n.error,t.n=3,(0,v.default)(function(){return f(c,void 0,void 0,r().m(function t(){var n,u,s,c,f,l=this;return r().w(function(t){for(;;)switch(t.n){case 0:if(!i){t.n=2;break}return this.handleStorageError(i),t.n=1,this.updateStateWithValidation(this.getState());case 1:case 3:case 5:case 7:case 9:return t.a(2);case 2:if(a){t.n=4;break}return t.n=3,this.updateStateWithValidation(this.getState());case 4:if(n=a.v===(null==o?void 0:o.version),u=null==o?void 0:o.migrator,!n&&u&&!e.adapter){t.n=6;break}return t.n=5,this.updateStateWithValidation(a.s);case 6:if(s=(0,v.default)(function(){return u({legacy:a.s,initial:l.getState()})}),c=s.result,f=s.error){t.n=8;break}return t.n=7,this.updateStateWithValidation(c);case 8:return this.handleStorageError(f),t.n=9,this.updateStateWithValidation(this.getState())}},t,this)}))});case 3:if(u=t.v,s=u.error,this.metadata.isAsyncStorageReady=!0,s){t.n=4;break}return t.a(2);case 4:this.handleStorageError(s);case 5:return t.a(2)}},t,this)}))},o.onStateChanged=function(t){return f(o,void 0,void 0,r().m(function e(){var o,n,a,i=this;return r().w(function(e){for(;;)switch(e.n){case 0:if((o=this.asyncStorage)&&this.isPersistStorageAvailable()){e.n=1;break}return e.a(2);case 1:return e.n=2,(0,v.default)(function(){var e,r=null===(e=o.adapter)||void 0===e?void 0:e.setItem;return r?r(o.key,t.state):i.setStorageItem(t.state)});case 2:if(n=e.v,a=n.error){e.n=3;break}return e.a(2);case 3:this.handleStorageError(a);case 4:return e.a(2)}},e,this)}))},o.getStorageItem=function(){return f(o,void 0,void 0,r().m(function t(){var e,o;return r().w(function(t){for(;;)switch(t.n){case 0:return t.n=1,g.default.getItem(this.asyncStorage.key);case 1:return e=t.v,o=(0,h.default)(e)?null:(0,y.default)(e),b(this.asyncStorage.key,o),t.a(2,o)}},t,this)}))},o.setStorageItem=function(t){return f(o,void 0,void 0,r().m(function e(){var o,n,a,i,u;return r().w(function(e){for(;;)if(0===e.n)return i={s:t,v:null!==(a=null===(n=null===(o=this.asyncStorage)||void 0===o?void 0:o.versioning)||void 0===n?void 0:n.version)&&void 0!==a?a:-1},u=(0,p.default)(i),e.a(2,g.default.setItem(this.asyncStorage.key,u))},e,this)}))},o.handleStorageError=function(t){var e;if(null===(e=o.asyncStorage)||void 0===e?void 0:e.onError)return o.asyncStorage.onError(t)},o.asyncStorage=null!==(u=m.asyncStorage)&&void 0!==u?u:null,Object.assign(o.getMetadata(),{isAsyncStorageReady:!1,asyncStorageKey:null!==(c=null===(s=o.asyncStorage)||void 0===s?void 0:s.key)&&void 0!==c?c:null}),o.constructor!==e?n(o):(o.initialize(),o)}return 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&&c(t,e)}(e,t),o=e,Object.defineProperty(o,"prototype",{writable:!1}),o;var o}(d.default);function b(t,r){if(!((0,h.default)(r)||"object"===e(r)&&"s"in r&&"v"in r))throw new Error('[react-native-global-state-hooks] The value of the key "'.concat(t,'" is not a valid storage envelope.'))}t.GlobalStore=S,t.default=S})(),s})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./asyncStorageWrapper.js"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("react-hooks-global-states/GlobalStore"),require("json-storage-formatter/isNil")):"function"==typeof define&&define.amd?define(["./asyncStorageWrapper.js","json-storage-formatter/formatFromStore","json-storage-formatter/formatToStore","react-hooks-global-states/GlobalStore","json-storage-formatter/isNil"],e):"object"==typeof exports?exports["react-native-global-state-hooks"]=e(require("./asyncStorageWrapper.js"),require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("react-hooks-global-states/GlobalStore"),require("json-storage-formatter/isNil")):t["react-native-global-state-hooks"]=e(t["./asyncStorageWrapper.js"],t["json-storage-formatter/formatFromStore"],t["json-storage-formatter/formatToStore"],t["react-hooks-global-states/GlobalStore"],t["json-storage-formatter/isNil"]);