react-hooks-global-states 15.0.11 → 15.0.14
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 +37 -31
- package/GlobalStore.js +1 -1
- package/README.md +1438 -176
- package/actions.d.ts +32 -0
- package/actions.js +1 -0
- package/bundle.js +1 -1
- package/createContext.d.ts +4 -549
- package/createGlobalState.d.ts +2 -191
- package/index.d.ts +5 -4
- package/isRecord.js +1 -1
- package/package.json +9 -3
- package/types.d.ts +866 -7
- package/uniqueId.d.ts +2 -71
- package/uniqueId.js +1 -1
- package/webpack.config.js +1 -0
package/GlobalStore.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, SubscriberParameters, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment, AnyFunction, ReadonlyHook, ReadonlyStateApi,
|
|
1
|
+
import type { ActionCollectionConfig, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, SubscribeCallbackConfig, SubscribeCallback, SelectorCallback, SubscriberParameters, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment, UnsubscribeCallback, AnyFunction, ReadonlyHook, ReadonlyStateApi, CleanupFunction } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* A unique symbol used as a default value placeholder.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT: unique symbol;
|
|
2
6
|
/**
|
|
3
7
|
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
|
|
4
8
|
* */
|
|
@@ -24,9 +28,11 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
24
28
|
*/
|
|
25
29
|
subscribers: Set<SubscriberParameters>;
|
|
26
30
|
state: State;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
protected stateCallback?: () => State;
|
|
32
|
+
protected metadataCallback?: () => Metadata;
|
|
33
|
+
constructor(state: State | (() => State));
|
|
34
|
+
constructor(state: State | (() => State), args: {
|
|
35
|
+
metadata?: Metadata | (() => Metadata);
|
|
30
36
|
callbacks?: GlobalStoreCallbacks<State, PublicStateMutator, Metadata>;
|
|
31
37
|
actions?: ActionsConfig;
|
|
32
38
|
name?: string;
|
|
@@ -43,17 +49,17 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
43
49
|
* @description
|
|
44
50
|
* Initializes the global store, setting up the main hook and actions map if applicable,
|
|
45
51
|
*/
|
|
46
|
-
protected initialize
|
|
52
|
+
protected initialize(): Promise<void>;
|
|
47
53
|
/**
|
|
48
54
|
* set the state for a single subscriber
|
|
49
55
|
* validate if the state should be updated by comparing the previous state and the new state
|
|
50
56
|
*/
|
|
51
|
-
protected executeSetStateForSubscriber
|
|
57
|
+
protected executeSetStateForSubscriber(subscription: SubscriberParameters, args: {
|
|
52
58
|
forceUpdate: boolean | undefined;
|
|
53
59
|
newState: State;
|
|
54
60
|
currentState: State;
|
|
55
61
|
identifier: string | undefined;
|
|
56
|
-
})
|
|
62
|
+
}): {
|
|
57
63
|
didUpdate: boolean;
|
|
58
64
|
};
|
|
59
65
|
/**
|
|
@@ -63,49 +69,53 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
63
69
|
* @param {boolean} [options.forceUpdate] - Whether to force the update even if the state is the same
|
|
64
70
|
* @param {string} [options.identifier] - An optional identifier for the state change
|
|
65
71
|
* */
|
|
66
|
-
setActualStateWithoutValidations
|
|
72
|
+
setActualStateWithoutValidations(newState: State, { forceUpdate, identifier }: {
|
|
67
73
|
forceUpdate?: boolean;
|
|
68
74
|
identifier?: string;
|
|
69
|
-
})
|
|
75
|
+
}): void;
|
|
70
76
|
/**
|
|
71
77
|
* Set the value of the metadata property, this is no reactive and will not trigger a re-render
|
|
72
78
|
* @param {MetadataSetter<Metadata>} setter - The setter function or the value to set
|
|
73
79
|
* */
|
|
74
|
-
setMetadata: MetadataSetter<Metadata
|
|
80
|
+
setMetadata(setter: Parameters<MetadataSetter<Metadata>>[0]): void;
|
|
75
81
|
/**
|
|
76
82
|
* Returns the metadata [non-reactive additional information associated with the global state]
|
|
77
83
|
*/
|
|
78
|
-
getMetadata
|
|
84
|
+
getMetadata(): Metadata;
|
|
79
85
|
/**
|
|
80
86
|
* Get the current value of the state
|
|
81
87
|
*/
|
|
82
|
-
getState
|
|
88
|
+
getState(): State;
|
|
83
89
|
/**
|
|
84
90
|
* Subscribe an individual callback to state changes
|
|
85
91
|
*/
|
|
86
|
-
subscribe:
|
|
92
|
+
subscribe<TDerivate>(...[param1, param2, param3]: [
|
|
93
|
+
SubscribeCallback<State> | SelectorCallback<State, TDerivate>,
|
|
94
|
+
(SubscribeCallbackConfig<State> | SubscribeCallback<TDerivate>)?,
|
|
95
|
+
SubscribeCallbackConfig<State | TDerivate>?
|
|
96
|
+
]): UnsubscribeCallback;
|
|
87
97
|
/**
|
|
88
98
|
* Adds a subscription object to the subscribers set and returns the unsubscribe function
|
|
89
99
|
*/
|
|
90
|
-
protected subscribeCallback
|
|
91
|
-
partialUpdateSubscription
|
|
100
|
+
protected subscribeCallback(subscription: SubscriberParameters): () => void;
|
|
101
|
+
partialUpdateSubscription(subscription: SubscriberParameters, values: Partial<SubscriberParameters>): void;
|
|
92
102
|
/**
|
|
93
103
|
* Returns a custom hook that allows to handle a global state
|
|
94
104
|
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
|
|
95
105
|
* */
|
|
96
|
-
getMainHook
|
|
97
|
-
protected reselectIfDependenciesChanged
|
|
106
|
+
getMainHook(): StateHook<State, PublicStateMutator, Metadata>;
|
|
107
|
+
protected reselectIfDependenciesChanged({ subscription, newDependencies, currentDependencies, }: {
|
|
98
108
|
subscription: SubscriberParameters;
|
|
99
109
|
newDependencies: unknown[] | undefined;
|
|
100
110
|
currentDependencies: unknown[] | undefined;
|
|
101
|
-
})
|
|
111
|
+
}): void;
|
|
102
112
|
createSelectorHook: typeof createSelectorHook;
|
|
103
113
|
createObservable: typeof createObservable;
|
|
104
114
|
/**
|
|
105
115
|
* Returns the state setter or the actions map
|
|
106
116
|
* @returns {StateMutator} - The state setter or the actions map
|
|
107
117
|
* */
|
|
108
|
-
protected getStateOrchestrator
|
|
118
|
+
protected getStateOrchestrator(): PublicStateMutator;
|
|
109
119
|
/**
|
|
110
120
|
* This is the only setState function that should be exposed outside the class
|
|
111
121
|
* This is responsible for defining whenever or not the state change should be allowed or prevented
|
|
@@ -113,26 +123,22 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
113
123
|
* - onStateChanged (if defined) - this function is executed after the state change
|
|
114
124
|
* - computePreventStateChange (if defined) - this function is executed before the state change and it should return a boolean value that will be used to determine if the state change should be prevented or not
|
|
115
125
|
*/
|
|
116
|
-
setState
|
|
126
|
+
setState(setter: Parameters<React.Dispatch<React.SetStateAction<State>>>[0], { forceUpdate, identifier, }?: {
|
|
117
127
|
forceUpdate?: boolean;
|
|
118
128
|
identifier?: string;
|
|
119
|
-
})
|
|
129
|
+
}): void;
|
|
120
130
|
/**
|
|
121
131
|
* This creates storeTools and actions map if applicable
|
|
122
132
|
* */
|
|
123
|
-
getStoreActionsMap
|
|
133
|
+
getStoreActionsMap(): {
|
|
124
134
|
actions: PublicStateMutator extends AnyFunction ? null : PublicStateMutator;
|
|
125
135
|
storeTools: StoreTools<State, PublicStateMutator, Metadata>;
|
|
126
136
|
};
|
|
127
|
-
protected removeSubscriptions
|
|
128
|
-
protected executeCleanupTasks
|
|
129
|
-
dispose
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
*
|
|
133
|
-
* This method is reserved for advanced use cases and testing scenarios, use with caution.
|
|
134
|
-
*/
|
|
135
|
-
reset: (state: State, metadata: Metadata) => void;
|
|
137
|
+
protected removeSubscriptions(): void;
|
|
138
|
+
protected executeCleanupTasks(): void;
|
|
139
|
+
dispose(): void;
|
|
140
|
+
reset(): void;
|
|
141
|
+
reset(state: State, metadata: Metadata): void;
|
|
136
142
|
__devtools_getLifeCycleStoreToolsWrapper?: (logsPrefix: string) => StoreTools<State, PublicStateMutator, Metadata>;
|
|
137
143
|
__devtools_initialize_getStoreActionsMapWrapped?: () => {
|
|
138
144
|
actions: PublicStateMutator extends AnyFunction ? null : PublicStateMutator;
|
package/GlobalStore.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e;t=this,e=(t,e,s,i,n,o)=>(()=>{"use strict";var r={78:e=>{e.exports=t},132:function(t,e,s){var i,n=this&&this.__createBinding||(Object.create?function(t,e,s,i){void 0===i&&(i=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,i,n)}:function(t,e,s,i){void 0===i&&(i=s),t[i]=e[s]}),o=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),r=this&&this.__importStar||(i=function(t){return i=Object.getOwnPropertyNames||function(t){var e=[];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[e.length]=s);return e},i(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s=i(t),r=0;r<s.length;r++)"default"!==s[r]&&n(e,t,s[r]);return o(e,t),e}),a=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0,e.createObservable=g,e.createSelectorHook=v;const l=s(155),c=a(s(506)),u=a(s(487)),d=r(s(673)),h=a(s(361)),b=a(s(78)),p=a(s(627));class f{constructor(t,e={metadata:{}}){var s,i;this.cleanupFunctions=[],this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Set,this.initialize=async()=>{var t,e,s,i,n,o;const r=null!==(e=null===(t=this.__devtools_initialize_getStoreActionsMapWrapped)||void 0===t?void 0:t.call(this))&&void 0!==e?e:this.getStoreActionsMap(),{actions:a,storeTools:l}=r;this.actions=a,this.storeTools=l,this.use=this.getMainHook(),null===(s=this.onInit)||void 0===s||s.call(this);const{onInit:c}=null!==(i=this.callbacks)&&void 0!==i?i:{};if(!c)return;const u=null!==(o=null===(n=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===n?void 0:n.call(this,"config/onInit/"))&&void 0!==o?o:l;null==c||c(u)},this.executeSetStateForSubscriber=(t,e)=>{const{selector:s,onStoreChange:i,currentState:n,isEqualRoot:o=(t,e)=>t===e,isEqual:r=(t,e)=>t===e}=t;if(!e.forceUpdate&&o(e.currentState,e.newState))return{didUpdate:!1};const a=s?s(e.newState):e.newState;return!e.forceUpdate&&r(n,a)?{didUpdate:!1}:(this.partialUpdateSubscription(t,{currentState:a}),i({state:a},{identifier:e.identifier}),{didUpdate:!0})},this.setActualStateWithoutValidations=(t,{forceUpdate:e,identifier:s})=>{const i=this.state;if(!e&&i===t)return;this.state=t;const n=this.subscribers.values(),o={forceUpdate:e,newState:t,currentState:i,identifier:s};for(const t of n)this.executeSetStateForSubscriber(t,o)},this.setMetadata=t=>{const e=(0,c.default)(t)?t(this.metadata):t;this.metadata=e},this.getMetadata=()=>this.metadata,this.getState=()=>this.state,this.subscribe=(...[t,e,s])=>{var i;const n=(0,c.default)(e),o=n?t:void 0,r=n?e:t,a=null!==(i=n?s:e)&&void 0!==i?i:void 0,l=o?o(this.state):this.state;(null==a?void 0:a.skipFirst)||r(l);const u=Object.assign({selector:o,currentState:l,onStoreChange:({state:t})=>r(t)},a);return this.subscribeCallback(u),()=>{this.subscribers.delete(u)}},this.subscribeCallback=t=>{var e,s;return null===(s=null===(e=this.callbacks)||void 0===e?void 0:e.onSubscribed)||void 0===s||s.call(e,this.storeTools,t),this.subscribers.add(t),()=>{this.subscribers.delete(t)}},this.partialUpdateSubscription=(t,e)=>{Object.assign(t,e)},this.getMainHook=()=>{const t=`globalHook:${this._name}`,e=(e,s=[])=>{(0,l.useDebugValue)(t);const i=(0,d.isArray)(s)?{dependencies:s}:null!=s?s:{},n=(0,l.useRef)(null),o=t=>(0,c.default)(e)?e(t):t;n.current=(()=>n.current?n.current:Object.assign({selector:o,currentState:o(this.state),onStoreChange:()=>{throw new Error("Callback not set")}},i))();const r=n.current.dependencies,a=null==i?void 0:i.dependencies,u=Object.assign(Object.assign({},i),{selector:o,dependencies:a});this.partialUpdateSubscription(n.current,u);const{subscribe:h,getSnapshot:b,getServerSnapshot:p}=(0,l.useMemo)((()=>{const t=()=>n.current.currentState;return{subscribe:t=>(n.current.onStoreChange=t,this.subscribeCallback(n.current)),getSnapshot:t,getServerSnapshot:t}}),[]);return this.reselectIfDependenciesChanged({subscription:n.current,newDependencies:a,currentDependencies:r}),[(0,l.useSyncExternalStore)(h,b,p),this.getStateOrchestrator(),this.metadata]},{setMetadata:s,getMetadata:i,actions:n}=this,o={actions:n,createObservable:this.createObservable.bind(this),createSelectorHook:this.createSelectorHook.bind(this),dispose:this.dispose.bind(this),getMetadata:i,getState:this.getState.bind(this),reset:this.reset.bind(this),setMetadata:s,setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),subscribers:this.subscribers,use:e,select:(...t)=>e(...t)[0]};return Object.assign(e,o),e},this.reselectIfDependenciesChanged=({subscription:t,newDependencies:e,currentDependencies:s})=>{t.selector&&s!==e&&((null==s?void 0:s.length)===(null==e?void 0:e.length)&&(0,d.default)(s,e)||this.partialUpdateSubscription(t,{currentState:t.selector(this.state)}))},this.createSelectorHook=v,this.createObservable=g,this.getStateOrchestrator=()=>(()=>this.actions?this.actions:this.setState)(),this.setState=(t,{forceUpdate:e,identifier:s}={})=>{var i,n,o,r;const a=this.state,l=(0,c.default)(t)?t(a):t;if(!e&&this.state===l)return;const u=this.setActualStateWithoutValidations,d={previousState:a,state:l,identifier:s},h=Object.assign(Object.assign(Object.assign({},this.storeTools),d),{setState:u});if((null===(i=this.callbacks)||void 0===i?void 0:i.computePreventStateChange)&&(null===(o=null===(n=this.callbacks)||void 0===n?void 0:n.computePreventStateChange)||void 0===o?void 0:o.call(n,h)))return;this.setActualStateWithoutValidations(l,{forceUpdate:e,identifier:s});const{onStateChanged:b}=this,p=null===(r=this.callbacks)||void 0===r?void 0:r.onStateChanged;null==b||b(h),null==p||p(h)},this.getStoreActionsMap=()=>{const{actionsConfig:t,getMetadata:e,getState:s,setMetadata:i,setState:n,subscribe:o}=this,r={setMetadata:i,getMetadata:e,getState:s,setState:n,subscribe:o,actions:null};if(!(0,u.default)(t))return{actions:null,storeTools:r};const a={};r.actions=a;const l=Object.keys(t);for(const e of l)Object.assign(a,{[e](...s){const i=t[e].apply(a,s);return"function"!=typeof i&&(0,h.default)(e),i.call(a,r)}});return{actions:a,storeTools:r}},this.removeSubscriptions=()=>{this.subscribers.clear()},this.executeCleanupTasks=()=>{this.cleanupFunctions.forEach((t=>{null==t||t()})),this.cleanupFunctions.length=0},this.dispose=()=>{this.removeSubscriptions(),this.executeCleanupTasks()},this.reset=(t,e)=>{var s,i,n,o,r,a;this.executeCleanupTasks(),this.setActualStateWithoutValidations(t,{forceUpdate:!0}),this.metadata=e;const l=null!==(i=null===(s=this.onInit)||void 0===s?void 0:s.call(this))&&void 0!==i?i:null;(0,c.default)(l)&&this.cleanupFunctions.push(l);const{onInit:u}=null!==(n=this.callbacks)&&void 0!==n?n:{};if(!u)return;const d=null!==(r=null===(o=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===o?void 0:o.call(this,"config/onInit/"))&&void 0!==r?r:this.storeTools,h=null!==(a=null==u?void 0:u(d))&&void 0!==a?a:null;(0,c.default)(h)&&this.cleanupFunctions.push(h)};const{metadata:n,callbacks:o,actions:r,name:a}=e;if(this.state=t,this._name=null!=a?a:(0,b.default)("gs:"),this.metadata=null!=n?n:{},this.callbacks=null!=o?o:null,this.actionsConfig=null!=r?r:null,p.default.isDevToolsPresent){const t=null!==(s=(new Error).stack)&&void 0!==s?s:"";null===(i=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(p.default,this,e,t)}this.constructor!==f||this.initialize()}}function g(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let o=this.getState(),r=(null!=t?t:t=>t)(o);const a=new f(r,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(o,e))return;o=e;const s=t(e);(null!=n?n:Object.is)(r,s)||(r=s,a.setState(s))}),{skipFirst:!0}),c=a.subscribe.bind(a),u={getState:a.getState.bind(a),subscribe:a.subscribe.bind(a),createSelectorHook:v.bind(c),createObservable:g.bind(c),dispose:()=>{l(),a.dispose()},subscribers:a.subscribers};return Object.assign(c,u),c}function v(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let o=this.getState(),r=(null!=t?t:t=>t)(o);const a=new f(r,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(o,e))return;o=e;const s=t(e);(null!=n?n:Object.is)(r,s)||(r=s,a.setState(s))}),{skipFirst:!0}),c=(...t)=>{const[e]=a.use(...t);return e},u={getState:()=>a.getState(),subscribe:a.subscribe.bind(a),createSelectorHook:v.bind(c),createObservable:g.bind(c),dispose:()=>{l(),a.dispose()},subscribers:a.subscribers};return Object.assign(c,u),c}e.GlobalStore=f,e.default=f},155:t=>{t.exports=e},361:t=>{t.exports=s},487:t=>{t.exports=i},506:t=>{t.exports=n},627:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});const s=globalThis;s.isDevToolsPresent=Boolean(s.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=s},673:t=>{t.exports=o}},a={};return function t(e){var s=a[e];if(void 0!==s)return s.exports;var i=a[e]={exports:{}};return r[e].call(i.exports,i,i.exports,t),i.exports}(132)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./uniqueId.js"),require("react"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","json-storage-formatter/isFunction","./shallowCompare.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("./uniqueId.js"),require("react"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):t["react-hooks-global-states"]=e(t["./uniqueId.js"],t.react,t["./throwWrongKeyOnActionCollectionConfig.js"],t["./isRecord.js"],t["json-storage-formatter/isFunction"],t["./shallowCompare.js"]);
|
|
1
|
+
var t,e;t=this,e=(t,e,s,i,n,a)=>(()=>{"use strict";var o={78:e=>{e.exports=t},132:function(t,e,s){var i,n=this&&this.__createBinding||(Object.create?function(t,e,s,i){void 0===i&&(i=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,i,n)}:function(t,e,s,i){void 0===i&&(i=s),t[i]=e[s]}),a=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),o=this&&this.__importStar||(i=function(t){return i=Object.getOwnPropertyNames||function(t){var e=[];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[e.length]=s);return e},i(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s=i(t),o=0;o<s.length;o++)"default"!==s[o]&&n(e,t,s[o]);return a(e,t),e}),r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0,e.createObservable=v,e.createSelectorHook=g;const l=s(155),c=r(s(506)),u=r(s(487)),d=o(s(673)),h=r(s(361)),b=r(s(78)),p=r(s(627));class f{constructor(t,e={metadata:{}}){var s,i;this.cleanupFunctions=[],this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Set,this.createSelectorHook=g,this.createObservable=v;const{metadata:n,callbacks:a,actions:o,name:r}=e;if((0,c.default)(t)&&(this.stateCallback=t),(0,c.default)(n)&&(this.metadataCallback=n),this._name=null!=r?r:(0,b.default)("gs:"),this.state=this.stateCallback?this.stateCallback():t,this.metadata=this.metadataCallback?this.metadataCallback():null!=n?n:{},this.callbacks=null!=a?a:null,this.actionsConfig=null!=o?o:null,p.default.isDevToolsPresent){const t=null!==(s=(new Error).stack)&&void 0!==s?s:"";null===(i=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(p.default,this,e,t)}this.constructor!==f||this.initialize()}async initialize(){var t,e,s,i,n,a,o,r;const l=null!==(e=null===(t=this.__devtools_initialize_getStoreActionsMapWrapped)||void 0===t?void 0:t.call(this))&&void 0!==e?e:this.getStoreActionsMap(),{actions:u,storeTools:d}=l;this.actions=u,this.storeTools=d,this.use=this.getMainHook();const h=null!==(i=null===(s=this.onInit)||void 0===s?void 0:s.call(this))&&void 0!==i?i:null;(0,c.default)(h)&&this.cleanupFunctions.push(h);const{onInit:b}=null!==(n=this.callbacks)&&void 0!==n?n:{};if(!b)return;const p=null!==(o=null===(a=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===a?void 0:a.call(this,"config/onInit/"))&&void 0!==o?o:d,f=null!==(r=null==b?void 0:b(p))&&void 0!==r?r:null;(0,c.default)(f)&&this.cleanupFunctions.push(f)}executeSetStateForSubscriber(t,e){const{selector:s,onStoreChange:i,currentState:n,isEqualRoot:a=(t,e)=>t===e,isEqual:o=(t,e)=>t===e}=t;if(!e.forceUpdate&&a(e.currentState,e.newState))return{didUpdate:!1};const r=s?s(e.newState):e.newState;return!e.forceUpdate&&o(n,r)?{didUpdate:!1}:(this.partialUpdateSubscription(t,{currentState:r}),i({state:r},{identifier:e.identifier}),{didUpdate:!0})}setActualStateWithoutValidations(t,{forceUpdate:e,identifier:s}){const i=this.state;if(!e&&i===t)return;this.state=t;const n=this.subscribers.values(),a={forceUpdate:e,newState:t,currentState:i,identifier:s};for(const t of n)this.executeSetStateForSubscriber(t,a)}setMetadata(t){const e=(0,c.default)(t)?t(this.metadata):t;this.metadata=e}getMetadata(){return this.metadata}getState(){return this.state}subscribe(...[t,e,s]){var i;const n=(0,c.default)(e),a=n?t:void 0,o=n?e:t,r=null!==(i=n?s:e)&&void 0!==i?i:void 0,l=a?a(this.state):this.state;(null==r?void 0:r.skipFirst)||o(l);const u=Object.assign({selector:a,currentState:l,onStoreChange:({state:t})=>o(t)},r);return this.subscribeCallback(u),()=>{this.subscribers.delete(u)}}subscribeCallback(t){var e,s;return null===(s=null===(e=this.callbacks)||void 0===e?void 0:e.onSubscribed)||void 0===s||s.call(e,this.storeTools,t),this.subscribers.add(t),()=>{this.subscribers.delete(t)}}partialUpdateSubscription(t,e){Object.assign(t,e)}getMainHook(){const t=`globalHook:${this._name}`,e=(e,s=[])=>{(0,l.useDebugValue)(t);const i=(0,d.isArray)(s)?{dependencies:s}:null!=s?s:{},n=(0,l.useRef)(null),a=t=>(0,c.default)(e)?e(t):t;n.current=(()=>n.current?n.current:Object.assign({selector:a,currentState:a(this.state),onStoreChange:()=>{throw new Error("Callback not set")}},i))();const o=n.current.dependencies,r=null==i?void 0:i.dependencies,u=Object.assign(Object.assign({},i),{selector:a,dependencies:r});this.partialUpdateSubscription(n.current,u);const{subscribe:h,getSnapshot:b,getServerSnapshot:p}=(0,l.useMemo)((()=>{const t=()=>n.current.currentState;return{subscribe:t=>(n.current.onStoreChange=t,this.subscribeCallback(n.current)),getSnapshot:t,getServerSnapshot:t}}),[]);return this.reselectIfDependenciesChanged({subscription:n.current,newDependencies:r,currentDependencies:o}),[(0,l.useSyncExternalStore)(h,b,p),this.getStateOrchestrator(),this.metadata]},s={actions:this.actions,createObservable:this.createObservable.bind(this),createSelectorHook:this.createSelectorHook.bind(this),dispose:this.dispose.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),reset:this.reset.bind(this),setMetadata:this.setMetadata.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),subscribers:this.subscribers,use:e,select:(...t)=>e(...t)[0]};return Object.assign(e,s),e}reselectIfDependenciesChanged({subscription:t,newDependencies:e,currentDependencies:s}){t.selector&&s!==e&&((null==s?void 0:s.length)===(null==e?void 0:e.length)&&(0,d.default)(s,e)||this.partialUpdateSubscription(t,{currentState:t.selector(this.state)}))}getStateOrchestrator(){return(()=>this.actions?this.actions:this.setState.bind(this))()}setState(t,{forceUpdate:e,identifier:s}={}){var i,n,a,o,r,l;const u=this.state,d=(0,c.default)(t)?t(u):t;if(!e&&this.state===d)return;const h=this.setActualStateWithoutValidations,b={previousState:u,state:d,identifier:s},p=Object.assign(Object.assign(Object.assign({},this.storeTools),b),{setState:h});(null===(i=this.callbacks)||void 0===i?void 0:i.computePreventStateChange)&&(null===(a=null===(n=this.callbacks)||void 0===n?void 0:n.computePreventStateChange)||void 0===a?void 0:a.call(n,p))||(this.setActualStateWithoutValidations(d,{forceUpdate:e,identifier:s}),null===(o=this.onStateChanged)||void 0===o||o.call(this,p),null===(l=null===(r=this.callbacks)||void 0===r?void 0:r.onStateChanged)||void 0===l||l.call(r,p))}getStoreActionsMap(){const t={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),actions:null};if(!(0,u.default)(this.actionsConfig))return{actions:null,storeTools:t};const e=this.actionsConfig,s={};t.actions=s;const i=Object.keys(e);for(const n of i)Object.assign(s,{[n](...i){const a=e[n].apply(s,i);return"function"!=typeof a&&(0,h.default)(n),a.call(s,t)}});return{actions:s,storeTools:t}}removeSubscriptions(){this.subscribers.clear()}executeCleanupTasks(){this.cleanupFunctions.forEach((t=>{null==t||t()})),this.cleanupFunctions.length=0}dispose(){this.removeSubscriptions(),this.executeCleanupTasks()}reset(t,s){var i,n,a,o,r,l;this.executeCleanupTasks();const u=(()=>t===e.DEFAULT?this.state:this.stateCallback?this.stateCallback():t)(),d=(()=>s===e.DEFAULT?this.metadata:this.metadataCallback?this.metadataCallback():s)();this.setActualStateWithoutValidations(u,{forceUpdate:!0}),this.metadata=d;const h=null!==(n=null===(i=this.onInit)||void 0===i?void 0:i.call(this))&&void 0!==n?n:null;(0,c.default)(h)&&this.cleanupFunctions.push(h);const{onInit:b}=null!==(a=this.callbacks)&&void 0!==a?a:{};if(!b)return;const p=null!==(r=null===(o=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===o?void 0:o.call(this,"config/onInit/"))&&void 0!==r?r:this.storeTools,f=null!==(l=null==b?void 0:b(p))&&void 0!==l?l:null;(0,c.default)(f)&&this.cleanupFunctions.push(f)}}function v(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=r.subscribe.bind(r),u={getState:r.getState.bind(r),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}function g(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=(...t)=>{const[e]=r.use(...t);return e},u={getState:()=>r.getState(),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}e.GlobalStore=f,e.default=f},155:t=>{t.exports=e},361:t=>{t.exports=s},487:t=>{t.exports=i},506:t=>{t.exports=n},627:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});const s=globalThis;s.isDevToolsPresent=Boolean(s.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=s},673:t=>{t.exports=a}},r={};return function t(e){var s=r[e];if(void 0!==s)return s.exports;var i=r[e]={exports:{}};return o[e].call(i.exports,i,i.exports,t),i.exports}(132)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./uniqueId.js"),require("react"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","json-storage-formatter/isFunction","./shallowCompare.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("./uniqueId.js"),require("react"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):t["react-hooks-global-states"]=e(t["./uniqueId.js"],t.react,t["./throwWrongKeyOnActionCollectionConfig.js"],t["./isRecord.js"],t["json-storage-formatter/isFunction"],t["./shallowCompare.js"]);
|