UNPKG

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 11.4 kB
var t,e;t=this,e=(t,e,r)=>(()=>{"use strict";var n={493:e=>{e.exports=t},466:t=>{t.exports=e},413:t=>{t.exports=r}},o={};function a(t){var e=o[t];if(void 0!==e)return e.exports;var r=o[t]={exports:{}};return n[t](r,r.exports,a),r.exports}var i={};return(()=>{var t=i;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(){r=function(){return n};var t,n={},o=Object.prototype,a=o.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},c="function"==typeof Symbol?Symbol:{},u=c.iterator||"@@iterator",s=c.asyncIterator||"@@asyncIterator",l=c.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 h(t,e,r,n){var o=e&&e.prototype instanceof m?e:m,a=Object.create(o.prototype),c=new G(n||[]);return i(a,"_invoke",{value:E(t,r,c)}),a}function y(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}n.wrap=h;var p="suspendedStart",v="suspendedYield",g="executing",d="completed",b={};function m(){}function w(){}function S(){}var j={};f(j,u,(function(){return this}));var x=Object.getPrototypeOf,O=x&&x(x(R([])));O&&O!==o&&a.call(O,u)&&(j=O);var _=S.prototype=m.prototype=Object.create(j);function A(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function L(t,r){function n(o,i,c,u){var s=y(t[o],t,i);if("throw"!==s.type){var l=s.arg,f=l.value;return f&&"object"==e(f)&&a.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,c,u)}),(function(t){n("throw",t,c,u)})):r.resolve(f).then((function(t){l.value=t,c(l)}),(function(t){return n("throw",t,c,u)}))}u(s.arg)}var o;i(this,"_invoke",{value:function(t,e){function a(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(a,a):a()}})}function E(e,r,n){var o=p;return function(a,i){if(o===g)throw new Error("Generator is already running");if(o===d){if("throw"===a)throw i;return{value:t,done:!0}}for(n.method=a,n.arg=i;;){var c=n.delegate;if(c){var u=k(c,n);if(u){if(u===b)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=g;var s=y(e,r,n);if("normal"===s.type){if(o=n.done?d:v,s.arg===b)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=d,n.method="throw",n.arg=s.arg)}}}function k(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,k(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),b;var a=y(o,e.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,b;var i=a.arg;return i?i.done?(r[e.resultName]=i.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,b):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,b)}function I(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 P(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function G(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(I,this),this.reset(!0)}function R(r){if(r||""===r){var n=r[u];if(n)return n.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var o=-1,i=function e(){for(;++o<r.length;)if(a.call(r,o))return e.value=r[o],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(e(r)+" is not iterable")}return w.prototype=S,i(_,"constructor",{value:S,configurable:!0}),i(S,"constructor",{value:w,configurable:!0}),w.displayName=f(S,l,"GeneratorFunction"),n.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===w||"GeneratorFunction"===(e.displayName||e.name))},n.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,S):(t.__proto__=S,f(t,l,"GeneratorFunction")),t.prototype=Object.create(_),t},n.awrap=function(t){return{__await:t}},A(L.prototype),f(L.prototype,s,(function(){return this})),n.AsyncIterator=L,n.async=function(t,e,r,o,a){void 0===a&&(a=Promise);var i=new L(h(t,e,r,o),a);return n.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},A(_),f(_,l,"Generator"),f(_,u,(function(){return this})),f(_,"toString",(function(){return"[object Generator]"})),n.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}},n.values=R,G.prototype={constructor:G,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(P),!e)for(var r in this)"t"===r.charAt(0)&&a.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],c=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=a.call(i,"catchLoc"),s=a.call(i,"finallyLoc");if(u&&s){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(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&a.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o.finallyLoc,b):this.complete(i)},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),b},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),P(r),b}},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;P(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:R(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),b}},n}function n(t,e){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},n(t,e)}function o(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 c(t)}function c(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(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var s=a(413),l=a(493),f=a(466),h=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&&n(t,e)}(h,t);var e,a,i,s=(a=h,i=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(a);if(i){var r=u(this).constructor;t=Reflect.construct(e,arguments,r)}else t=e.apply(this,arguments);return o(this,t)});function h(t){var e,n,a=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,h),(e=s.call(this,t,a)).asyncStorage=null,e.getMetadata=function(){var t,r;return(null===(t=e.asyncStorage)||void 0===t?void 0:t.key)?Object.assign({isAsyncStorageReady:!1,asyncStorageKey:e.asyncStorage.key},null!==(r=e.metadata)&&void 0!==r?r:{}):e.metadata},e._onInitialize=function(t){var n,o,a,i=t.setState,u=t.getState,s=t.setMetadata;return n=c(e),o=void 0,a=r().mark((function t(){var e;return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(h.isAsyncStorageAvailable(this.asyncStorage)){t.next=2;break}return t.abrupt("return");case 2:return s((function(t){return Object.assign(Object.assign({},null!=t?t:{}),{isAsyncStorageReady:!1})})),t.next=5,(0,l.getAsyncStorageItem)(this.asyncStorage);case 5:e=t.sent,s((function(t){return Object.assign(Object.assign({},null!=t?t:{}),{isAsyncStorageReady:!0})})),null===e&&(e=u(),(0,f.setAsyncStorageItem)(e,this.asyncStorage)),i(e,{forceUpdate:!0});case 9:case"end":return t.stop()}}),t,this)})),new(o||(o=Promise))((function(t,e){function r(t){try{c(a.next(t))}catch(t){e(t)}}function i(t){try{c(a.throw(t))}catch(t){e(t)}}function c(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(r,i)}c((a=a.apply(n,[])).next())}))},e._onChange=function(t){var r=t.getState;h.isAsyncStorageAvailable(e.asyncStorage)&&(0,f.setAsyncStorageItem)(r(),e.asyncStorage)},e.onInitialize=null,e.onChange=null,e.onInit=function(t){var r;null===(r=e._onInitialize)||void 0===r||r.call(c(e),t)},e.onStateChanged=function(t){var r;null===(r=e._onChange)||void 0===r||r.call(c(e),t)},e.asyncStorage=null!==(n=a.asyncStorage)&&void 0!==n?n:null,e.setMetadata((function(t){var r,n;return Object.assign(Object.assign({},null!=t?t:{}),{isAsyncStorageReady:!1,asyncStorageKey:null!==(n=null===(r=e.asyncStorage)||void 0===r?void 0:r.key)&&void 0!==n?n:null})})),e.constructor!==h?o(e):(e.initialize(),e)}return e=h,Object.defineProperty(e,"prototype",{writable:!1}),e}(s.GlobalStoreAbstract);t.GlobalStore=h,h.isAsyncStorageAvailable=function(t){return Boolean(null==t?void 0:t.key)},t.default=h})(),i})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./getAsyncStorageItem.js"),require("./setAsyncStorageItem.js"),require("react-hooks-global-states/GlobalStoreAbstract")):"function"==typeof define&&define.amd?define(["./getAsyncStorageItem.js","./setAsyncStorageItem.js","react-hooks-global-states/GlobalStoreAbstract"],e):"object"==typeof exports?exports["react-native-global-state-hooks"]=e(require("./getAsyncStorageItem.js"),require("./setAsyncStorageItem.js"),require("react-hooks-global-states/GlobalStoreAbstract")):t["react-native-global-state-hooks"]=e(t["./getAsyncStorageItem.js"],t["./setAsyncStorageItem.js"],t["react-hooks-global-states/GlobalStoreAbstract"]);