react-hooks-global-states 11.0.0-beta.2 → 11.0.0-beta.4
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 +21 -16
- package/GlobalStore.js +18 -21
- 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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, SubscribeCallbackConfig, SubscribeCallback, SelectorCallback, SubscriberParameters, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment, UnsubscribeCallback, AnyFunction } from './types';
|
|
1
|
+
import type { ActionCollectionConfig, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, SubscribeCallbackConfig, SubscribeCallback, SelectorCallback, SubscriberParameters, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment, StateApi, UnsubscribeCallback, AnyFunction } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
|
|
4
4
|
* */
|
|
@@ -88,24 +88,13 @@ 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;
|
|
98
95
|
}) => unknown;
|
|
99
|
-
createSelectorHook:
|
|
100
|
-
|
|
101
|
-
isEqualRoot?: ((current: unknown, next: unknown) => boolean) | undefined;
|
|
102
|
-
name?: string;
|
|
103
|
-
} | undefined) => StateHook<unknown, unknown, unknown, BaseMetadata>;
|
|
104
|
-
createObservable: (selector: (state: unknown) => unknown, options?: {
|
|
105
|
-
isEqual?: ((current: unknown, next: unknown) => boolean) | undefined;
|
|
106
|
-
isEqualRoot?: ((current: unknown, next: unknown) => boolean) | undefined;
|
|
107
|
-
name?: string;
|
|
108
|
-
} | undefined) => ObservableFragment<unknown, unknown, unknown, BaseMetadata>;
|
|
96
|
+
createSelectorHook: typeof createSelectorHook;
|
|
97
|
+
createObservable: typeof createObservable;
|
|
109
98
|
/**
|
|
110
99
|
* Returns the state setter or the actions map
|
|
111
100
|
* @returns {StateMutator} - The state setter or the actions map
|
|
@@ -127,7 +116,23 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
127
116
|
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
128
117
|
* */
|
|
129
118
|
getStoreActionsMap: () => typeof this.actions;
|
|
130
|
-
removeSubscriptions: () => void;
|
|
119
|
+
protected removeSubscriptions: () => void;
|
|
131
120
|
dispose: () => void;
|
|
132
121
|
}
|
|
122
|
+
declare function createObservable<RootState, PublicStateMutator, Metadata, Selected, StateDispatch = React.Dispatch<React.SetStateAction<RootState>>>(this: StateApi<any, any, any, any>, selector: (state: any) => Selected, options?: {
|
|
123
|
+
isEqual?: (current: Selected, next: Selected) => boolean;
|
|
124
|
+
isEqualRoot?: (current: any, next: any) => boolean;
|
|
125
|
+
name?: string;
|
|
126
|
+
}): ObservableFragment<Selected, StateDispatch, PublicStateMutator, Metadata extends BaseMetadata ? Metadata : BaseMetadata>;
|
|
127
|
+
/**
|
|
128
|
+
* @description
|
|
129
|
+
* Creates a derived hook bound to a selected fragment of the root state.
|
|
130
|
+
* The derived hook re-renders only when the selected value changes and
|
|
131
|
+
* exposes the same API as the parent state hook.
|
|
132
|
+
*/
|
|
133
|
+
declare function createSelectorHook<RootState, PublicStateMutator, Metadata, Selected, StateDispatch = React.Dispatch<React.SetStateAction<RootState>>>(this: StateApi<any, any, any, any>, selector: (state: any) => Selected, options?: {
|
|
134
|
+
isEqual?: (current: Selected, next: Selected) => boolean;
|
|
135
|
+
isEqualRoot?: (current: any, next: any) => boolean;
|
|
136
|
+
name?: string;
|
|
137
|
+
}): StateHook<Selected, StateDispatch, PublicStateMutator, any>;
|
|
133
138
|
export default GlobalStore;
|
package/GlobalStore.js
CHANGED
|
@@ -25,20 +25,20 @@ var t,e;t=this,e=(t,e,r,n,i,o,a)=>/******/(()=>{
|
|
|
25
25
|
/***/811:
|
|
26
26
|
/***/(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});var r=globalThis;r.isDevToolsPresent=Boolean(r.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=r}
|
|
27
27
|
/***/
|
|
28
|
-
/******/},
|
|
28
|
+
/******/},u={};
|
|
29
29
|
/************************************************************************/
|
|
30
30
|
/******/
|
|
31
31
|
/******/
|
|
32
32
|
/******/
|
|
33
33
|
/******/
|
|
34
|
-
/******/function
|
|
34
|
+
/******/function c(t){
|
|
35
35
|
/******/
|
|
36
|
-
/******/var e=
|
|
36
|
+
/******/var e=u[t];
|
|
37
37
|
/******/if(void 0!==e)
|
|
38
38
|
/******/return e.exports;
|
|
39
39
|
/******/
|
|
40
40
|
/******/
|
|
41
|
-
/******/var r=
|
|
41
|
+
/******/var r=u[t]={
|
|
42
42
|
/******/
|
|
43
43
|
/******/
|
|
44
44
|
/******/exports:{}
|
|
@@ -48,11 +48,11 @@ var t,e;t=this,e=(t,e,r,n,i,o,a)=>/******/(()=>{
|
|
|
48
48
|
/******/
|
|
49
49
|
/******/
|
|
50
50
|
/******/
|
|
51
|
-
/******/return s[t](r,r.exports,
|
|
51
|
+
/******/return s[t](r,r.exports,c),r.exports;
|
|
52
52
|
/******/}
|
|
53
53
|
/******/
|
|
54
54
|
/************************************************************************/var l={};
|
|
55
|
-
/******/return(()=>{var t=l;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(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function i(){i=function(){return r};var t,r={},n=Object.prototype,o=n.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},
|
|
55
|
+
/******/return(()=>{var t=l;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(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function i(){i=function(){return r};var t,r={},n=Object.prototype,o=n.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},u=s.iterator||"@@iterator",c=s.asyncIterator||"@@asyncIterator",l=s.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var i=e&&e.prototype instanceof m?e:m,o=Object.create(i.prototype),s=new A(n||[]);return a(o,"_invoke",{value:M(t,r,s)}),o}function d(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=h;var b="suspendedStart",v="suspendedYield",p="executing",g="completed",y={};function m(){}function S(){}function w(){}var j={};f(j,u,(function(){return this}));var O=Object.getPrototypeOf,k=O&&O(O(P([])));k&&k!==n&&o.call(k,u)&&(j=k);var x=w.prototype=m.prototype=Object.create(j);function C(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,r){function n(i,a,s,u){var c=d(t[i],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,s,u)}),(function(t){n("throw",t,s,u)})):r.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,u)}))}u(c.arg)}var i;a(this,"_invoke",{value:function(t,e){function o(){return new r((function(r,i){n(t,e,r,i)}))}return i=i?i.then(o,o):o()}})}function M(e,r,n){var i=b;return function(o,a){if(i===p)throw Error("Generator is already running");if(i===g){if("throw"===o)throw a;return{value:t,done:!0}}for(n.method=o,n.arg=a;;){var s=n.delegate;if(s){var u=L(s,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(i===b)throw i=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);i=p;var c=d(e,r,n);if("normal"===c.type){if(i=n.done?g:v,c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(i=g,n.method="throw",n.arg=c.arg)}}}function L(e,r){var n=r.method,i=e.iterator[n];if(i===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,L(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var o=d(i,e.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,y;var a=o.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function _(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function q(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function P(r){if(r||""===r){var n=r[u];if(n)return n.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var i=-1,a=function e(){for(;++i<r.length;)if(o.call(r,i))return e.value=r[i],e.done=!1,e;return e.value=t,e.done=!0,e};return a.next=a}}throw new TypeError(e(r)+" is not iterable")}return S.prototype=w,a(x,"constructor",{value:w,configurable:!0}),a(w,"constructor",{value:S,configurable:!0}),S.displayName=f(w,l,"GeneratorFunction"),r.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===S||"GeneratorFunction"===(e.displayName||e.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,w):(t.__proto__=w,f(t,l,"GeneratorFunction")),t.prototype=Object.create(x),t},r.awrap=function(t){return{__await:t}},C(E.prototype),f(E.prototype,c,(function(){return this})),r.AsyncIterator=E,r.async=function(t,e,n,i,o){void 0===o&&(o=Promise);var a=new E(h(t,e,n,i),o);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},C(x),f(x,l,"Generator"),f(x,u,(function(){return this})),f(x,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=P,A.prototype={constructor:A,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(q),!e)for(var r in this)"t"===r.charAt(0)&&o.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,i){return s.type="throw",s.arg=e,r.next=n,i&&(r.method="next",r.arg=t),!!i}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),c=o.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),q(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;q(r)}return i}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:P(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},r}function o(t){var r=function(t){if("object"!=e(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=e(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==e(r)?r:r+""}Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var a,s=c(155),u=c(506),f=c(773),h=c(487),d=c(673),b=c(361),v=c(78),p=(a=c(811))&&a.__esModule?a:{default:a},g=function(){return t=function t(e){var n,a,c=this,l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{metadata:{}};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.actionsConfig=null,this.callbacks=null,
|
|
56
56
|
/**
|
|
57
57
|
* @description If the actionsConfig is defined, this will be a map of actions that can be used to modify or interact with the state
|
|
58
58
|
* */
|
|
@@ -61,12 +61,12 @@ this.actions=null,this.subscribers=new Set,
|
|
|
61
61
|
* @description
|
|
62
62
|
* Initializes the global store, setting up the main hook and actions map if applicable,
|
|
63
63
|
*/
|
|
64
|
-
this.initialize=function(){return t=
|
|
64
|
+
this.initialize=function(){return t=c,e=void 0,r=i().mark((function t(){var e,r,n,o,a;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Object.keys(null!==(e=this.actionsConfig)&&void 0!==e?e:{}).length>0&&(this.actions=this.getStoreActionsMap()),this.use=this.getMainHook(),this.configurationCallbackParam=this.getConfigCallbackParam(),n=this.onInit,o=null!==(r=this.callbacks)&&void 0!==r?r:{},a=o.onInit,n||a){t.next=8;break}return t.abrupt("return");case 8:null==n||n(this.configurationCallbackParam),(0,f.isNil)(a)||null==a||a(this.configurationCallbackParam);case 10:case"end":return t.stop()}}),t,this)})),new(e||(e=Promise))((function(n,i){function o(t){try{s(r.next(t))}catch(t){i(t)}}function a(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var r;t.done?n(t.value):(r=t.value,r instanceof e?r:new e((function(t){t(r)}))).then(o,a)}s((r=r.apply(t,[])).next())}));var t,e,r},
|
|
65
65
|
/**
|
|
66
66
|
* set the state for a single subscriber
|
|
67
67
|
* validate if the state should be updated by comparing the previous state and the new state
|
|
68
68
|
*/
|
|
69
|
-
this.executeSetStateForSubscriber=function(t,e){var r,n,i,o=t.selector,a=t.callback,s=t.currentState,
|
|
69
|
+
this.executeSetStateForSubscriber=function(t,e){var r,n,i,o=t.selector,a=t.callback,s=t.currentState,u=t.getConfig,l=null!==(r=null==u?void 0:u())&&void 0!==r?r:{};if(!e.forceUpdate&&(null!==(n=null==l?void 0:l.isEqualRoot)&&void 0!==n?n:function(t,e){return t===e})(e.currentState,e.newState))return{didUpdate:!1};var f=o?o(e.newState):e.newState;return!e.forceUpdate&&(null!==(i=null==l?void 0:l.isEqual)&&void 0!==i?i:function(t,e){return t===e})(s,f)?{didUpdate:!1}:(c.partialUpdateSubscription(t,{currentState:f}),a({state:f},{identifier:e.identifier}),{didUpdate:!0})},
|
|
70
70
|
/**
|
|
71
71
|
* set the state and update all the subscribers
|
|
72
72
|
* @param {State} newState - The new state to set
|
|
@@ -74,38 +74,35 @@ this.executeSetStateForSubscriber=function(t,e){var r,n,i,o=t.selector,a=t.callb
|
|
|
74
74
|
* @param {boolean} [options.forceUpdate] - Whether to force the update even if the state is the same
|
|
75
75
|
* @param {string} [options.identifier] - An optional identifier for the state change
|
|
76
76
|
* */
|
|
77
|
-
this.setSubscribersState=function(t,e){var n=e.forceUpdate,i=e.identifier,o=
|
|
77
|
+
this.setSubscribersState=function(t,e){var n=e.forceUpdate,i=e.identifier,o=c.state;if(n||o!==t){c.state=t;var a,s={forceUpdate:n,newState:t,currentState:o,identifier:i},u=function(t){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=r(t))){e&&(t=e);var n=0,i=function(){};return{s:i,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==e.return||e.return()}finally{if(s)throw o}}}}(c.subscribers.values());try{for(u.s();!(a=u.n()).done;){var l=a.value;c.executeSetStateForSubscriber(l,s)}}catch(t){u.e(t)}finally{u.f()}}},
|
|
78
78
|
/**
|
|
79
79
|
* Set the value of the metadata property, this is no reactive and will not trigger a re-render
|
|
80
80
|
* @param {MetadataSetter<Metadata>} setter - The setter function or the value to set
|
|
81
81
|
* */
|
|
82
|
-
this.setMetadata=function(t){var e=(0,
|
|
82
|
+
this.setMetadata=function(t){var e=(0,u.isFunction)(t)?t(c.metadata):t;c.metadata=e},
|
|
83
83
|
/**
|
|
84
84
|
* Returns the metadata [non-reactive additional information associated with the global state]
|
|
85
85
|
*/
|
|
86
|
-
this.getMetadata=function(){return
|
|
86
|
+
this.getMetadata=function(){return c.metadata},
|
|
87
87
|
/**
|
|
88
88
|
* Get the current value of the state
|
|
89
89
|
*/
|
|
90
|
-
this.getState=function(){return
|
|
90
|
+
this.getState=function(){return c.state},
|
|
91
91
|
/**
|
|
92
92
|
* get the parameters object to pass to the callback functions:
|
|
93
93
|
* onInit, onStateChanged, onSubscribed, computePreventStateChange
|
|
94
94
|
* */
|
|
95
|
-
this.getConfigCallbackParam=function(){var t=
|
|
95
|
+
this.getConfigCallbackParam=function(){var t=c.setMetadata,e=c.getMetadata,r=c.getState,n=c.subscribe,i=c.actions;return{setMetadata:t,getMetadata:e,getState:r,subscribe:n,setState:c.setState,actions:i}},this.subscribeCallback=function(t){return c.executeOnSubscribed(t),c.subscribers.add(t),function(){c.subscribers.delete(t)}},this.partialUpdateSubscription=function(t,e){(0,h.isRecord)(t)&&Object.assign(t,e)},this.executeOnSubscribed=function(t){var e,r=c.onSubscribed,n=null===(e=c.callbacks)||void 0===e?void 0:e.onSubscribed;(r||n)&&(null==r||r(c.configurationCallbackParam,t),null==n||n(c.configurationCallbackParam,t))},
|
|
96
96
|
/**
|
|
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,
|
|
101
|
-
/**
|
|
102
|
-
* Extended properties and methods of the hook
|
|
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),
|
|
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,u.isFunction)(e)?e(t):t},e={currentState:t(c.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,c.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),[c.computeSelectedState({subscription:h,currentDependencies:i}),c.getStateOrchestrator(),c.metadata]},e={setMetadata:c.setMetadata,getMetadata:c.getMetadata,actions:c.actions,setState:c.actions?null:c.setState.bind(c),getState:c.getState.bind(c),subscribe:c.subscribe.bind(c),dispose:c.dispose.bind(c),createSelectorHook:c.createSelectorHook.bind(c),createObservable:c.createObservable.bind(c)};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)||c.partialUpdateSubscription(r,{currentState:r.selector(c.state)}),r.currentState},this.createSelectorHook=m,this.createObservable=y,
|
|
104
101
|
/**
|
|
105
102
|
* Returns the state setter or the actions map
|
|
106
103
|
* @returns {StateMutator} - The state setter or the actions map
|
|
107
104
|
* */
|
|
108
|
-
this.getStateOrchestrator=function(){return
|
|
105
|
+
this.getStateOrchestrator=function(){return c.actions?c.actions:c.setState},
|
|
109
106
|
/**
|
|
110
107
|
* This is the only setState function that should be exposed outside the class
|
|
111
108
|
* This is responsible for defining whenever or not the state change should be allowed or prevented
|
|
@@ -113,16 +110,16 @@ this.getStateOrchestrator=function(){return u.actions?u.actions:u.setState},
|
|
|
113
110
|
* - onStateChanged (if defined) - this function is executed after the state change
|
|
114
111
|
* - 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
112
|
*/
|
|
116
|
-
this.setState=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=n.forceUpdate,o=n.identifier,a=
|
|
113
|
+
this.setState=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=n.forceUpdate,o=n.identifier,a=c.state,s=(0,u.isFunction)(t)?t(a):t;if(i||c.state!==s){var l=c.setMetadata,f=c.getMetadata,h=c.getState,d=c.actions,b={setMetadata:l,getMetadata:f,setState:c.setSubscribersState,getState:h,actions:d,previousState:a,state:s,identifier:o},v=c.computePreventStateChange,p=null===(e=c.callbacks)||void 0===e?void 0:e.computePreventStateChange;if((v||p)&&((null==v?void 0:v(b))||(null==p?void 0:p(b))))return;c.setSubscribersState(s,{forceUpdate:i,identifier:o});var g=c.onStateChanged,y=null===(r=c.callbacks)||void 0===r?void 0:r.onStateChanged;(g||y)&&(null==g||g(b),null==y||y(b))}},
|
|
117
114
|
/**
|
|
118
115
|
* This creates a map of actions that can be used to modify or interact with the state
|
|
119
116
|
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
120
117
|
* */
|
|
121
|
-
this.getStoreActionsMap=function(){if(!(0,h.isRecord)(
|
|
118
|
+
this.getStoreActionsMap=function(){if(!(0,h.isRecord)(c.actionsConfig))return null;var t=c.actionsConfig,e=c.setMetadata,r=c.setState,n=c.getState,i=c.getMetadata,a=Object.keys(t).reduce((function(s,u){var c,l,f;return Object.assign(s,(c={},f=function(){for(var o=t[u],s=arguments.length,c=new Array(s),l=0;l<s;l++)c[l]=arguments[l];var f=o.apply(a,c);return"function"!=typeof f&&(0,b.throwWrongKeyOnActionCollectionConfig)(u),f.call(a,{setState:r,getState:n,setMetadata:e,getMetadata:i,actions:a})},(l=o(l=u))in c?Object.defineProperty(c,l,{value:f,enumerable:!0,configurable:!0,writable:!0}):c[l]=f,c)),s}),{});return a},this.removeSubscriptions=function(){c.subscribers.clear()},this.dispose=function(){c.removeSubscriptions(),c._name="",c.actionsConfig=null,c.callbacks=null,c.metadata={},c.actions=null,c.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],c=(0,u.isFunction)(a),l=c?o:void 0,f=c?a:o,h=null!==(i=c?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:")}),u=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}),c=this.actions?null:this.setState,l=s.subscribe.bind(s),f={setMetadata:this.setMetadata,getMetadata:this.getMetadata,actions:this.actions,setState:c,getState:s.getState.bind(s),subscribe:s.subscribe.bind(s),createSelectorHook:m.bind(l),createObservable:y.bind(l),dispose:function(){u(),s.dispose()}};return Object.assign(l,f),l}
|
|
122
119
|
/**
|
|
123
120
|
* @description
|
|
124
121
|
* Creates a derived hook bound to a selected fragment of the root state.
|
|
125
122
|
* The derived hook re-renders only when the selected value changes and
|
|
126
123
|
* 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,
|
|
124
|
+
*/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,u=this.getState(),c=(null!=t?t:function(t){return t})(u),l=new g(c,{name:null!=o?o:(0,v.uniqueId)("sh:")}),f=this.subscribe((function(e){if(!(null!=a?a:Object.is)(u,e)){u=e;var r=t(e);(null!=s?s:Object.is)(c,r)||(c=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,u=!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){u=!0,n=t}finally{try{if(!s&&null!=e.return&&(o=e.return(),Object(o)!==o))return}finally{if(u)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,getMetadata:this.getMetadata,actions:this.actions,setState:d,getState:function(){return l.getState()},subscribe:l.subscribe.bind(l),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
125
|
/******/})(),"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
|