react-global-state-hooks 15.0.12 → 15.0.14-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.d.ts +14 -12
- package/GlobalStore.js +1 -1
- package/README.md +1604 -168
- package/actions.d.ts +1 -0
- package/bundle.js +1 -1
- package/createGlobalState.d.ts +1 -139
- package/index.d.ts +6 -7
- package/package.json +7 -2
- package/tryCatch.d.ts +2 -5
- package/types.d.ts +143 -4
package/GlobalStore.d.ts
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, ActionCollectionResult, AnyFunction, BaseMetadata, GlobalStoreCallbacks, StateChanges } from 'react-hooks-global-states/types';
|
|
1
|
+
import type { ActionCollectionConfig, ActionCollectionResult, Any, AnyFunction, BaseMetadata, GlobalStoreCallbacks, StateChanges } from 'react-hooks-global-states/types';
|
|
2
2
|
import GlobalStoreBase from 'react-hooks-global-states/GlobalStore';
|
|
3
3
|
import type { LocalStorageConfig, ItemEnvelope } from './types';
|
|
4
4
|
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, Metadata, ActionsConfig> {
|
|
5
5
|
protected localStorage: LocalStorageConfig<State, Metadata> | null;
|
|
6
|
-
constructor(state: State);
|
|
7
|
-
constructor(state: State, args: {
|
|
8
|
-
metadata?: Metadata;
|
|
9
|
-
callbacks?: GlobalStoreCallbacks<
|
|
6
|
+
constructor(state: State | (() => State));
|
|
7
|
+
constructor(state: State | (() => State), args: {
|
|
8
|
+
metadata?: Metadata | (() => Metadata);
|
|
9
|
+
callbacks?: GlobalStoreCallbacks<Any, PublicStateMutator extends AnyFunction ? null : PublicStateMutator, Any>;
|
|
10
10
|
actions?: ActionsConfig;
|
|
11
11
|
name?: string;
|
|
12
|
-
localStorage?: LocalStorageConfig<
|
|
12
|
+
localStorage?: LocalStorageConfig<Any, Any>;
|
|
13
13
|
});
|
|
14
|
-
|
|
14
|
+
isPersistStorageAvailable(): boolean;
|
|
15
15
|
protected onInit: () => void;
|
|
16
|
-
trySetStorageItem
|
|
17
|
-
updateStateWithValidation
|
|
16
|
+
trySetStorageItem(state: State): void;
|
|
17
|
+
updateStateWithValidation(state: State): void;
|
|
18
18
|
protected onStateChanged: ({ state }: StateChanges<State>) => void;
|
|
19
|
-
getStorageItem
|
|
20
|
-
setStorageItem
|
|
21
|
-
|
|
19
|
+
getStorageItem(): ItemEnvelope<State> | ItemEnvelope<Partial<State>> | null;
|
|
20
|
+
setStorageItem(state: State): void;
|
|
21
|
+
reset(): void;
|
|
22
|
+
reset(state: State, metadata: Metadata): void;
|
|
23
|
+
handleStorageError(error: unknown): void;
|
|
22
24
|
}
|
|
23
25
|
export default GlobalStore;
|
package/GlobalStore.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e;t=this,e=(t,e,o,r,a)=>(()=>{"use strict";var s={132(t,e,o){var r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0;const a=r(o(536)),s=r(o(862)),i=r(o(330)),l=r(o(413)),n=r(o(773)),u=r(o(955));class
|
|
1
|
+
var t,e;t=this,e=(t,e,o,r,a)=>(()=>{"use strict";var s={132(t,e,o){var r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0;const a=r(o(536)),s=r(o(862)),i=r(o(330)),l=r(o(413)),n=r(o(773)),u=r(o(955));class g extends a.default{constructor(t,e={metadata:{}}){var o;super(t,e),this.localStorage=null,this.onInit=()=>{const t=this.localStorage;if(!t||!this.isPersistStorageAvailable())return;const e=t.versioning,{result:o,error:r}=(0,s.default)(()=>{var o,r,a;if(null===(o=t.adapter)||void 0===o?void 0:o.getItem)return{s:null===(r=t.adapter)||void 0===r?void 0:r.getItem(t.key),v:null!==(a=null==e?void 0:e.version)&&void 0!==a?a:-1};const s=this.getStorageItem();return s&&t.selector&&!(0,u.default)(null==s?void 0:s.s)?Object.assign(Object.assign({},s),{s:Object.assign(Object.assign({},this.getState()),s.s)}):s});if(r)return this.handleStorageError(r),void this.updateStateWithValidation(this.getState());if(!o)return void this.updateStateWithValidation(this.getState());const a=o.v===(null==e?void 0:e.version),i=null==e?void 0:e.migrator;if(a||!i||t.adapter)return void this.updateStateWithValidation(o.s);const{result:l,error:n}=(0,s.default)(()=>i({legacy:o.s,initial:this.getState()},this.storeTools));n?(this.handleStorageError(n),this.updateStateWithValidation(this.getState())):this.updateStateWithValidation(l)},this.onStateChanged=({state:t})=>{const e=this.localStorage;if(!e||!this.isPersistStorageAvailable())return;const{error:o}=(0,s.default)(()=>{var o;const r=null===(o=e.adapter)||void 0===o?void 0:o.setItem;if(r)return r(e.key,t);this.setStorageItem(t)});o&&this.handleStorageError(o)},this.localStorage=null!==(o=e.localStorage)&&void 0!==o?o:null,this.constructor!==g||this.initialize()}isPersistStorageAvailable(){var t;return Boolean((null===(t=this.localStorage)||void 0===t?void 0:t.key)&&(null===globalThis||void 0===globalThis?void 0:globalThis.localStorage))}trySetStorageItem(t){const e=this.localStorage;if(!e)return;const{error:o}=(0,s.default)(()=>{var o;const r=null===(o=e.adapter)||void 0===o?void 0:o.setItem;if(!r)return this.setStorageItem(t);r(e.key,t)});o&&this.handleStorageError(o)}updateStateWithValidation(t){const e=this.localStorage;if(!e)return;const{result:o,error:r}=(0,s.default)(()=>{var o;return null===(o=e.validator)||void 0===o?void 0:o.call(e,{restored:t,initial:this.getState()},this.storeTools)});return r?(this.handleStorageError(r),void this.trySetStorageItem(this.getState())):void 0===o?void 0===t?void this.trySetStorageItem(this.getState()):(this.setState(t),void this.trySetStorageItem(t)):(this.setState(o),void this.trySetStorageItem(o))}getStorageItem(){const t=globalThis.localStorage.getItem(this.localStorage.key),e=(0,n.default)(t)?null:(0,i.default)(t);return function(t,e){if(!((0,n.default)(e)||"object"==typeof e&&"s"in e&&"v"in e))throw new Error(`[react-native-global-state-hooks] The value of the key "${t}" is not a valid storage envelope.`)}(this.localStorage.key,e),e}setStorageItem(t){var e,o,r,a;const s={s:(null===(e=this.localStorage)||void 0===e?void 0:e.selector)?this.localStorage.selector(t):t,v:null!==(a=null===(r=null===(o=this.localStorage)||void 0===o?void 0:o.versioning)||void 0===r?void 0:r.version)&&void 0!==a?a:-1},i=(0,l.default)(s);globalThis.localStorage.setItem(this.localStorage.key,i)}reset(...t){this.isPersistStorageAvailable()&&0===t.length&&globalThis.localStorage.removeItem(this.localStorage.key),super.reset(...t)}handleStorageError(t){var e,o;if(null===(e=this.localStorage)||void 0===e?void 0:e.onError)return this.localStorage.onError(t,this.storeTools);globalThis.console.error(["[react-global-state-hooks]\n"," localStorage sync error:","[hook]:",` ${this._name}`,"[Localstorage Key]:",` ${null===(o=this.localStorage)||void 0===o?void 0:o.key}`,"[Error]:",` ${null!=t?t:"undefined"}\n\n`,"Stacktrace:"].join("\n"),t.stack)}}e.GlobalStore=g,e.default=g},330(e){e.exports=t},413(t){t.exports=e},536(t){t.exports=o},773(t){t.exports=r},862(t,e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(t){try{return{result:t(),error:null}}catch(t){return{result:null,error:t}}}},955(t){t.exports=a}},i={};return function t(e){var o=i[e];if(void 0!==o)return o.exports;var r=i[e]={exports:{}};return s[e].call(r.exports,r,r.exports,t),r.exports}(132)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("react-hooks-global-states/GlobalStore"),require("json-storage-formatter/isNil"),require("json-storage-formatter/isPrimitive")):"function"==typeof define&&define.amd?define(["json-storage-formatter/formatFromStore","json-storage-formatter/formatToStore","react-hooks-global-states/GlobalStore","json-storage-formatter/isNil","json-storage-formatter/isPrimitive"],e):"object"==typeof exports?exports["react-global-state-hooks"]=e(require("json-storage-formatter/formatFromStore"),require("json-storage-formatter/formatToStore"),require("react-hooks-global-states/GlobalStore"),require("json-storage-formatter/isNil"),require("json-storage-formatter/isPrimitive")):t["react-global-state-hooks"]=e(t["json-storage-formatter/formatFromStore"],t["json-storage-formatter/formatToStore"],t["react-hooks-global-states/GlobalStore"],t["json-storage-formatter/isNil"],t["json-storage-formatter/isPrimitive"]);
|