react-global-state-hooks 1.0.14 → 1.0.16

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.
@@ -1,53 +1,50 @@
1
- import * as IGlobalStore from './GlobalStoreTypes';
2
- export type IValueWithMedaData = {
3
- _type_?: 'map' | 'set' | 'date';
4
- value?: unknown;
5
- };
6
- export declare const debounce: <T extends Function>(callback: T, wait?: number) => T;
7
- /**
8
- * This is a class to create global-store objects
9
- * @template IState
10
- * @param {IState} state - Initial state,
11
- * @template IActions
12
- * @param {IActions} actions - An specific api to restrict the use of the state,
13
- * this will disable the default return of the state-setter of the hook, and instead will return the API
14
- * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
15
- * */
16
- export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCollectionConfig<IState> | null = null> implements IGlobalStore.IGlobalState<IState, IActions> {
17
- protected state: IState;
18
- protected actions: IActions;
19
- protected persistStoreAs: string | null;
20
- protected subscribers: IGlobalStore.StateSetter<IState>[];
21
- get isPersistStore(): boolean;
22
- constructor(state: IState, actions?: IActions, persistStoreAs?: string | null);
23
- private storedStateItem;
24
- protected localStorageGetItem(): string | null;
25
- protected getStoreItem(): IState;
26
- protected localStorageSetItem(valueToStore: string): void;
27
- protected setStoreItem(): void;
28
- protected getPersistStoreValue: () => IState;
29
- protected getStateCopy: () => IState;
1
+ export * from 'react-native-global-state-hooks';
2
+ import { GlobalStore as GlobalStoreBase } from 'react-native-global-state-hooks';
3
+ import { ActionCollectionConfig, GlobalStoreConfig, StateConfigCallbackParam, StateSetter } from './GlobalStore.types';
4
+ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> | null = StateSetter<TState>> extends GlobalStoreBase<TState, TMetadata, TStateSetter> {
30
5
  /**
31
- * Returns a global hook that will share information across components by subscribing them to a specific store.
32
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
6
+ * additional configuration for the store
7
+ * @template {TState} TState - The type of the state object
8
+ * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
9
+ * @template {TStateSetter} TStateSetter - The type of the setterConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
10
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
11
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
12
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
13
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onSubscribed - The callback to execute when a component is subscribed to the store (optional) (default: null)
14
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
15
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.localStorageKey - The key to use to store the state in the localStorage (optional) (default: null) if not null the state will be stored in the localStorage
33
16
  */
34
- getHook: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => () => [IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
17
+ protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
35
18
  /**
36
- * This is an access to the subscribers queue and to the current state of a specific store...
37
- * THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
38
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
39
- */
40
- getHookDecoupled: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => [() => IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
41
- private _stateOrchestrator;
42
- protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.IActionCollectionResult<IState, IActions>;
19
+ * Create a new instance of the GlobalStore
20
+ * @param {TState} state - The initial state
21
+ * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
22
+ * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
23
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
24
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
25
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
26
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
27
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
28
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.localStorageKey - The key to use to store the state in the localStorage (optional) (default: null) if not null the state will be stored in the localStorage
29
+ * */
30
+ constructor(state: TState, { onInit: onInitConfig, ...config }?: GlobalStoreConfig<TState, TMetadata, TStateSetter>, setterConfig?: TStateSetter | null);
31
+ protected setLocalStorageValue: () => void;
32
+ protected getLocalStorageValue: () => string;
43
33
  /**
44
- ** [subscriber-update-callback, hook, newState]
34
+ * This method will be called once the store is created after the constructor,
35
+ * this method is different from the onInit of the confg property and it won't be overriden
45
36
  */
46
- protected static batchedUpdates: [() => void, object, object][];
47
- protected globalSetter: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => void;
48
- protected globalSetterToPersistStoreAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
49
- static ExecutePendingBatches: () => void;
50
- protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
37
+ protected onInit: ({ setState, }: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => Promise<void>;
38
+ protected onStateChanged: () => void;
39
+ /**
40
+ * set the state and update all the subscribers,
41
+ * In react web ReacDom allows to batch the state updates, this method will use the unstable_batchedUpdates method if it exists
42
+ * @param {StateSetter<TState>} setter - The setter function or the value to set
43
+ * @param {React.Dispatch<React.SetStateAction<TState>>} invokerSetState - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
44
+ * */
45
+ protected setState: ({ invokerSetState, state, }: {
46
+ state: TState;
47
+ invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
48
+ }) => void;
51
49
  }
52
- export default GlobalStore;
53
50
  //# sourceMappingURL=GlobalStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAWnD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,QAAQ,uDAUpB,CAAC;AAEF;;;;;;;;IAQI;AACJ,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAAS,YAAY,CAAC,uBAAuB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI,CAC3E,YAAW,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC;IAQ1C,SAAS,CAAC,KAAK,EAAE,MAAM;IAAE,SAAS,CAAC,OAAO,EAAE,QAAQ;IAAqB,SAAS,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI;IAN5H,SAAS,CAAC,WAAW,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAM;IAE/D,IAAW,cAAc,YAExB;gBAEqB,KAAK,EAAE,MAAM,EAAY,OAAO,GAAE,QAA2B,EAA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B,CAAC;AAE7B,qBAAa,WAAW,CACtB,MAAM,EACN,SAAS,GAAG,IAAI,EAChB,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GACnB,IAAI,GAAG,WAAW,CAAC,MAAM,CAAC,CAC9B,SAAQ,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC;IACxD;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IAErE;;;;;;;;;;;SAWK;gBAEH,KAAK,EAAE,MAAM,EACb,EACE,MAAM,EAAE,YAAY,EACpB,GAAG,MAAM,EACV,GAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAM,EAC1D,YAAY,GAAE,YAAY,GAAG,IAAW;IAwB1C,SAAS,CAAC,oBAAoB,aAqB5B;IAEF,SAAS,CAAC,oBAAoB,eAmB5B;IAEF;;;OAGG;IACH,SAAS,CAAC,MAAM,kBAEb,yBAAyB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,mBAgB1D;IAEF,SAAS,CAAC,cAAc,aAEtB;IAEF;;;;;SAKK;IACL,SAAS,CAAC,QAAQ;eAIT,MAAM;0BACK,MAAM,QAAQ,CAAC,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;eAoB9D;CACH"}
@@ -1,165 +1,101 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalStore = exports.debounce = void 0;
3
+ exports.GlobalStore = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const react_dom_1 = tslib_1.__importDefault(require("react-dom"));
6
- const json_storage_formatter_1 = require("json-storage-formatter");
7
- const react_1 = require("react");
8
- const debounce = (callback, wait = 300) => {
9
- let timer;
10
- return ((...args) => {
11
- clearTimeout(timer);
12
- timer = setTimeout(() => {
13
- callback(...args);
14
- }, wait);
15
- });
16
- };
17
- exports.debounce = debounce;
18
- /**
19
- * This is a class to create global-store objects
20
- * @template IState
21
- * @param {IState} state - Initial state,
22
- * @template IActions
23
- * @param {IActions} actions - An specific api to restrict the use of the state,
24
- * this will disable the default return of the state-setter of the hook, and instead will return the API
25
- * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
26
- * */
27
- class GlobalStore {
28
- get isPersistStore() {
29
- return !!this.persistStoreAs;
30
- }
31
- constructor(state, actions = null, persistStoreAs = null) {
32
- this.state = state;
33
- this.actions = actions;
34
- this.persistStoreAs = persistStoreAs;
35
- this.subscribers = [];
36
- this.storedStateItem = undefined;
37
- this.getPersistStoreValue = () => this.getStoreItem();
38
- this.getStateCopy = () => Object.freeze((0, json_storage_formatter_1.clone)(this.state));
39
- /**
40
- * Returns a global hook that will share information across components by subscribing them to a specific store.
41
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
42
- */
43
- this.getHook = () => () => {
44
- const [value, setter] = (0, react_1.useState)(this.state);
45
- const valueWrapper = this.isPersistStore ? this.getPersistStoreValue() : value;
46
- (0, react_1.useEffect)(() => {
47
- this.subscribers.push(setter);
48
- return () => {
49
- this.subscribers = this.subscribers.filter((hook) => setter !== hook);
50
- };
51
- }, []);
52
- return [
53
- valueWrapper,
54
- this.stateOrchestrator,
55
- ];
6
+ tslib_1.__exportStar(require("react-native-global-state-hooks"), exports);
7
+ const react_native_global_state_hooks_1 = require("react-native-global-state-hooks");
8
+ class GlobalStore extends react_native_global_state_hooks_1.GlobalStore {
9
+ /**
10
+ * Create a new instance of the GlobalStore
11
+ * @param {TState} state - The initial state
12
+ * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
13
+ * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
14
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
15
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
16
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
17
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
18
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
19
+ * @param {StateConfigCallbackParam<TState, TMetadata>} config.localStorageKey - The key to use to store the state in the localStorage (optional) (default: null) if not null the state will be stored in the localStorage
20
+ * */
21
+ constructor(state, _a = {}, setterConfig) {
22
+ var _b;
23
+ var { onInit: onInitConfig } = _a, config = tslib_1.__rest(_a, ["onInit"]);
24
+ if (setterConfig === void 0) { setterConfig = null; }
25
+ debugger;
26
+ const decrypt = (config === null || config === void 0 ? void 0 : config.decrypt) === undefined ? (_b = config === null || config === void 0 ? void 0 : config.encrypt) !== null && _b !== void 0 ? _b : true : config === null || config === void 0 ? void 0 : config.decrypt;
27
+ super(state, Object.assign({ metadata: null, encrypt: true, decrypt }, (config !== null && config !== void 0 ? config : {})), setterConfig);
28
+ this.setLocalStorageValue = () => {
29
+ const { localStorageKey } = this.config;
30
+ debugger;
31
+ let stateToStore = (0, react_native_global_state_hooks_1.formatToStore)(this.getStateClone(), {
32
+ stringify: true,
33
+ });
34
+ const { encrypt } = this.config;
35
+ if (encrypt) {
36
+ const isEncryptCallback = typeof encrypt === 'function';
37
+ const encryptCallback = (isEncryptCallback ? encrypt : (value) => btoa(value));
38
+ stateToStore = encryptCallback(stateToStore);
39
+ }
40
+ localStorage.setItem(localStorageKey, stateToStore);
41
+ };
42
+ this.getLocalStorageValue = () => {
43
+ const { localStorageKey } = this.config;
44
+ debugger;
45
+ let storedState = localStorage.getItem(localStorageKey);
46
+ const { decrypt } = this.config;
47
+ if (decrypt) {
48
+ const isDecryptCallback = typeof decrypt === 'function';
49
+ const decryptCallback = (isDecryptCallback ? decrypt : (value) => atob(value));
50
+ storedState = decryptCallback(storedState);
51
+ }
52
+ return storedState;
56
53
  };
57
54
  /**
58
- * This is an access to the subscribers queue and to the current state of a specific store...
59
- * THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
60
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
55
+ * This method will be called once the store is created after the constructor,
56
+ * this method is different from the onInit of the confg property and it won't be overriden
61
57
  */
62
- this.getHookDecoupled = () => {
63
- const valueWrapper = this.isPersistStore ? () => this.getPersistStoreValue() : () => this.state;
64
- return [
65
- valueWrapper,
66
- this.stateOrchestrator,
67
- ];
58
+ this.onInit = ({ setState, }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
59
+ const { localStorageKey } = this.config;
60
+ if (!localStorageKey)
61
+ return;
62
+ const storedState = this.getLocalStorageValue();
63
+ if (storedState === null) {
64
+ this.setLocalStorageValue();
65
+ return;
66
+ }
67
+ const jsonParsed = JSON.parse(storedState);
68
+ const state = (0, react_native_global_state_hooks_1.formatFromStore)(jsonParsed);
69
+ setState(state);
70
+ });
71
+ this.onStateChanged = () => {
72
+ this.setLocalStorageValue();
68
73
  };
69
- this._stateOrchestrator = null;
70
- this.globalSetter = (setter) => {
71
- const partialState = typeof setter === 'function' ? setter(this.getStateCopy()) : setter;
72
- let newState = (0, json_storage_formatter_1.isPrimitive)(partialState) || Array.isArray(partialState)
73
- ? partialState : Object.assign(Object.assign({}, this.state), partialState);
74
- // avoid perform multiple update batches by accumulating state changes of the same hook
75
- GlobalStore.batchedUpdates = GlobalStore.batchedUpdates.filter(([, hook, previousState]) => {
76
- const isSameHook = hook === this;
77
- if (isSameHook) {
78
- // eslint-disable-next-line no-console
79
- console.warn('You should try avoid call the same state-setter multiple times at one execution line');
80
- newState = (0, json_storage_formatter_1.isPrimitive)(newState) || Array.isArray(partialState)
81
- ? newState : Object.assign(Object.assign({}, previousState), newState);
82
- }
83
- return !isSameHook;
74
+ /**
75
+ * set the state and update all the subscribers,
76
+ * In react web ReacDom allows to batch the state updates, this method will use the unstable_batchedUpdates method if it exists
77
+ * @param {StateSetter<TState>} setter - The setter function or the value to set
78
+ * @param {React.Dispatch<React.SetStateAction<TState>>} invokerSetState - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
79
+ * */
80
+ this.setState = ({ invokerSetState, state, }) => {
81
+ // update the state
82
+ this.state = state;
83
+ const unstable_batchedUpdates = react_dom_1.default.unstable_batchedUpdates ||
84
+ ((callback) => callback());
85
+ unstable_batchedUpdates(() => {
86
+ // execute first the callback of the component that invoked the state change
87
+ invokerSetState === null || invokerSetState === void 0 ? void 0 : invokerSetState(state);
88
+ // update all the subscribers
89
+ this.subscribers.forEach((setState) => {
90
+ if (setState === invokerSetState)
91
+ return;
92
+ setState(state);
93
+ });
84
94
  });
85
- this.state = newState;
86
- // batch store updates
87
- GlobalStore.batchedUpdates.push([() => this.subscribers.forEach((updateChild) => updateChild(newState)), this, newState]);
88
- GlobalStore.ExecutePendingBatches();
89
95
  };
90
- this.globalSetterToPersistStoreAsync = (setter) => tslib_1.__awaiter(this, void 0, void 0, function* () {
91
- this.globalSetter(setter);
92
- this.setStoreItem();
93
- });
94
- this.getActions = () => {
95
- const actions = this.actions;
96
- // Setter is allways async because of the render batch
97
- const setter = this.isPersistStore ? this.globalSetterToPersistStoreAsync : this.globalSetter;
98
- return Object.keys(actions).reduce((accumulator, key) => (Object.assign(Object.assign({}, accumulator), { [key]: (...parameres) => tslib_1.__awaiter(this, void 0, void 0, function* () {
99
- let promise;
100
- const setterWrapper = (value) => {
101
- promise = setter(value);
102
- return promise;
103
- };
104
- const result = actions[key](...parameres)(setterWrapper, this.getStateCopy());
105
- const resultPromise = Promise.resolve(result) === result ? result : Promise.resolve();
106
- yield Promise.all([promise, resultPromise]);
107
- return result;
108
- }) })), {});
109
- };
110
- }
111
- localStorageGetItem() {
112
- return localStorage.getItem(this.persistStoreAs);
113
- }
114
- getStoreItem() {
115
- if (this.storedStateItem !== undefined)
116
- return this.storedStateItem;
117
- const item = this.localStorageGetItem();
118
- if (item) {
119
- const value = JSON.parse(item);
120
- const newState = (0, json_storage_formatter_1.formatFromStore)(value);
121
- this.state = newState;
122
- }
123
- return this.state;
124
- }
125
- localStorageSetItem(valueToStore) {
126
- localStorage.setItem(this.persistStoreAs, valueToStore);
127
- }
128
- setStoreItem() {
129
- if (this.storedStateItem === this.state)
130
- return;
131
- this.storedStateItem = this.state;
132
- const valueToStore = (0, json_storage_formatter_1.formatToStore)(this.state);
133
- this.localStorageSetItem(JSON.stringify(valueToStore));
134
- }
135
- get stateOrchestrator() {
136
- if (this._stateOrchestrator)
137
- return this._stateOrchestrator;
138
- if (this.actions) {
139
- this._stateOrchestrator = this.getActions();
140
- }
141
- else if (this.persistStoreAs) {
142
- this._stateOrchestrator = this.globalSetterToPersistStoreAsync;
143
- }
144
- else {
145
- this._stateOrchestrator = this.globalSetter;
146
- }
147
- return this._stateOrchestrator;
96
+ const parameters = this.getConfigCallbackParam({});
97
+ this.onInit(parameters);
98
+ onInitConfig === null || onInitConfig === void 0 ? void 0 : onInitConfig(parameters);
148
99
  }
149
100
  }
150
101
  exports.GlobalStore = GlobalStore;
151
- /**
152
- ** [subscriber-update-callback, hook, newState]
153
- */
154
- GlobalStore.batchedUpdates = [];
155
- // avoid multiples calls to batchedUpdates
156
- GlobalStore.ExecutePendingBatches = () => {
157
- const reactBatchedUpdates = react_dom_1.default.unstable_batchedUpdates || ((mock) => mock());
158
- reactBatchedUpdates(() => {
159
- GlobalStore.batchedUpdates.forEach(([execute]) => {
160
- execute();
161
- });
162
- GlobalStore.batchedUpdates = [];
163
- });
164
- };
165
- exports.default = GlobalStore;
@@ -0,0 +1,25 @@
1
+ import * as TGlobalStoreBase from 'react-native-global-state-hooks/lib/GlobalStore.types';
2
+ export type StateSetter<TState> = TGlobalStoreBase.StateSetter<TState>;
3
+ export type StateChanges<TState> = TGlobalStoreBase.StateChanges<TState>;
4
+ export type StoreTools<TState, TMetadata = null> = TGlobalStoreBase.StoreTools<TState, TMetadata>;
5
+ export interface ActionCollectionConfig<TState, TMetadata> extends TGlobalStoreBase.ActionCollectionConfig<TState, TMetadata> {
6
+ }
7
+ export type ActionCollectionResult<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = TGlobalStoreBase.ActionCollectionResult<TState, TMetadata, TStateSetter>;
8
+ export type StateConfigCallbackParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = TGlobalStoreBase.StateConfigCallbackParam<TState, TMetadata, TStateSetter>;
9
+ export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = TGlobalStoreBase.StateChangesParam<TState, TMetadata, TStateSetter>;
10
+ /**
11
+ * Configuration of the store (optional) - if you don't need to use the store configuration you don't need to pass this parameter
12
+ * @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
13
+ * @param {StateConfigCallbackParam<TState, TMetadata> => void} onSubscribed - callback function called every time a component is subscribed to the store
14
+ * @param {StateChangesParam<TState, TMetadata> => boolean} computePreventStateChange - callback function called every time the state is changed and it allows you to prevent the state change
15
+ * @param {StateChangesParam<TState, TMetadata> => void} onStateChanged - callback function called every time the state is changed
16
+ * @template TState - the type of the state
17
+ * @template TMetadata - the type of the metadata (optional) - if you don't pass an metadata as a parameter, you can pass null
18
+ * @template {ActionCollectionConfig<TState,TMetadata> | null} TStateSetter - the configuration of the API (optional) - if you don't pass an API as a parameter, you can pass null
19
+ * */
20
+ export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends TGlobalStoreBase.ActionCollectionConfig<TState, TMetadata> | TGlobalStoreBase.StateSetter<TState> = TGlobalStoreBase.StateSetter<TState>> = TGlobalStoreBase.GlobalStoreConfig<TState, TMetadata, TStateSetter> & {
21
+ localStorageKey?: string;
22
+ encrypt?: boolean | ((value: string) => string);
23
+ decrypt?: boolean | ((value: string) => string);
24
+ };
25
+ //# sourceMappingURL=GlobalStore.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GlobalStore.types.d.ts","sourceRoot":"","sources":["../src/GlobalStore.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,gBAAgB,MAAM,uDAAuD,CAAC;AAE1F,MAAM,MAAM,WAAW,CAAC,MAAM,IAAI,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AAEvE,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI,gBAAgB,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;AAEzE,MAAM,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,IAAI,gBAAgB,CAAC,UAAU,CAC5E,MAAM,EACN,SAAS,CACV,CAAC;AAEF,MAAM,WAAW,sBAAsB,CAAC,MAAM,EAAE,SAAS,CACvD,SAAQ,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC;CAAG;AAEvE,MAAM,MAAM,sBAAsB,CAChC,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAE7E,MAAM,MAAM,wBAAwB,CAClC,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAE/E,MAAM,MAAM,iBAAiB,CAC3B,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C,gBAAgB,CAAC,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;AAExE;;;;;;;;;KASK;AACL,MAAM,MAAM,iBAAiB,CAC3B,MAAM,EACN,SAAS,EACT,YAAY,SACR,gBAAgB,CAAC,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GAC1D,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,gBAAgB,CAAC,WAAW,CAAC,MAAM,CAAC,IAC7E,gBAAgB,CAAC,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG;IACxE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;IAChD,OAAO,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;CACjD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-global-state-hooks",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "This is a package to easily handling global-state across your react components No-redux, No-context.",
5
5
  "main": "lib/GlobalStore.js",
6
6
  "files": [
@@ -60,6 +60,6 @@
60
60
  "react-dom": "workspace:*"
61
61
  },
62
62
  "dependencies": {
63
- "json-storage-formatter": "^1.0.2"
63
+ "react-native-global-state-hooks": "^2.1.9"
64
64
  }
65
65
  }
package/README.advance.md DELETED
@@ -1,123 +0,0 @@
1
-
2
- ## Creating hooks with reusable actions
3
-
4
- Let's say you want to have a STATE with a specific set of actions that you could be reused. With this library is pretty easy to accomplish. Let's create **increase** and **decrease** actions to our count-store. **useCountGlobal.ts**:
5
-
6
- ```JSX
7
- import {
8
- IActionCollectionConfig,
9
- IActionCollectionResult,
10
- StateSetter,
11
- } from 'react-global-state-hooks/lib/GlobalStoreTypes';
12
-
13
- /**
14
- * When using a custom api, the getHook and getHookDecoupled will not longer return directly the setter,
15
- * intead they will return and api with the specific actions and mutations defined for the store
16
- * Creating a configuration object for our api
17
- */
18
- const countActionsApi: IActionCollectionConfig<number> = {
19
- /* Decrease the value of the count */
20
- decrease(decrease: number) {
21
- /**
22
- * We need to return the async function that is gonna take care of the state mutation or actions
23
- */
24
- return async (setter: StateSetter<number>, state: number) => {
25
- /**
26
- * Next, we perfom whatever modification we want on top of the store
27
- */
28
- return setter(state - decrease);
29
- };
30
- },
31
-
32
- //* Lets add a new action to increase the value of the count */
33
- increase(increase: number) {
34
-
35
- return async (setter: StateSetter<number>, state: number) => {
36
- return setter(state + increase);
37
- };
38
- },
39
- };
40
-
41
- /**
42
- * Now our getHook and getHookDecoupled are gonna return our custom api instead of the StateSetter,
43
- * This will allow us to have more control over our store since the mutations of the same are gonna be limitated
44
- */
45
- const countStore = new GlobalStore(0, countActionsApi);
46
- ```
47
-
48
- If we remove all the explanatory comments the code will look like this:
49
-
50
- ```TS
51
- const countStore = new GlobalStore(0, {
52
- decrease(decrease: number) {
53
- return (setter: StateSetter<number>, state: number) =>
54
- setter(state - decrease);
55
- },
56
-
57
- increase(increase: number) {
58
- return (setter: StateSetter<number>, state: number) =>
59
- setter(state + increase);
60
- },
61
- } as IActionCollectionConfig<number>);
62
- ```
63
-
64
- Now lets get our new global hook with specific API
65
-
66
- ```TS
67
- export interface ICountActions
68
- extends IActionCollectionResult<number, IActionCollectionConfig<number>> {
69
- decrease: (decrease: number) => Promise<number>;
70
- increase: (increase: number) => Promise<number>;
71
- }
72
-
73
- /**
74
- * The ICountActions interface is optional but it allow you yo get more accurate results for the typescript autocompletes and validations, ignore this if you are not using TS
75
- */
76
- export const useCountGlobal = countStore.getHook<ICountActions>();
77
- ```
78
-
79
- And that's it! the result of our useCountGlobal will return our actions instead of a simple setter... Let's see how that will look:
80
-
81
- ```JSX
82
- import { useCountGlobal } from './useCountGlobal'
83
-
84
- const MyComponent: Reac.FC = () => {
85
- const [count, countActions] = useCountGlobal();
86
-
87
- // this functions are strongly typed
88
- const increaseClick = () => countActions.increase(1);
89
- const decreaseClick = () => countActions.decrease(1);
90
-
91
- return (<>
92
- <label>{count}<label/><br/>
93
- <button onPress={increaseClick}>increase<button/>
94
- <button onPress={decreaseClick}>decrease<button/>
95
- </>);
96
- }
97
-
98
- ```
99
-
100
- ## Customize persist storage
101
-
102
- Let suppose you don't like **async-storage** or you also want to implement some kind of encrypt-process. You could easily extend the **GlobalStore** Class, and customize your persist store implementation.
103
-
104
- ```JSX
105
- import GlobalState from 'react-global-state-hooks';
106
- import { IActionCollection } from 'react-global-state-hooks/lib/GlobalStoreTypes';
107
-
108
- export class SecureGlobalState<
109
- IState,
110
- IPersist extends string | null = null,
111
- IsPersist extends boolean = IPersist extends null ? false : true,
112
- IActions extends IActionCollection<IState> | null = null
113
- > extends GlobalState<IState, IPersist, IsPersist, IActions> {
114
-
115
- protected getStoreItem = () => secureStorage.getItem(this.persistStoreAs as string);
116
-
117
- /** value is a json string*/
118
- protected setStoreItem = (value: string) => secureStorage.setItem(this.persistStoreAs as string, value);
119
-
120
- }
121
-
122
- export default SecureGlobalState;
123
- ```
@@ -1,54 +0,0 @@
1
- /**
2
- * @param {StateSetter<IState>} setter - add a new value to the state
3
- * @returns {Promise<void>} result - resolves when update_batches finished
4
- */
5
- export type StateSetter<IState> = (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => void;
6
- /**
7
- * This is the structure required by the API actions in order to be able to capture action parameters and inject state setter into actions.
8
- */
9
- export type IAction<IState> = <IResult>(...params: any[]) => (setter: StateSetter<IState>, currentState: IState) => Promise<unknown> | IResult;
10
- /**
11
- * Configuration of you API
12
- */
13
- export interface IActionCollectionConfig<IState> {
14
- [key: string]: IAction<IState>;
15
- }
16
- /**
17
- * This is the API result of the hook (if you passed an API as a parameter)
18
- */
19
- export type IActionCollectionResult<IState, IActions extends IActionCollectionConfig<IState> | null> = {
20
- [key in keyof IActions]: (...params: any[]) => unknown;
21
- };
22
- /**
23
- * Hook result, if you passed an API as a parameter it will be returned in the second position of the hook invoke.
24
- */
25
- export type IHookResult<IState, IActions extends IActionCollectionConfig<IState> | null = null, IApi extends IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IActionCollectionResult<IState, IActions>> = IApi extends null ? StateSetter<IState> : IActions extends IActionCollectionConfig<IState> ? IApi extends IActionCollectionResult<IState, IActions> ? IApi : StateSetter<IState> : StateSetter<IState>;
26
- /**
27
- * This is a class to create global-store objects
28
- * @template IState
29
- * @param {IState} state - Initial state,
30
- * @template IActions
31
- * @param {IActions} actions - An specific api to restrict the use of the state,
32
- * this will disable the default return of the state-setter of the hook, and instead will return the API
33
- * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
34
- * */
35
- export interface IGlobalState<IState, IActions extends IActionCollectionConfig<IState> | null = null> {
36
- /**
37
- * Returns a global hook that will share information across components by subscribing them to a specific store.
38
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
39
- */
40
- getHook: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => () => [
41
- IState,
42
- IHookResult<IState, IActions, IApi>
43
- ];
44
- /**
45
- * This is an access to the subscribers queue and to the current state of a specific store...
46
- * THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
47
- * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
48
- */
49
- getHookDecoupled: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => [
50
- () => IState,
51
- IHookResult<IState, IActions, IApi>
52
- ];
53
- }
54
- //# sourceMappingURL=GlobalStoreTypes.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GlobalStoreTypes.d.ts","sourceRoot":"","sources":["../src/GlobalStoreTypes.ts"],"names":[],"mappings":"AAAA;;;EAGE;A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