react-hooks-global-states 11.0.0-beta.2 → 11.0.0-beta.3
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 +2 -5
- package/GlobalStore.js +3 -3
- package/createContext.d.ts +1 -1
- package/createGlobalState.d.ts +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/types.d.ts +4 -0
package/GlobalStore.d.ts
CHANGED
|
@@ -88,10 +88,7 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
88
88
|
* Returns a custom hook that allows to handle a global state
|
|
89
89
|
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
|
|
90
90
|
* */
|
|
91
|
-
getMainHook: () => StateHook<State, StateDispatch, PublicStateMutator, Metadata
|
|
92
|
-
removeSubscriptions: () => void;
|
|
93
|
-
dispose: () => void;
|
|
94
|
-
};
|
|
91
|
+
getMainHook: () => StateHook<State, StateDispatch, PublicStateMutator, Metadata>;
|
|
95
92
|
protected computeSelectedState: ({ subscription, currentDependencies, }: {
|
|
96
93
|
subscription: SubscriberParameters;
|
|
97
94
|
currentDependencies: unknown[] | undefined;
|
|
@@ -127,7 +124,7 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
127
124
|
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
128
125
|
* */
|
|
129
126
|
getStoreActionsMap: () => typeof this.actions;
|
|
130
|
-
removeSubscriptions: () => void;
|
|
127
|
+
protected removeSubscriptions: () => void;
|
|
131
128
|
dispose: () => void;
|
|
132
129
|
}
|
|
133
130
|
export default GlobalStore;
|
package/GlobalStore.js
CHANGED
|
@@ -97,7 +97,7 @@ this.getConfigCallbackParam=function(){var t=u.setMetadata,e=u.getMetadata,r=u.g
|
|
|
97
97
|
* Returns a custom hook that allows to handle a global state
|
|
98
98
|
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
|
|
99
99
|
* */
|
|
100
|
-
this.getMainHook=function(){var t=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=(0,d.isArray)(e)?{dependencies:e}:null!=e?e:{},n=(0,s.useRef)({selector:t,config:r}),i=n.current.config.dependencies;n.current.selector=t,n.current.config=r;var o=(0,s.useMemo)((function(){var t=function(t){var e=n.current.selector;return(0,c.isFunction)(e)?e(t):t},e={currentState:t(u.state),selector:t,getConfig:function(){return n.current.config},callback:function(){throw new Error("Callback not set")}},r=function(){return e.currentState};return{subscribe:function(t){return e.callback=t,u.subscribeCallback(e)},getSnapshot:r,getServerSnapshot:r,subscription:e}}),[]),a=o.subscribe,l=o.getSnapshot,f=o.getServerSnapshot,h=o.subscription;return(0,s.useSyncExternalStore)(a,l,f),[u.computeSelectedState({subscription:h,currentDependencies:i}),u.getStateOrchestrator(),u.metadata]},e={setMetadata:u.setMetadata.bind(u),getMetadata:u.getMetadata.bind(u),actions:u.actions,setState:u.actions?null:u.setState,getState:u.getState.bind(u),subscribe:u.subscribe.bind(u),createSelectorHook:u.createSelectorHook.bind(t),createObservable:u.createObservable.bind(t),
|
|
100
|
+
this.getMainHook=function(){var t=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=(0,d.isArray)(e)?{dependencies:e}:null!=e?e:{},n=(0,s.useRef)({selector:t,config:r}),i=n.current.config.dependencies;n.current.selector=t,n.current.config=r;var o=(0,s.useMemo)((function(){var t=function(t){var e=n.current.selector;return(0,c.isFunction)(e)?e(t):t},e={currentState:t(u.state),selector:t,getConfig:function(){return n.current.config},callback:function(){throw new Error("Callback not set")}},r=function(){return e.currentState};return{subscribe:function(t){return e.callback=t,u.subscribeCallback(e)},getSnapshot:r,getServerSnapshot:r,subscription:e}}),[]),a=o.subscribe,l=o.getSnapshot,f=o.getServerSnapshot,h=o.subscription;return(0,s.useSyncExternalStore)(a,l,f),[u.computeSelectedState({subscription:h,currentDependencies:i}),u.getStateOrchestrator(),u.metadata]},e={setMetadata:u.setMetadata.bind(u),getMetadata:u.getMetadata.bind(u),actions:u.actions,setState:u.actions?null:u.setState,getState:u.getState.bind(u),subscribe:u.subscribe.bind(u),createSelectorHook:u.createSelectorHook.bind(t),createObservable:u.createObservable.bind(t),dispose:u.dispose.bind(u)};
|
|
101
101
|
/**
|
|
102
102
|
* Extended properties and methods of the hook
|
|
103
103
|
*/return Object.assign(t,e),t},this.computeSelectedState=function(t){var e,r=t.subscription,n=t.currentDependencies;if(!r.selector)return r.currentState;var i=(null!==(e=r.getConfig())&&void 0!==e?e:{}).dependencies;return n===i||(null==n?void 0:n.length)===(null==i?void 0:i.length)&&(0,d.shallowCompare)(n,i)||u.partialUpdateSubscription(r,{currentState:r.selector(u.state)}),r.currentState},this.createSelectorHook=m.bind(this),this.createObservable=y.bind(this),
|
|
@@ -118,11 +118,11 @@ this.setState=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?ar
|
|
|
118
118
|
* This creates a map of actions that can be used to modify or interact with the state
|
|
119
119
|
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
120
120
|
* */
|
|
121
|
-
this.getStoreActionsMap=function(){if(!(0,h.isRecord)(u.actionsConfig))return null;var t=u.actionsConfig,e=u.setMetadata,r=u.setState,n=u.getState,i=u.getMetadata,a=Object.keys(t).reduce((function(s,c){var u,l,f;return Object.assign(s,(u={},f=function(){for(var o=t[c],s=arguments.length,u=new Array(s),l=0;l<s;l++)u[l]=arguments[l];var f=o.apply(a,u);return"function"!=typeof f&&(0,b.throwWrongKeyOnActionCollectionConfig)(c),f.call(a,{setState:r,getState:n,setMetadata:e,getMetadata:i,actions:a})},(l=o(l=c))in u?Object.defineProperty(u,l,{value:f,enumerable:!0,configurable:!0,writable:!0}):u[l]=f,u)),s}),{});return a},this.removeSubscriptions=function(){u.subscribers.clear()},this.dispose=function(){u.removeSubscriptions(),u._name="",u.actionsConfig=null,u.callbacks=null,u.metadata={},u.actions=null,u.state=Object.create(null)};var g=l.metadata,S=l.callbacks,w=l.actions,j=l.name;if(this.state=e,this._name=null!=j?j:(0,v.uniqueId)("gs:"),this.metadata=null!=g?g:{},this.callbacks=null!=S?S:null,this.actionsConfig=null!=w?w:null,p.default.isDevToolsPresent){var O=null!==(n=(new Error).stack)&&void 0!==n?n:"";null===(a=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===a||a.call(p.default,this,l,O)}this.constructor!==t||this.initialize()},e=[{key:"subscribe",value:function(){for(var t=this,e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var i,o=r[0],a=r[1],s=r[2],u=(0,c.isFunction)(a),l=u?o:void 0,f=u?a:o,h=null!==(i=u?s:a)&&void 0!==i?i:void 0,d=l?l(this.state):this.state;(null==h?void 0:h.skipFirst)||f(d);var b={selector:l,getConfig:function(){return h},currentState:d,callback:function(t){var e=t.state;return f(e)}};return this.subscribeCallback(b),function(){t.subscribers.delete(b)}}}],e&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,o(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();function y(t,e){var r=null==e?void 0:e.name,n=null==e?void 0:e.isEqualRoot,i=null==e?void 0:e.isEqual,o=this.getState(),a=(null!=t?t:function(t){return t})(o),s=new g(a,{name:null!=r?r:(0,v.uniqueId)("sh:")}),c=this.subscribe((function(e){if(!(null!=n?n:Object.is)(o,e)){o=e;var r=t(e);(null!=i?i:Object.is)(a,r)||(a=r,s.setState(r))}}),{skipFirst:!0}),u=this.actions?null:this.setState,l=s.subscribe.bind(s),f={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),actions:this.actions,setState:u,getState:s.getState.bind(s),subscribe:s.subscribe.bind(s),createSelectorHook:m.bind(l),createObservable:y.bind(l),
|
|
121
|
+
this.getStoreActionsMap=function(){if(!(0,h.isRecord)(u.actionsConfig))return null;var t=u.actionsConfig,e=u.setMetadata,r=u.setState,n=u.getState,i=u.getMetadata,a=Object.keys(t).reduce((function(s,c){var u,l,f;return Object.assign(s,(u={},f=function(){for(var o=t[c],s=arguments.length,u=new Array(s),l=0;l<s;l++)u[l]=arguments[l];var f=o.apply(a,u);return"function"!=typeof f&&(0,b.throwWrongKeyOnActionCollectionConfig)(c),f.call(a,{setState:r,getState:n,setMetadata:e,getMetadata:i,actions:a})},(l=o(l=c))in u?Object.defineProperty(u,l,{value:f,enumerable:!0,configurable:!0,writable:!0}):u[l]=f,u)),s}),{});return a},this.removeSubscriptions=function(){u.subscribers.clear()},this.dispose=function(){u.removeSubscriptions(),u._name="",u.actionsConfig=null,u.callbacks=null,u.metadata={},u.actions=null,u.state=Object.create(null)};var g=l.metadata,S=l.callbacks,w=l.actions,j=l.name;if(this.state=e,this._name=null!=j?j:(0,v.uniqueId)("gs:"),this.metadata=null!=g?g:{},this.callbacks=null!=S?S:null,this.actionsConfig=null!=w?w:null,p.default.isDevToolsPresent){var O=null!==(n=(new Error).stack)&&void 0!==n?n:"";null===(a=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===a||a.call(p.default,this,l,O)}this.constructor!==t||this.initialize()},e=[{key:"subscribe",value:function(){for(var t=this,e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var i,o=r[0],a=r[1],s=r[2],u=(0,c.isFunction)(a),l=u?o:void 0,f=u?a:o,h=null!==(i=u?s:a)&&void 0!==i?i:void 0,d=l?l(this.state):this.state;(null==h?void 0:h.skipFirst)||f(d);var b={selector:l,getConfig:function(){return h},currentState:d,callback:function(t){var e=t.state;return f(e)}};return this.subscribeCallback(b),function(){t.subscribers.delete(b)}}}],e&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,o(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();function y(t,e){var r=null==e?void 0:e.name,n=null==e?void 0:e.isEqualRoot,i=null==e?void 0:e.isEqual,o=this.getState(),a=(null!=t?t:function(t){return t})(o),s=new g(a,{name:null!=r?r:(0,v.uniqueId)("sh:")}),c=this.subscribe((function(e){if(!(null!=n?n:Object.is)(o,e)){o=e;var r=t(e);(null!=i?i:Object.is)(a,r)||(a=r,s.setState(r))}}),{skipFirst:!0}),u=this.actions?null:this.setState,l=s.subscribe.bind(s),f={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),actions:this.actions,setState:u,getState:s.getState.bind(s),subscribe:s.subscribe.bind(s),createSelectorHook:m.bind(l),createObservable:y.bind(l),dispose:function(){c(),s.dispose()}};return Object.assign(l,f),l}
|
|
122
122
|
/**
|
|
123
123
|
* @description
|
|
124
124
|
* Creates a derived hook bound to a selected fragment of the root state.
|
|
125
125
|
* The derived hook re-renders only when the selected value changes and
|
|
126
126
|
* exposes the same API as the parent state hook.
|
|
127
|
-
*/function m(t,e){var n,i=this,o=null==e?void 0:e.name,a=null==e?void 0:e.isEqualRoot,s=null==e?void 0:e.isEqual,c=this.getState(),u=(null!=t?t:function(t){return t})(c),l=new g(u,{name:null!=o?o:(0,v.uniqueId)("sh:")}),f=this.subscribe((function(e){if(!(null!=a?a:Object.is)(c,e)){c=e;var r=t(e);(null!=s?s:Object.is)(u,r)||(u=r,l.setState(r))}}),{skipFirst:!0}),h=null!==(n=this.actions)&&void 0!==n?n:this.setState,d=this.actions?null:this.setState,b=function(){return[(t=l.use.apply(l,arguments),function(t){if(Array.isArray(t))return t}(t)||function(t){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var r,n,i,o,a=[],s=!0,c=!1;try{for(i=(e=e.call(t)).next;!(s=(r=i.call(e)).done)&&(a.push(r.value),1!==a.length);s=!0);}catch(t){c=!0,n=t}finally{try{if(!s&&null!=e.return&&(o=e.return(),Object(o)!==o))return}finally{if(c)throw n}}return a}}(t)||r(t,1)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}())[0],h,i.getMetadata()];var t},p={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),actions:this.actions,setState:d,getState:this.getState.bind(this),subscribe:this.subscribe.bind(this),createSelectorHook:m.bind(b),createObservable:y.bind(b),
|
|
127
|
+
*/function m(t,e){var n,i=this,o=null==e?void 0:e.name,a=null==e?void 0:e.isEqualRoot,s=null==e?void 0:e.isEqual,c=this.getState(),u=(null!=t?t:function(t){return t})(c),l=new g(u,{name:null!=o?o:(0,v.uniqueId)("sh:")}),f=this.subscribe((function(e){if(!(null!=a?a:Object.is)(c,e)){c=e;var r=t(e);(null!=s?s:Object.is)(u,r)||(u=r,l.setState(r))}}),{skipFirst:!0}),h=null!==(n=this.actions)&&void 0!==n?n:this.setState,d=this.actions?null:this.setState,b=function(){return[(t=l.use.apply(l,arguments),function(t){if(Array.isArray(t))return t}(t)||function(t){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var r,n,i,o,a=[],s=!0,c=!1;try{for(i=(e=e.call(t)).next;!(s=(r=i.call(e)).done)&&(a.push(r.value),1!==a.length);s=!0);}catch(t){c=!0,n=t}finally{try{if(!s&&null!=e.return&&(o=e.return(),Object(o)!==o))return}finally{if(c)throw n}}return a}}(t)||r(t,1)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}())[0],h,i.getMetadata()];var t},p={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),actions:this.actions,setState:d,getState:this.getState.bind(this),subscribe:this.subscribe.bind(this),createSelectorHook:m.bind(b),createObservable:y.bind(b),dispose:function(){f(),l.dispose()}};return Object.assign(b,p),b}t.GlobalStore=g,t.default=g})(),l;
|
|
128
128
|
/******/})(),"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"),require("json-storage-formatter/isNil")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","json-storage-formatter/isFunction","./shallowCompare.js","json-storage-formatter/isNil"],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"),require("json-storage-formatter/isNil")):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"],t["json-storage-formatter/isNil"]);
|
package/createContext.d.ts
CHANGED
|
@@ -141,7 +141,7 @@ export type HookExtensions<State, StateMutator, Metadata extends BaseMetadata> =
|
|
|
141
141
|
*/
|
|
142
142
|
api: () => ContextApi<State, StateMutator, Metadata>;
|
|
143
143
|
};
|
|
144
|
-
|
|
144
|
+
interface CreateContext {
|
|
145
145
|
/**
|
|
146
146
|
* @description Creates a highly granular React context with its associated provider and state hook.
|
|
147
147
|
* @param value Initial state value or initializer function.
|
package/createGlobalState.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ActionCollectionConfig, ActionCollectionResult, StateHook, BaseMetadata, GlobalStoreCallbacks } from './types';
|
|
2
|
-
|
|
2
|
+
interface CreateGlobalState {
|
|
3
3
|
/**
|
|
4
4
|
* Creates a global state hook.
|
|
5
5
|
* @param state initial state value
|
package/index.d.ts
CHANGED
|
@@ -6,4 +6,4 @@ export { shallowCompare } from './shallowCompare';
|
|
|
6
6
|
export { uniqueId } from './uniqueId';
|
|
7
7
|
export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
|
|
8
8
|
export { isRecord } from './isRecord';
|
|
9
|
-
export { type ContextApi
|
|
9
|
+
export { type ContextApi, type ContextProvider, type ContextHook, type InferContextApi, createContext, } from './createContext';
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -59,6 +59,10 @@ export type StateApi<State, StateDispatch, StateMutator, Metadata extends BaseMe
|
|
|
59
59
|
*/
|
|
60
60
|
name?: string;
|
|
61
61
|
}) => ObservableFragment<Selection, StateDispatch, StateMutator, Metadata>;
|
|
62
|
+
/**
|
|
63
|
+
* @description Disposes the global state instance, cleaning up all resources and subscriptions.
|
|
64
|
+
*/
|
|
65
|
+
dispose: () => void;
|
|
62
66
|
};
|
|
63
67
|
/**
|
|
64
68
|
* @description Function that allows you to subscribe to a fragment of the state
|