react-native-global-state-hooks 15.0.2 → 16.0.3-beta.0
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.cjs +5 -0
- package/GlobalStore.d.ts +3 -3
- package/GlobalStore.js +5 -1
- package/GlobalStore.mjs +5 -0
- package/README.md +1209 -202
- package/actions.cjs +1 -0
- package/actions.d.ts +1 -0
- package/actions.js +1 -0
- package/actions.mjs +1 -0
- package/asyncStorageWrapper.cjs +4 -0
- package/asyncStorageWrapper.js +4 -1
- package/asyncStorageWrapper.mjs +4 -0
- package/bundle.cjs +1 -0
- package/bundle.js +1 -1
- package/bundle.mjs +1 -0
- package/createContext.cjs +1 -0
- package/createContext.js +1 -1
- package/createContext.mjs +1 -0
- package/createGlobalState.cjs +1 -0
- package/createGlobalState.d.ts +8 -8
- package/createGlobalState.js +1 -1
- package/createGlobalState.mjs +1 -0
- package/index.d.ts +11 -10
- package/isPromise.cjs +1 -0
- package/isPromise.js +1 -0
- package/isPromise.mjs +1 -0
- package/isRecord.cjs +1 -0
- package/isRecord.js +1 -1
- package/isRecord.mjs +1 -0
- package/package.json +48 -90
- package/shallowCompare.cjs +1 -0
- package/shallowCompare.js +1 -1
- package/shallowCompare.mjs +1 -0
- package/throwWrongKeyOnActionCollectionConfig.cjs +1 -0
- package/throwWrongKeyOnActionCollectionConfig.js +1 -1
- package/throwWrongKeyOnActionCollectionConfig.mjs +1 -0
- package/tryCatch.cjs +1 -0
- package/tryCatch.js +1 -0
- package/tryCatch.mjs +1 -0
- package/types.cjs +1 -0
- package/types.d.ts +15 -4
- package/types.js +1 -1
- package/types.mjs +0 -0
- package/uniqueId.cjs +1 -0
- package/uniqueId.d.ts +1 -1
- package/uniqueId.js +1 -1
- package/uniqueId.mjs +1 -0
- package/webpack.config.js +0 -132
package/GlobalStore.cjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";var k=Object.create;var d=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var I=(r,o)=>{for(var t in o)d(r,t,{get:o[t],enumerable:!0})},m=(r,o,t,e)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of b(o))!w.call(r,a)&&a!==t&&d(r,a,{get:()=>o[a],enumerable:!(e=E(o,a))||e.enumerable});return r};var S=(r,o,t)=>(t=r!=null?k(M(r)):{},m(o||!r||!r.__esModule?d(t,"default",{value:r,enumerable:!0}):t,r)),P=r=>m(d({},"__esModule",{value:!0}),r);var V={};I(V,{GlobalStore:()=>u,default:()=>F});module.exports=P(V);var v=S(require("react-hooks-global-states/GlobalStore")),c=S(require("./tryCatch.cjs")),y=S(require("json-storage-formatter/isNil")),A=S(require("json-storage-formatter/formatFromStore")),C=S(require("json-storage-formatter/formatToStore")),h=S(require("./asyncStorageWrapper.cjs")),p=-1,u=class r extends v.default{constructor(t,e={metadata:{}}){var n,s,i;super(t,e);this.asyncStorage=null;this.getMetadata=()=>this.metadata;this.isPersistStorageAvailable=()=>{var t;return!!((t=this.asyncStorage)!=null&&t.key)};this.trySetStorageItem=async t=>{let e=this.asyncStorage;if(!e)return;let{error:a}=await(0,c.default)(()=>{var s;let n=(s=e.adapter)==null?void 0:s.setItem;return n?n(e.key,t):this.setStorageItem(t)});a&&this.handleStorageError(a)};this.updateStateWithValidation=async t=>{let e=this.asyncStorage;if(!e)return;let{result:a,error:n}=(0,c.default)(()=>{var s;return(s=e.validator)==null?void 0:s.call(e,{restored:t,initial:this.getState()})});if(n){this.handleStorageError(n),await this.trySetStorageItem(this.getState());return}if(a===void 0){if(t===void 0){await this.trySetStorageItem(this.getState());return}this.setState(t,{forceUpdate:!0}),await this.trySetStorageItem(t);return}this.setState(a,{forceUpdate:!0}),await this.trySetStorageItem(a)};this.onInit=async()=>{let t=this.asyncStorage;if(!t||!this.isPersistStorageAvailable())return;this.metadata.isAsyncStorageReady=!1;let e=t.versioning,{result:a,error:n}=await(0,c.default)(async()=>{var l,g;let i=(l=t.adapter)==null?void 0:l.getItem;return i?{s:await i(t.key),v:(g=e==null?void 0:e.version)!=null?g:p}:this.getStorageItem()}),{error:s}=await(0,c.default)(async()=>{if(n){this.handleStorageError(n),await this.updateStateWithValidation(this.getState());return}if(!a){await this.updateStateWithValidation(this.getState());return}let i=a.v===(e==null?void 0:e.version),l=e==null?void 0:e.migrator;if(i||!l||t.adapter){await this.updateStateWithValidation(a.s);return}let{result:g,error:f}=(0,c.default)(()=>l({legacy:a.s,initial:this.getState()}));if(!f){await this.updateStateWithValidation(g);return}this.handleStorageError(f),await this.updateStateWithValidation(this.getState())});this.metadata.isAsyncStorageReady=!0,s&&this.handleStorageError(s)};this.onStateChanged=async t=>{let e=this.asyncStorage;if(!e||!this.isPersistStorageAvailable())return;let{error:a}=await(0,c.default)(()=>{var s;let n=(s=e.adapter)==null?void 0:s.setItem;return n?n(e.key,t.state):this.setStorageItem(t.state)});a&&this.handleStorageError(a)};this.getStorageItem=async()=>{let t=this.asyncStorage.key,e=await h.default.getItem(t),a=(0,y.default)(e)?null:(0,A.default)(e);return x(t,a),a};this.setStorageItem=async t=>{var n,s,i;let e={s:t,v:(i=(s=(n=this.asyncStorage)==null?void 0:n.versioning)==null?void 0:s.version)!=null?i:p},a=(0,C.default)(e);return h.default.setItem(this.asyncStorage.key,a)};this.handleStorageError=t=>{var e,a;if((e=this.asyncStorage)!=null&&e.onError)return this.asyncStorage.onError(t);console.error([`[react-native-global-state-hooks]
|
|
2
|
+
`," asyncStorage sync error:","[hook]:",` ${this._name}`,"[AsyncStorage Key]:",` ${(a=this.asyncStorage)==null?void 0:a.key}`,"[Error]:",` ${t!=null?t:"undefined"}
|
|
3
|
+
|
|
4
|
+
`,"Stacktrace:"].join(`
|
|
5
|
+
`),t.stack)};this.asyncStorage=(n=e.asyncStorage)!=null?n:null,Object.assign(this.getMetadata(),{isAsyncStorageReady:!1,asyncStorageKey:(i=(s=this.asyncStorage)==null?void 0:s.key)!=null?i:null}),this.constructor===r&&this.initialize()}};function x(r,o){if(!(0,y.default)(o)&&!(typeof o=="object"&&"s"in o&&"v"in o))throw new Error(`[react-native-global-state-hooks] The value of the key "${r}" is not a valid storage envelope.`)}var F=u;
|
package/GlobalStore.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ import GlobalStoreBase from "react-hooks-global-states/GlobalStore";
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> | undefined | unknown, PublicStateMutator = keyof ActionsConfig extends never | undefined ? React.Dispatch<React.SetStateAction<State>> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>> extends GlobalStoreBase<State, AsyncMetadata<Metadata>, ActionsConfig> {
|
|
8
8
|
asyncStorage: AsyncStorageConfig<State> | null;
|
|
9
|
-
constructor(state: State);
|
|
10
|
-
constructor(state: State, args: {
|
|
11
|
-
metadata?: Metadata;
|
|
9
|
+
constructor(state: State | (() => State));
|
|
10
|
+
constructor(state: State | (() => State), args: {
|
|
11
|
+
metadata?: Metadata | (() => Metadata);
|
|
12
12
|
callbacks?: GlobalStoreCallbacks<State, PublicStateMutator extends AnyFunction ? null : PublicStateMutator, Metadata>;
|
|
13
13
|
actions?: ActionsConfig;
|
|
14
14
|
name?: string;
|
package/GlobalStore.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
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"]);
|
|
1
|
+
"use strict";var k=Object.create;var d=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var I=(r,o)=>{for(var t in o)d(r,t,{get:o[t],enumerable:!0})},m=(r,o,t,e)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of b(o))!w.call(r,a)&&a!==t&&d(r,a,{get:()=>o[a],enumerable:!(e=E(o,a))||e.enumerable});return r};var S=(r,o,t)=>(t=r!=null?k(M(r)):{},m(o||!r||!r.__esModule?d(t,"default",{value:r,enumerable:!0}):t,r)),P=r=>m(d({},"__esModule",{value:!0}),r);var V={};I(V,{GlobalStore:()=>u,default:()=>F});module.exports=P(V);var v=S(require("react-hooks-global-states/GlobalStore")),c=S(require("./tryCatch.js")),y=S(require("json-storage-formatter/isNil")),A=S(require("json-storage-formatter/formatFromStore")),C=S(require("json-storage-formatter/formatToStore")),h=S(require("./asyncStorageWrapper.js")),p=-1,u=class r extends v.default{constructor(t,e={metadata:{}}){var n,s,i;super(t,e);this.asyncStorage=null;this.getMetadata=()=>this.metadata;this.isPersistStorageAvailable=()=>{var t;return!!((t=this.asyncStorage)!=null&&t.key)};this.trySetStorageItem=async t=>{let e=this.asyncStorage;if(!e)return;let{error:a}=await(0,c.default)(()=>{var s;let n=(s=e.adapter)==null?void 0:s.setItem;return n?n(e.key,t):this.setStorageItem(t)});a&&this.handleStorageError(a)};this.updateStateWithValidation=async t=>{let e=this.asyncStorage;if(!e)return;let{result:a,error:n}=(0,c.default)(()=>{var s;return(s=e.validator)==null?void 0:s.call(e,{restored:t,initial:this.getState()})});if(n){this.handleStorageError(n),await this.trySetStorageItem(this.getState());return}if(a===void 0){if(t===void 0){await this.trySetStorageItem(this.getState());return}this.setState(t,{forceUpdate:!0}),await this.trySetStorageItem(t);return}this.setState(a,{forceUpdate:!0}),await this.trySetStorageItem(a)};this.onInit=async()=>{let t=this.asyncStorage;if(!t||!this.isPersistStorageAvailable())return;this.metadata.isAsyncStorageReady=!1;let e=t.versioning,{result:a,error:n}=await(0,c.default)(async()=>{var l,g;let i=(l=t.adapter)==null?void 0:l.getItem;return i?{s:await i(t.key),v:(g=e==null?void 0:e.version)!=null?g:p}:this.getStorageItem()}),{error:s}=await(0,c.default)(async()=>{if(n){this.handleStorageError(n),await this.updateStateWithValidation(this.getState());return}if(!a){await this.updateStateWithValidation(this.getState());return}let i=a.v===(e==null?void 0:e.version),l=e==null?void 0:e.migrator;if(i||!l||t.adapter){await this.updateStateWithValidation(a.s);return}let{result:g,error:f}=(0,c.default)(()=>l({legacy:a.s,initial:this.getState()}));if(!f){await this.updateStateWithValidation(g);return}this.handleStorageError(f),await this.updateStateWithValidation(this.getState())});this.metadata.isAsyncStorageReady=!0,s&&this.handleStorageError(s)};this.onStateChanged=async t=>{let e=this.asyncStorage;if(!e||!this.isPersistStorageAvailable())return;let{error:a}=await(0,c.default)(()=>{var s;let n=(s=e.adapter)==null?void 0:s.setItem;return n?n(e.key,t.state):this.setStorageItem(t.state)});a&&this.handleStorageError(a)};this.getStorageItem=async()=>{let t=this.asyncStorage.key,e=await h.default.getItem(t),a=(0,y.default)(e)?null:(0,A.default)(e);return x(t,a),a};this.setStorageItem=async t=>{var n,s,i;let e={s:t,v:(i=(s=(n=this.asyncStorage)==null?void 0:n.versioning)==null?void 0:s.version)!=null?i:p},a=(0,C.default)(e);return h.default.setItem(this.asyncStorage.key,a)};this.handleStorageError=t=>{var e,a;if((e=this.asyncStorage)!=null&&e.onError)return this.asyncStorage.onError(t);console.error([`[react-native-global-state-hooks]
|
|
2
|
+
`," asyncStorage sync error:","[hook]:",` ${this._name}`,"[AsyncStorage Key]:",` ${(a=this.asyncStorage)==null?void 0:a.key}`,"[Error]:",` ${t!=null?t:"undefined"}
|
|
3
|
+
|
|
4
|
+
`,"Stacktrace:"].join(`
|
|
5
|
+
`),t.stack)};this.asyncStorage=(n=e.asyncStorage)!=null?n:null,Object.assign(this.getMetadata(),{isAsyncStorageReady:!1,asyncStorageKey:(i=(s=this.asyncStorage)==null?void 0:s.key)!=null?i:null}),this.constructor===r&&this.initialize()}};function x(r,o){if(!(0,y.default)(o)&&!(typeof o=="object"&&"s"in o&&"v"in o))throw new Error(`[react-native-global-state-hooks] The value of the key "${r}" is not a valid storage envelope.`)}var F=u;
|
package/GlobalStore.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import f from"react-hooks-global-states/GlobalStore";import s from"./tryCatch.mjs";import y from"json-storage-formatter/isNil";import m from"json-storage-formatter/formatFromStore";import p from"json-storage-formatter/formatToStore";import u from"./asyncStorageWrapper.mjs";var h=-1,g=class l extends f{constructor(e,t={metadata:{}}){var r,o,n;super(e,t);this.asyncStorage=null;this.getMetadata=()=>this.metadata;this.isPersistStorageAvailable=()=>{var e;return!!((e=this.asyncStorage)!=null&&e.key)};this.trySetStorageItem=async e=>{let t=this.asyncStorage;if(!t)return;let{error:a}=await s(()=>{var o;let r=(o=t.adapter)==null?void 0:o.setItem;return r?r(t.key,e):this.setStorageItem(e)});a&&this.handleStorageError(a)};this.updateStateWithValidation=async e=>{let t=this.asyncStorage;if(!t)return;let{result:a,error:r}=s(()=>{var o;return(o=t.validator)==null?void 0:o.call(t,{restored:e,initial:this.getState()})});if(r){this.handleStorageError(r),await this.trySetStorageItem(this.getState());return}if(a===void 0){if(e===void 0){await this.trySetStorageItem(this.getState());return}this.setState(e,{forceUpdate:!0}),await this.trySetStorageItem(e);return}this.setState(a,{forceUpdate:!0}),await this.trySetStorageItem(a)};this.onInit=async()=>{let e=this.asyncStorage;if(!e||!this.isPersistStorageAvailable())return;this.metadata.isAsyncStorageReady=!1;let t=e.versioning,{result:a,error:r}=await s(async()=>{var c,S;let n=(c=e.adapter)==null?void 0:c.getItem;return n?{s:await n(e.key),v:(S=t==null?void 0:t.version)!=null?S:h}:this.getStorageItem()}),{error:o}=await s(async()=>{if(r){this.handleStorageError(r),await this.updateStateWithValidation(this.getState());return}if(!a){await this.updateStateWithValidation(this.getState());return}let n=a.v===(t==null?void 0:t.version),c=t==null?void 0:t.migrator;if(n||!c||e.adapter){await this.updateStateWithValidation(a.s);return}let{result:S,error:d}=s(()=>c({legacy:a.s,initial:this.getState()}));if(!d){await this.updateStateWithValidation(S);return}this.handleStorageError(d),await this.updateStateWithValidation(this.getState())});this.metadata.isAsyncStorageReady=!0,o&&this.handleStorageError(o)};this.onStateChanged=async e=>{let t=this.asyncStorage;if(!t||!this.isPersistStorageAvailable())return;let{error:a}=await s(()=>{var o;let r=(o=t.adapter)==null?void 0:o.setItem;return r?r(t.key,e.state):this.setStorageItem(e.state)});a&&this.handleStorageError(a)};this.getStorageItem=async()=>{let e=this.asyncStorage.key,t=await u.getItem(e),a=y(t)?null:m(t);return v(e,a),a};this.setStorageItem=async e=>{var r,o,n;let t={s:e,v:(n=(o=(r=this.asyncStorage)==null?void 0:r.versioning)==null?void 0:o.version)!=null?n:h},a=p(t);return u.setItem(this.asyncStorage.key,a)};this.handleStorageError=e=>{var t,a;if((t=this.asyncStorage)!=null&&t.onError)return this.asyncStorage.onError(e);console.error([`[react-native-global-state-hooks]
|
|
2
|
+
`," asyncStorage sync error:","[hook]:",` ${this._name}`,"[AsyncStorage Key]:",` ${(a=this.asyncStorage)==null?void 0:a.key}`,"[Error]:",` ${e!=null?e:"undefined"}
|
|
3
|
+
|
|
4
|
+
`,"Stacktrace:"].join(`
|
|
5
|
+
`),e.stack)};this.asyncStorage=(r=t.asyncStorage)!=null?r:null,Object.assign(this.getMetadata(),{isAsyncStorageReady:!1,asyncStorageKey:(n=(o=this.asyncStorage)==null?void 0:o.key)!=null?n:null}),this.constructor===l&&this.initialize()}};function v(l,i){if(!y(i)&&!(typeof i=="object"&&"s"in i&&"v"in i))throw new Error(`[react-native-global-state-hooks] The value of the key "${l}" is not a valid storage envelope.`)}var w=g;export{g as GlobalStore,w as default};
|