react-hooks-global-states 15.0.3 → 15.0.5
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 +15 -8
- package/GlobalStore.js +1 -1
- package/bundle.js +1 -1
- package/createContext.d.ts +4 -3
- package/createContext.js +1 -1
- package/createGlobalState.d.ts +26 -2
- package/index.d.ts +1 -2
- package/package.json +1 -1
- package/types.d.ts +11 -2
- package/uniqueId.d.ts +30 -1
- package/uniqueId.js +1 -1
- package/webpack.config.js +0 -1
- package/GlobalStoreAbstract.d.ts +0 -15
- package/GlobalStoreAbstract.js +0 -1
package/GlobalStore.d.ts
CHANGED
|
@@ -30,10 +30,14 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
30
30
|
actions?: ActionsConfig;
|
|
31
31
|
name?: string;
|
|
32
32
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
protected
|
|
33
|
+
/**
|
|
34
|
+
* This method is meant to be overridden by the extended classes
|
|
35
|
+
*/
|
|
36
|
+
protected onInit?: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* This method is meant to be overridden by the extended classes
|
|
39
|
+
*/
|
|
40
|
+
protected onStateChanged?: (args: StateChanges<State>) => void;
|
|
37
41
|
/**
|
|
38
42
|
* @description
|
|
39
43
|
* Initializes the global store, setting up the main hook and actions map if applicable,
|
|
@@ -58,7 +62,7 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
58
62
|
* @param {boolean} [options.forceUpdate] - Whether to force the update even if the state is the same
|
|
59
63
|
* @param {string} [options.identifier] - An optional identifier for the state change
|
|
60
64
|
* */
|
|
61
|
-
|
|
65
|
+
setActualStateWithoutValidations: (newState: State, { forceUpdate, identifier }: {
|
|
62
66
|
forceUpdate?: boolean;
|
|
63
67
|
identifier?: string;
|
|
64
68
|
}) => void;
|
|
@@ -84,7 +88,6 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
84
88
|
*/
|
|
85
89
|
protected subscribeCallback: (subscription: SubscriberParameters) => () => void;
|
|
86
90
|
partialUpdateSubscription: (subscription: SubscriberParameters, values: Partial<SubscriberParameters>) => void;
|
|
87
|
-
protected executeOnSubscribed: (subscription: SubscriberParameters) => void;
|
|
88
91
|
/**
|
|
89
92
|
* Returns a custom hook that allows to handle a global state
|
|
90
93
|
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
|
|
@@ -114,8 +117,7 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
114
117
|
identifier?: string;
|
|
115
118
|
}) => void;
|
|
116
119
|
/**
|
|
117
|
-
* This creates
|
|
118
|
-
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
120
|
+
* This creates storeTools and actions map if applicable
|
|
119
121
|
* */
|
|
120
122
|
getStoreActionsMap: () => {
|
|
121
123
|
actions: PublicStateMutator extends AnyFunction ? null : PublicStateMutator;
|
|
@@ -123,6 +125,11 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
123
125
|
};
|
|
124
126
|
protected removeSubscriptions: () => void;
|
|
125
127
|
dispose: () => void;
|
|
128
|
+
__devtools_getLifeCycleStoreToolsWrapper?: (logsPrefix: string) => StoreTools<State, PublicStateMutator, Metadata>;
|
|
129
|
+
__devtools_initialize_getStoreActionsMapWrapped?: () => {
|
|
130
|
+
actions: PublicStateMutator extends AnyFunction ? null : PublicStateMutator;
|
|
131
|
+
storeTools: StoreTools<State, PublicStateMutator, Metadata>;
|
|
132
|
+
};
|
|
126
133
|
}
|
|
127
134
|
export declare function createObservable<RootState, PublicStateMutator, Metadata extends BaseMetadata, Selected>(this: Pick<ReadonlyStateApi<RootState, PublicStateMutator, Metadata>, 'getState' | 'subscribe'>, selector: (state: RootState) => Selected, options?: {
|
|
128
135
|
isEqual?: (current: Selected, next: Selected) => boolean;
|
package/GlobalStore.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e;t=this,e=(t,e,r,n,o,i,a)=>(()=>{"use strict";var s={78:e=>{e.exports=t},155:t=>{t.exports=e},361:t=>{t.exports=r},487:t=>{t.exports=n},506:t=>{t.exports=o},673:t=>{t.exports=i},773:t=>{t.exports=a},811:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});var r=globalThis;r.isDevToolsPresent=Boolean(r.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=r}},u={};function c(t){var e=u[t];if(void 0!==e)return e.exports;var r=u[t]={exports:{}};return s[t](r,r.exports,c),r.exports}var l={};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 o(){o=function(){return r};var t,r={},n=Object.prototype,i=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 d(t,e,r,n){var o=e&&e.prototype instanceof m?e:m,i=Object.create(o.prototype),s=new T(n||[]);return a(i,"_invoke",{value:C(t,r,s)}),i}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=d;var b="suspendedStart",p="suspendedYield",v="executing",g="completed",y={};function m(){}function S(){}function w(){}var j={};f(j,u,(function(){return this}));var O=Object.getPrototypeOf,x=O&&O(O(q([])));x&&x!==n&&i.call(x,u)&&(j=x);var E=w.prototype=m.prototype=Object.create(j);function k(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function _(t,r){function n(o,a,s,u){var c=h(t[o],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==e(f)&&i.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 o;a(this,"_invoke",{value:function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}})}function C(e,r,n){var o=b;return function(i,a){if(o===v)throw Error("Generator is already running");if(o===g){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,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(o===b)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var c=h(e,r,n);if("normal"===c.type){if(o=n.done?g:p,c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=g,n.method="throw",n.arg=c.arg)}}}function L(e,r){var n=r.method,o=e.iterator[n];if(o===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 i=h(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.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 A(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 M(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function T(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function q(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 o=-1,a=function e(){for(;++o<r.length;)if(i.call(r,o))return e.value=r[o],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(E,"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(E),t},r.awrap=function(t){return{__await:t}},k(_.prototype),f(_.prototype,c,(function(){return this})),r.AsyncIterator=_,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new _(d(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},k(E),f(E,l,"Generator"),f(E,u,(function(){return this})),f(E,"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=q,T.prototype={constructor:T,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(M),!e)for(var r in this)"t"===r.charAt(0)&&i.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,o){return s.type="throw",s.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=i.call(a,"catchLoc"),c=i.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&&i.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.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),M(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 o=n.arg;M(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:q(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},r}function i(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,s(n.key),n)}}function a(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(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+""}var u,f=Object.create?function(t,e,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(e,r);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,o)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]},d=Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e},h=(u=function(t){return u=Object.getOwnPropertyNames||function(t){var e=[];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[e.length]=r);return e},u(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r=u(t),n=0;n<r.length;n++)"default"!==r[n]&&f(e,t,r[n]);return d(e,t),e}),b=function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0,t.createObservable=x,t.createSelectorHook=E;var p=c(155),v=b(c(506)),g=b(c(773)),y=b(c(487)),m=h(c(673)),S=b(c(361)),w=b(c(78)),j=b(c(811)),O=a((function t(e){var n,i,a=this,u=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,this.actions=null,this.subscribers=new Set,this.initialize=function(){return t=a,e=void 0,r=o().mark((function t(){var e,r,n,i,a,s,u;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=this.getStoreActionsMap(),n=r.actions,i=r.storeTools,this.actions=n,this.storeTools=i,this.use=this.getMainHook(),a=this.onInit,s=null!==(e=this.callbacks)&&void 0!==e?e:{},u=s.onInit,a||u){t.next=8;break}return t.abrupt("return");case 8:null==a||a(i),(0,g.default)(u)||null==u||u(i);case 10:case"end":return t.stop()}}),t,this)})),new(e||(e=Promise))((function(n,o){function i(t){try{s(r.next(t))}catch(t){o(t)}}function a(t){try{s(r.throw(t))}catch(t){o(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(i,a)}s((r=r.apply(t,[])).next())}));var t,e,r},this.executeSetStateForSubscriber=function(t,e){var r=t.selector,n=t.onStoreChange,o=t.currentState,i=t.isEqualRoot,s=void 0===i?function(t,e){return t===e}:i,u=t.isEqual,c=void 0===u?function(t,e){return t===e}:u;if(!e.forceUpdate&&s(e.currentState,e.newState))return{didUpdate:!1};var l=r?r(e.newState):e.newState;return!e.forceUpdate&&c(o,l)?{didUpdate:!1}:(a.partialUpdateSubscription(t,{currentState:l}),n({state:l},{identifier:e.identifier}),{didUpdate:!0})},this.setActualStateWithoutValidations=function(t,e){var n=e.forceUpdate,o=e.identifier,i=a.state;if(n||i!==t){a.state=t;var s,u={forceUpdate:n,newState:t,currentState:i,identifier:o},c=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,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}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 i,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,i=t},f:function(){try{a||null==e.return||e.return()}finally{if(s)throw i}}}}(a.subscribers.values());try{for(c.s();!(s=c.n()).done;){var l=s.value;a.executeSetStateForSubscriber(l,u)}}catch(t){c.e(t)}finally{c.f()}}},this.setMetadata=function(t){var e=(0,v.default)(t)?t(a.metadata):t;a.metadata=e},this.getMetadata=function(){return a.metadata},this.getState=function(){return a.state},this.subscribe=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n,o=e[0],i=e[1],s=e[2],u=(0,v.default)(i),c=u?o:void 0,l=u?i:o,f=null!==(n=u?s:i)&&void 0!==n?n:void 0,d=c?c(a.state):a.state;(null==f?void 0:f.skipFirst)||l(d);var h=Object.assign({selector:c,currentState:d,onStoreChange:function(t){var e=t.state;return l(e)}},f);return a.subscribeCallback(h),function(){a.subscribers.delete(h)}},this.subscribeCallback=function(t){return a.executeOnSubscribed(t),a.subscribers.add(t),function(){a.subscribers.delete(t)}},this.partialUpdateSubscription=function(t,e){Object.assign(t,e)},this.executeOnSubscribed=function(t){var e,r=a.onSubscribed,n=null===(e=a.callbacks)||void 0===e?void 0:e.onSubscribed;(r||n)&&(null==r||r(a.storeTools,t),null==n||n(a.storeTools,t))},this.getMainHook=function(){var t="globalHook:".concat(a._name),e=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];(0,p.useDebugValue)(t);var n=(0,m.isArray)(r)?{dependencies:r}:null!=r?r:{},o=(0,p.useRef)(null),i=function(t){return(0,v.default)(e)?e(t):t};o.current=o.current?o.current:Object.assign({selector:i,currentState:i(a.state),onStoreChange:function(){throw new Error("Callback not set")}},n);var s=o.current.dependencies,u=null==n?void 0:n.dependencies,c=Object.assign(Object.assign({},n),{selector:i,dependencies:u});a.partialUpdateSubscription(o.current,c);var l=(0,p.useMemo)((function(){var t=function(){return o.current.currentState};return{subscribe:function(t){return o.current.onStoreChange=t,a.subscribeCallback(o.current)},getSnapshot:t,getServerSnapshot:t}}),[]),f=l.subscribe,d=l.getSnapshot,h=l.getServerSnapshot;return a.reselectIfDependenciesChanged({subscription:o.current,newDependencies:u,currentDependencies:s}),[(0,p.useSyncExternalStore)(f,d,h),a.getStateOrchestrator(),a.metadata]},r=a.setMetadata,n=a.getMetadata,o=a.actions,i=a.actions?null:a.setState.bind(a),s=a,u={actions:o,createObservable:a.createObservable.bind(s),createSelectorHook:a.createSelectorHook.bind(s),dispose:a.dispose.bind(a),getMetadata:n,getState:a.getState.bind(a),setMetadata:r,setState:i,subscribe:a.subscribe.bind(a),subscribers:a.subscribers,use:e,select:function(){return e.apply(void 0,arguments)[0]}};return Object.assign(e,u),e},this.reselectIfDependenciesChanged=function(t){var e=t.subscription,r=t.newDependencies,n=t.currentDependencies;e.selector&&n!==r&&((null==n?void 0:n.length)===(null==r?void 0:r.length)&&(0,m.default)(n,r)||a.partialUpdateSubscription(e,{currentState:e.selector(a.state)}))},this.createSelectorHook=E,this.createObservable=x,this.getStateOrchestrator=function(){return a.actions?a.actions:a.setState},this.setState=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.forceUpdate,i=n.identifier,s=a.state,u=(0,v.default)(t)?t(s):t;if(o||a.state!==u){var c=a.setMetadata,l=a.getMetadata,f=a.getState,d=a.actions,h={setMetadata:c,getMetadata:l,setState:a.setActualStateWithoutValidations,getState:f,actions:d,previousState:s,state:u,identifier:i},b=a.computePreventStateChange,p=null===(e=a.callbacks)||void 0===e?void 0:e.computePreventStateChange;if((b||p)&&((null==b?void 0:b(h))||(null==p?void 0:p(h))))return;a.setActualStateWithoutValidations(u,{forceUpdate:o,identifier:i});var g=a.onStateChanged,y=null===(r=a.callbacks)||void 0===r?void 0:r.onStateChanged;null==g||g(h),null==y||y(h)}},this.getStoreActionsMap=function(){var t=a.actionsConfig,e=a.getMetadata,r=a.getState,n={setMetadata:a.setMetadata,getMetadata:e,getState:r,setState:a.setState,subscribe:a.subscribe,actions:null};if(!(0,y.default)(t))return{actions:null,storeTools:n};var o={};n.actions=o;for(var i=Object.keys(t),u=function(){var e,r,i,a=l[c];Object.assign(o,(e={},i=function(){for(var e=t[a],r=arguments.length,i=new Array(r),s=0;s<r;s++)i[s]=arguments[s];var u=e.apply(o,i);return"function"!=typeof u&&(0,S.default)(a),u.call(o,n)},(r=s(r=a))in e?Object.defineProperty(e,r,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[r]=i,e))},c=0,l=i;c<l.length;c++)u();return{actions:o,storeTools:n}},this.removeSubscriptions=function(){a.subscribers.clear()},this.dispose=function(){a.removeSubscriptions()};var c=u.metadata,l=u.callbacks,f=u.actions,d=u.name;if(this.state=e,this._name=null!=d?d:(0,w.default)("gs:"),this.metadata=null!=c?c:{},this.callbacks=null!=l?l:null,this.actionsConfig=null!=f?f:null,j.default.isDevToolsPresent){var h=null!==(n=(new Error).stack)&&void 0!==n?n:"";null===(i=j.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(j.default,this,u,h)}this.constructor!==t||this.initialize()}));function x(t,e){var r=null==e?void 0:e.name,n=null==e?void 0:e.isEqualRoot,o=null==e?void 0:e.isEqual,i=this.getState(),a=(null!=t?t:function(t){return t})(i),s=new O(a,{name:null!=r?r:(0,w.default)("sh:")}),u=this.subscribe((function(e){if(!(null!=n?n:Object.is)(i,e)){i=e;var r=t(e);(null!=o?o:Object.is)(a,r)||(a=r,s.setState(r))}}),{skipFirst:!0}),c=s.subscribe.bind(s),l={getState:s.getState.bind(s),subscribe:s.subscribe.bind(s),createSelectorHook:E.bind(c),createObservable:x.bind(c),dispose:function(){u(),s.dispose()},subscribers:s.subscribers};return Object.assign(c,l),c}function E(t,e){var n=null==e?void 0:e.name,o=null==e?void 0:e.isEqualRoot,i=null==e?void 0:e.isEqual,a=this.getState(),s=(null!=t?t:function(t){return t})(a),u=new O(s,{name:null!=n?n:(0,w.default)("sh:")}),c=this.subscribe((function(e){if(!(null!=o?o:Object.is)(a,e)){a=e;var r=t(e);(null!=i?i:Object.is)(s,r)||(s=r,u.setState(r))}}),{skipFirst:!0}),l=function(){return(t=u.use.apply(u,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,o,i,a=[],s=!0,u=!1;try{for(o=(e=e.call(t)).next;!(s=(r=o.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&&(i=e.return(),Object(i)!==i))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];var t},f={getState:function(){return u.getState()},subscribe:u.subscribe.bind(u),createSelectorHook:E.bind(l),createObservable:x.bind(l),dispose:function(){c(),u.dispose()},subscribers:u.subscribers};return Object.assign(l,f),l}t.GlobalStore=O,t.default=O})(),l})(),"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"]);
|
|
1
|
+
var t,e;t=this,e=(t,e,r,n,o,i)=>(()=>{"use strict";var a={78:e=>{e.exports=t},155:t=>{t.exports=e},361:t=>{t.exports=r},487:t=>{t.exports=n},506:t=>{t.exports=o},673:t=>{t.exports=i},811:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});var r=globalThis;r.isDevToolsPresent=Boolean(r.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=r}},s={};function u(t){var e=s[t];if(void 0!==e)return e.exports;var r=s[t]={exports:{}};return a[t](r,r.exports,u),r.exports}var c={};return(()=>{var t=c;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 o(){o=function(){return r};var t,r={},n=Object.prototype,i=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 d(t,e,r,n){var o=e&&e.prototype instanceof m?e:m,i=Object.create(o.prototype),s=new P(n||[]);return a(i,"_invoke",{value:C(t,r,s)}),i}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=d;var v="suspendedStart",p="suspendedYield",b="executing",y="completed",g={};function m(){}function S(){}function w(){}var j={};f(j,u,(function(){return this}));var O=Object.getPrototypeOf,x=O&&O(O(q([])));x&&x!==n&&i.call(x,u)&&(j=x);var E=w.prototype=m.prototype=Object.create(j);function _(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function k(t,r){function n(o,a,s,u){var c=h(t[o],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==e(f)&&i.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 o;a(this,"_invoke",{value:function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}})}function C(e,r,n){var o=v;return function(i,a){if(o===b)throw Error("Generator is already running");if(o===y){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var s=n.delegate;if(s){var u=L(s,n);if(u){if(u===g)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===v)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=b;var c=h(e,r,n);if("normal"===c.type){if(o=n.done?y:p,c.arg===g)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=y,n.method="throw",n.arg=c.arg)}}}function L(e,r){var n=r.method,o=e.iterator[n];if(o===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")),g;var i=h(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,g;var a=i.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,g):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,g)}function A(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 T(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function q(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 o=-1,a=function e(){for(;++o<r.length;)if(i.call(r,o))return e.value=r[o],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(E,"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(E),t},r.awrap=function(t){return{__await:t}},_(k.prototype),f(k.prototype,c,(function(){return this})),r.AsyncIterator=k,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new k(d(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},_(E),f(E,l,"Generator"),f(E,u,(function(){return this})),f(E,"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=q,P.prototype={constructor:P,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(T),!e)for(var r in this)"t"===r.charAt(0)&&i.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,o){return s.type="throw",s.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=i.call(a,"catchLoc"),c=i.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&&i.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,g):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),g},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),T(r),g}},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 o=n.arg;T(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:q(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),g}},r}function i(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,s(n.key),n)}}function a(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(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+""}var l,f=Object.create?function(t,e,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(e,r);o&&!("get"in o?!e.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return e[r]}}),Object.defineProperty(t,n,o)}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]},d=Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e},h=(l=function(t){return l=Object.getOwnPropertyNames||function(t){var e=[];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[e.length]=r);return e},l(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r=l(t),n=0;n<r.length;n++)"default"!==r[n]&&f(e,t,r[n]);return d(e,t),e}),v=function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0,t.createObservable=O,t.createSelectorHook=x;var p=u(155),b=v(u(506)),y=v(u(487)),g=h(u(673)),m=v(u(361)),S=v(u(78)),w=v(u(811)),j=a((function t(e){var n,i,a=this,u=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,this.actions=null,this.subscribers=new Set,this.initialize=function(){return t=a,e=void 0,r=o().mark((function t(){var e,r,n,i,a,s,u,c,l,f,d,h;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(u=null!==(r=null===(e=this.__devtools_initialize_getStoreActionsMapWrapped)||void 0===e?void 0:e.call(this))&&void 0!==r?r:this.getStoreActionsMap(),c=u.actions,l=u.storeTools,this.actions=c,this.storeTools=l,this.use=this.getMainHook(),null===(n=this.onInit)||void 0===n||n.call(this),f=null!==(i=this.callbacks)&&void 0!==i?i:{},d=f.onInit){t.next=9;break}return t.abrupt("return");case 9:h=null!==(s=null===(a=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===a?void 0:a.call(this,"config/onInit/"))&&void 0!==s?s:l,null==d||d(h);case 11:case"end":return t.stop()}}),t,this)})),new(e||(e=Promise))((function(n,o){function i(t){try{s(r.next(t))}catch(t){o(t)}}function a(t){try{s(r.throw(t))}catch(t){o(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(i,a)}s((r=r.apply(t,[])).next())}));var t,e,r},this.executeSetStateForSubscriber=function(t,e){var r=t.selector,n=t.onStoreChange,o=t.currentState,i=t.isEqualRoot,s=void 0===i?function(t,e){return t===e}:i,u=t.isEqual,c=void 0===u?function(t,e){return t===e}:u;if(!e.forceUpdate&&s(e.currentState,e.newState))return{didUpdate:!1};var l=r?r(e.newState):e.newState;return!e.forceUpdate&&c(o,l)?{didUpdate:!1}:(a.partialUpdateSubscription(t,{currentState:l}),n({state:l},{identifier:e.identifier}),{didUpdate:!0})},this.setActualStateWithoutValidations=function(t,e){var n=e.forceUpdate,o=e.identifier,i=a.state;if(n||i!==t){a.state=t;var s,u={forceUpdate:n,newState:t,currentState:i,identifier:o},c=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,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}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 i,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,i=t},f:function(){try{a||null==e.return||e.return()}finally{if(s)throw i}}}}(a.subscribers.values());try{for(c.s();!(s=c.n()).done;){var l=s.value;a.executeSetStateForSubscriber(l,u)}}catch(t){c.e(t)}finally{c.f()}}},this.setMetadata=function(t){var e=(0,b.default)(t)?t(a.metadata):t;a.metadata=e},this.getMetadata=function(){return a.metadata},this.getState=function(){return a.state},this.subscribe=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var n,o=e[0],i=e[1],s=e[2],u=(0,b.default)(i),c=u?o:void 0,l=u?i:o,f=null!==(n=u?s:i)&&void 0!==n?n:void 0,d=c?c(a.state):a.state;(null==f?void 0:f.skipFirst)||l(d);var h=Object.assign({selector:c,currentState:d,onStoreChange:function(t){var e=t.state;return l(e)}},f);return a.subscribeCallback(h),function(){a.subscribers.delete(h)}},this.subscribeCallback=function(t){var e,r;return null===(r=null===(e=a.callbacks)||void 0===e?void 0:e.onSubscribed)||void 0===r||r.call(e,a.storeTools,t),a.subscribers.add(t),function(){a.subscribers.delete(t)}},this.partialUpdateSubscription=function(t,e){Object.assign(t,e)},this.getMainHook=function(){var t="globalHook:".concat(a._name),e=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];(0,p.useDebugValue)(t);var n=(0,g.isArray)(r)?{dependencies:r}:null!=r?r:{},o=(0,p.useRef)(null),i=function(t){return(0,b.default)(e)?e(t):t};o.current=o.current?o.current:Object.assign({selector:i,currentState:i(a.state),onStoreChange:function(){throw new Error("Callback not set")}},n);var s=o.current.dependencies,u=null==n?void 0:n.dependencies,c=Object.assign(Object.assign({},n),{selector:i,dependencies:u});a.partialUpdateSubscription(o.current,c);var l=(0,p.useMemo)((function(){var t=function(){return o.current.currentState};return{subscribe:function(t){return o.current.onStoreChange=t,a.subscribeCallback(o.current)},getSnapshot:t,getServerSnapshot:t}}),[]),f=l.subscribe,d=l.getSnapshot,h=l.getServerSnapshot;return a.reselectIfDependenciesChanged({subscription:o.current,newDependencies:u,currentDependencies:s}),[(0,p.useSyncExternalStore)(f,d,h),a.getStateOrchestrator(),a.metadata]},r=a.setMetadata,n=a.getMetadata,o=a.actions,i=a.actions?null:a.setState.bind(a),s=a,u={actions:o,createObservable:a.createObservable.bind(s),createSelectorHook:a.createSelectorHook.bind(s),dispose:a.dispose.bind(a),getMetadata:n,getState:a.getState.bind(a),setMetadata:r,setState:i,subscribe:a.subscribe.bind(a),subscribers:a.subscribers,use:e,select:function(){return e.apply(void 0,arguments)[0]}};return Object.assign(e,u),e},this.reselectIfDependenciesChanged=function(t){var e=t.subscription,r=t.newDependencies,n=t.currentDependencies;e.selector&&n!==r&&((null==n?void 0:n.length)===(null==r?void 0:r.length)&&(0,g.default)(n,r)||a.partialUpdateSubscription(e,{currentState:e.selector(a.state)}))},this.createSelectorHook=x,this.createObservable=O,this.getStateOrchestrator=function(){return a.actions?a.actions:a.setState},this.setState=function(t){var e,r,n,o,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=i.forceUpdate,u=i.identifier,c=a.state,l=(0,b.default)(t)?t(c):t;if(s||a.state!==l){var f=a.setActualStateWithoutValidations,d={previousState:c,state:l,identifier:u},h=Object.assign(Object.assign(Object.assign({},a.storeTools),d),{setState:f});if((null===(e=a.callbacks)||void 0===e?void 0:e.computePreventStateChange)&&(null===(n=null===(r=a.callbacks)||void 0===r?void 0:r.computePreventStateChange)||void 0===n?void 0:n.call(r,h)))return;a.setActualStateWithoutValidations(l,{forceUpdate:s,identifier:u});var v=a.onStateChanged,p=null===(o=a.callbacks)||void 0===o?void 0:o.onStateChanged;null==v||v(h),null==p||p(h)}},this.getStoreActionsMap=function(){var t=a.actionsConfig,e=a.getMetadata,r=a.getState,n={setMetadata:a.setMetadata,getMetadata:e,getState:r,setState:a.setState,subscribe:a.subscribe,actions:null};if(!(0,y.default)(t))return{actions:null,storeTools:n};var o={};n.actions=o;for(var i=Object.keys(t),u=function(){var e,r,i,a=l[c];Object.assign(o,(e={},i=function(){for(var e=t[a],r=arguments.length,i=new Array(r),s=0;s<r;s++)i[s]=arguments[s];var u=e.apply(o,i);return"function"!=typeof u&&(0,m.default)(a),u.call(o,n)},(r=s(r=a))in e?Object.defineProperty(e,r,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[r]=i,e))},c=0,l=i;c<l.length;c++)u();return{actions:o,storeTools:n}},this.removeSubscriptions=function(){a.subscribers.clear()},this.dispose=function(){a.removeSubscriptions()};var c=u.metadata,l=u.callbacks,f=u.actions,d=u.name;if(this.state=e,this._name=null!=d?d:(0,S.default)("gs:"),this.metadata=null!=c?c:{},this.callbacks=null!=l?l:null,this.actionsConfig=null!=f?f:null,w.default.isDevToolsPresent){var h=null!==(n=(new Error).stack)&&void 0!==n?n:"";null===(i=w.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(w.default,this,u,h)}this.constructor!==t||this.initialize()}));function O(t,e){var r=null==e?void 0:e.name,n=null==e?void 0:e.isEqualRoot,o=null==e?void 0:e.isEqual,i=this.getState(),a=(null!=t?t:function(t){return t})(i),s=new j(a,{name:null!=r?r:(0,S.default)("sh:")}),u=this.subscribe((function(e){if(!(null!=n?n:Object.is)(i,e)){i=e;var r=t(e);(null!=o?o:Object.is)(a,r)||(a=r,s.setState(r))}}),{skipFirst:!0}),c=s.subscribe.bind(s),l={getState:s.getState.bind(s),subscribe:s.subscribe.bind(s),createSelectorHook:x.bind(c),createObservable:O.bind(c),dispose:function(){u(),s.dispose()},subscribers:s.subscribers};return Object.assign(c,l),c}function x(t,e){var n=null==e?void 0:e.name,o=null==e?void 0:e.isEqualRoot,i=null==e?void 0:e.isEqual,a=this.getState(),s=(null!=t?t:function(t){return t})(a),u=new j(s,{name:null!=n?n:(0,S.default)("sh:")}),c=this.subscribe((function(e){if(!(null!=o?o:Object.is)(a,e)){a=e;var r=t(e);(null!=i?i:Object.is)(s,r)||(s=r,u.setState(r))}}),{skipFirst:!0}),l=function(){return(t=u.use.apply(u,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,o,i,a=[],s=!0,u=!1;try{for(o=(e=e.call(t)).next;!(s=(r=o.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&&(i=e.return(),Object(i)!==i))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];var t},f={getState:function(){return u.getState()},subscribe:u.subscribe.bind(u),createSelectorHook:x.bind(l),createObservable:O.bind(l),dispose:function(){c(),u.dispose()},subscribers:u.subscribers};return Object.assign(l,f),l}t.GlobalStore=j,t.default=j})(),c})(),"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"]);
|
package/bundle.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,r;e=this,r=(e,r,t,o,n,a,
|
|
1
|
+
var e,r;e=this,r=(e,r,t,o,n,a,i)=>(()=>{"use strict";var l={78:r=>{r.exports=e},361:e=>{e.exports=r},487:e=>{e.exports=t},623:e=>{e.exports=o},670:e=>{e.exports=n},673:e=>{e.exports=a},778:e=>{e.exports=i}},s={};function u(e){var r=s[e];if(void 0!==r)return r.exports;var t=s[e]={exports:{}};return l[e](t,t.exports,u),t.exports}var c={};return(()=>{var e=c;Object.defineProperty(e,"__esModule",{value:!0}),e.createContext=e.isRecord=e.throwWrongKeyOnActionCollectionConfig=e.uniqueId=e.shallowCompare=e.createGlobalState=e.GlobalStore=void 0;var r=u(778);Object.defineProperty(e,"GlobalStore",{enumerable:!0,get:function(){return r.GlobalStore}});var t=u(670);Object.defineProperty(e,"createGlobalState",{enumerable:!0,get:function(){return t.createGlobalState}});var o=u(673);Object.defineProperty(e,"shallowCompare",{enumerable:!0,get:function(){return o.shallowCompare}});var n=u(78);Object.defineProperty(e,"uniqueId",{enumerable:!0,get:function(){return n.uniqueId}});var a=u(361);Object.defineProperty(e,"throwWrongKeyOnActionCollectionConfig",{enumerable:!0,get:function(){return a.throwWrongKeyOnActionCollectionConfig}});var i=u(487);Object.defineProperty(e,"isRecord",{enumerable:!0,get:function(){return i.isRecord}});var l=u(623);Object.defineProperty(e,"createContext",{enumerable:!0,get:function(){return l.createContext}})})(),c})(),"object"==typeof exports&&"object"==typeof module?module.exports=r(require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("./createContext.js"),require("./createGlobalState.js"),require("./shallowCompare.js"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","./createContext.js","./createGlobalState.js","./shallowCompare.js","./GlobalStore.js"],r):"object"==typeof exports?exports["react-hooks-global-states"]=r(require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("./createContext.js"),require("./createGlobalState.js"),require("./shallowCompare.js"),require("./GlobalStore.js")):e["react-hooks-global-states"]=r(e["./uniqueId.js"],e["./throwWrongKeyOnActionCollectionConfig.js"],e["./isRecord.js"],e["./createContext.js"],e["./createGlobalState.js"],e["./shallowCompare.js"],e["./GlobalStore.js"]);
|
package/createContext.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React, { type PropsWithChildren, Context as ReactContext } from 'react';
|
|
2
2
|
import { GlobalStore } from './GlobalStore';
|
|
3
3
|
import type { BaseMetadata, GlobalStoreCallbacks as GlobalStoreCallbacksBase, UseHookOptions, AnyFunction, StoreTools, ReadonlyStateApi, SelectHook, ObservableFragment, StateApi, UnsubscribeCallback } from './types';
|
|
4
|
+
import { AnyActions } from 'createGlobalState';
|
|
4
5
|
type GlobalStoreContextCallbacks<State, StateMutator, Metadata extends BaseMetadata> = {
|
|
5
6
|
/**
|
|
6
7
|
* @description Optional callback invoked after the context is created,
|
|
@@ -405,7 +406,7 @@ interface CreateContext {
|
|
|
405
406
|
args: {
|
|
406
407
|
name?: string;
|
|
407
408
|
metadata?: Metadata | (() => Metadata);
|
|
408
|
-
callbacks?: GlobalStoreCallbacks<State,
|
|
409
|
+
callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
|
|
409
410
|
actions?: ActionsConfig;
|
|
410
411
|
}): {
|
|
411
412
|
/**
|
|
@@ -469,7 +470,7 @@ interface CreateContext {
|
|
|
469
470
|
* - **`Context`** — The raw React `Context` object for advanced usage, such as integration with
|
|
470
471
|
* external tools or non-React consumers.
|
|
471
472
|
*/
|
|
472
|
-
<State, Metadata extends BaseMetadata, ActionsConfig extends ContextActionCollectionConfig<State, Metadata
|
|
473
|
+
<State, Metadata extends BaseMetadata, ActionsConfig extends ContextActionCollectionConfig<State, Metadata>>(
|
|
473
474
|
/**
|
|
474
475
|
* @description Initial state value or initializer function.
|
|
475
476
|
*/
|
|
@@ -484,7 +485,7 @@ interface CreateContext {
|
|
|
484
485
|
args: {
|
|
485
486
|
name?: string;
|
|
486
487
|
metadata?: Metadata | (() => Metadata);
|
|
487
|
-
callbacks?: GlobalStoreCallbacks<State,
|
|
488
|
+
callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
|
|
488
489
|
actions: ActionsConfig;
|
|
489
490
|
}): {
|
|
490
491
|
/**
|
package/createContext.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=(e,t,r,o,n)=>(()=>{"use strict";var a={78:t=>{t.exports=e},155:e=>{e.exports=t},506:e=>{e.exports=r},639:(e,t,r)=>{var o=function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++)t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}return r},n=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var a=r(155),u=r(778),i=n(r(506)),l=n(r(773)),s=n(r(78));function c(e,t){var r,o=this,n=null!==(r=null==t?void 0:t.name)&&void 0!==r?r:(0,s.default)(),i=function(){var r=o.api();return(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":use")),(0,a.useMemo)((function(){return r.createSelectorHook(e,t)}),[r]).apply(void 0,arguments)},l={displayName:this.displayName,createSelectorHook:c.bind(i),api:function(){(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":api"));var r=o.api();return(0,a.useMemo)((function(){var o=r.createObservable(e,t);return Object.assign(Object.assign({},r),{getState:function(){return o.getState()},subscribe:o.subscribe.bind(o),createSelectorHook:u.createSelectorHook.bind(o),createObservable:u.createObservable.bind(o)})}),[r])}};return Object.assign(i,l),i}t.createContext=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=(0,a.createContext)(null),d=null!==(t=r.name)&&void 0!==t?t:(0,s.default)(),v=function(t){var s=function(t){var n=t.value,s=o(t,["value"]);(0,a.useDebugValue)("".concat(d,":Provider"));var c=(0,a.useMemo)((function(){var t,o,a,c,d=function(){return(0,i.default)(e)?e():e},v=(0,l.default)(n)?d():(0,i.default)(n)?n(d()):n,f=new u.GlobalStore(v,Object.assign(Object.assign({},r),{metadata:null!==(t=(0,i.default)(r.metadata)?r.metadata():r.metadata)&&void 0!==t?t:{}})),p={use:b};return Object.assign(f.storeTools,p),null===(a=null===(o=r.callbacks)||void 0===o?void 0:o.onCreated)||void 0===a||a.call(o,f.storeTools,f),null===(c=s.onCreated)||void 0===c||c.call(s,f.storeTools,f),f}),[]);return(0,a.useEffect)((function(){var e,t,o,n=[null===(t=null===(e=r.callbacks)||void 0===e?void 0:e.onMounted)||void 0===t?void 0:t.call(e,c.storeTools,c),null===(o=s.onMounted)||void 0===o?void 0:o.call(s,c.storeTools,c)].filter(Boolean);return function(){var e,t,r,o;null===(t=null===(e=c.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e,c),n.forEach((function(e){return null==e?void 0:e()})),null===(o=(r=c).__onUnMountContext)||void 0===o||o.call(r,c)}}),[c]),c}(t);return(0,a.createElement)(n.Provider,{value:s.use},t.children)};v.displayName="".concat(d,":Provider"),n.displayName="".concat(d);var f={makeProviderWrapper:function(e){var t={current:void 0,instance:void 0};return{wrapper:function(r){var n=r.children,u=o(r,["children"]);return(0,a.createElement)(v,Object.assign(Object.assign(Object.assign({},e),u),{onCreated:function(r,o){var n;t.current=r,t.instance=o,null===(n=null==e?void 0:e.onCreated)||void 0===n||n.call(e,r,o)}}),n)},context:t}}},b=function(){(0,a.useDebugValue)("".concat(d,":use"));var e=(0,a.useContext)(n);if(!e)throw new Error("use hook must be used within a ContextProvider");return e.apply(void 0,arguments)},p=function(){(0,a.useDebugValue)("".concat(d,":api"));var e=(0,a.useContext)(n);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},m={displayName:n.displayName,createSelectorHook:c.bind(b),api:p,select:function(){return b.apply(void 0,arguments)[0]},observable:function(){(0,a.useDebugValue)("".concat(d,":observable"));for(var e=p(),t=arguments.length,r=new Array(t),o=0;o<t;o++)r[o]=arguments[o];var n=(0,a.useRef)(r);return n.current=r,(0,a.useMemo)((function(){var t,r,o;return e.createObservable((function(e){return(0,n.current[0])(e)}),{isEqualRoot:(null===(t=n.current[1])||void 0===t?void 0:t.isEqualRoot)?function(e,t){var r=n.current[1].isEqualRoot;return Boolean(r(e,t))}:void 0,isEqual:(null===(r=n.current[1])||void 0===r?void 0:r.isEqual)?function(e,t){var r=n.current[1].isEqual;return Boolean(r(e,t))}:void 0,name:null===(o=n.current[1])||void 0===o?void 0:o.name})}),[e])},actions:function(){return p().actions}};return Object.assign(v,f),Object.assign(b,m),{use:b,Provider:v,Context:n}},t.default=t.createContext},773:e=>{e.exports=o},778:e=>{e.exports=n}},u={};return function e(t){var r=u[t];if(void 0!==r)return r.exports;var o=u[t]={exports:{}};return a[t](o,o.exports,e),o.exports}(639)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","json-storage-formatter/isFunction","json-storage-formatter/isNil","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e["./uniqueId.js"],e.react,e["json-storage-formatter/isFunction"],e["json-storage-formatter/isNil"],e["./GlobalStore.js"]);
|
|
1
|
+
var e,t;e=this,t=(e,t,r,o,n)=>(()=>{"use strict";var a={78:t=>{t.exports=e},155:e=>{e.exports=t},506:e=>{e.exports=r},639:(e,t,r)=>{var o=function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++)t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}return r},n=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var a=r(155),u=r(778),i=n(r(506)),l=n(r(773)),s=n(r(78));function c(e,t){var r,o=this,n=null!==(r=null==t?void 0:t.name)&&void 0!==r?r:(0,s.default)("sh:"),i=function(){var r=o.api();return(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":use")),(0,a.useMemo)((function(){return r.createSelectorHook(e,t)}),[r]).apply(void 0,arguments)},l={displayName:this.displayName,createSelectorHook:c.bind(i),api:function(){(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":api"));var r=o.api();return(0,a.useMemo)((function(){var o=r.createObservable(e,t);return Object.assign(Object.assign({},r),{getState:function(){return o.getState()},subscribe:o.subscribe.bind(o),createSelectorHook:u.createSelectorHook.bind(o),createObservable:u.createObservable.bind(o)})}),[r])}};return Object.assign(i,l),i}t.createContext=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=(0,a.createContext)(null),d=null!==(t=r.name)&&void 0!==t?t:(0,s.default)("ctx:"),v=function(t){var s=function(t){var n=t.value,s=o(t,["value"]);(0,a.useDebugValue)("".concat(d,":Provider"));var c=(0,a.useMemo)((function(){var t,o,a,c,d=function(){return(0,i.default)(e)?e():e},v=(0,l.default)(n)?d():(0,i.default)(n)?n(d()):n,f=new u.GlobalStore(v,Object.assign(Object.assign({},r),{metadata:null!==(t=(0,i.default)(r.metadata)?r.metadata():r.metadata)&&void 0!==t?t:{}})),p={use:b};return Object.assign(f.storeTools,p),null===(a=null===(o=r.callbacks)||void 0===o?void 0:o.onCreated)||void 0===a||a.call(o,f.storeTools,f),null===(c=s.onCreated)||void 0===c||c.call(s,f.storeTools,f),f}),[]);return(0,a.useEffect)((function(){var e,t,o,n=[null===(t=null===(e=r.callbacks)||void 0===e?void 0:e.onMounted)||void 0===t?void 0:t.call(e,c.storeTools,c),null===(o=s.onMounted)||void 0===o?void 0:o.call(s,c.storeTools,c)].filter(Boolean);return function(){var e,t,r,o;null===(t=null===(e=c.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e,c),n.forEach((function(e){return null==e?void 0:e()})),null===(o=(r=c).__onUnMountContext)||void 0===o||o.call(r,c)}}),[c]),c}(t);return(0,a.createElement)(n.Provider,{value:s.use},t.children)};v.displayName="".concat(d,":Provider"),n.displayName="".concat(d);var f={makeProviderWrapper:function(e){var t={current:void 0,instance:void 0};return{wrapper:function(r){var n=r.children,u=o(r,["children"]);return(0,a.createElement)(v,Object.assign(Object.assign(Object.assign({},e),u),{onCreated:function(r,o){var n;t.current=r,t.instance=o,null===(n=null==e?void 0:e.onCreated)||void 0===n||n.call(e,r,o)}}),n)},context:t}}},b=function(){(0,a.useDebugValue)("".concat(d,":use"));var e=(0,a.useContext)(n);if(!e)throw new Error("use hook must be used within a ContextProvider");return e.apply(void 0,arguments)},p=function(){(0,a.useDebugValue)("".concat(d,":api"));var e=(0,a.useContext)(n);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},m={displayName:n.displayName,createSelectorHook:c.bind(b),api:p,select:function(){return b.apply(void 0,arguments)[0]},observable:function(){(0,a.useDebugValue)("".concat(d,":observable"));for(var e=p(),t=arguments.length,r=new Array(t),o=0;o<t;o++)r[o]=arguments[o];var n=(0,a.useRef)(r);return n.current=r,(0,a.useMemo)((function(){var t,r,o;return e.createObservable((function(e){return(0,n.current[0])(e)}),{isEqualRoot:(null===(t=n.current[1])||void 0===t?void 0:t.isEqualRoot)?function(e,t){var r=n.current[1].isEqualRoot;return Boolean(r(e,t))}:void 0,isEqual:(null===(r=n.current[1])||void 0===r?void 0:r.isEqual)?function(e,t){var r=n.current[1].isEqual;return Boolean(r(e,t))}:void 0,name:null===(o=n.current[1])||void 0===o?void 0:o.name})}),[e])},actions:function(){return p().actions}};return Object.assign(v,f),Object.assign(b,m),{use:b,Provider:v,Context:n}},t.default=t.createContext},773:e=>{e.exports=o},778:e=>{e.exports=n}},u={};return function e(t){var r=u[t];if(void 0!==r)return r.exports;var o=u[t]={exports:{}};return a[t](o,o.exports,e),o.exports}(639)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","json-storage-formatter/isFunction","json-storage-formatter/isNil","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e["./uniqueId.js"],e.react,e["json-storage-formatter/isFunction"],e["json-storage-formatter/isNil"],e["./GlobalStore.js"]);
|
package/createGlobalState.d.ts
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import type { ActionCollectionConfig, ActionCollectionResult, StateHook, BaseMetadata, GlobalStoreCallbacks, StoreTools } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Typescript is unable to infer the actions type correctly for the lifecycle callbacks
|
|
4
|
+
* so we use a generic AnyActions type here to bypass that limitation.
|
|
5
|
+
*
|
|
6
|
+
* The parameter could still be typed before using it with
|
|
7
|
+
* ```ts
|
|
8
|
+
* type StoreTools = InferStateApi<typeof <hook>>;
|
|
9
|
+
*
|
|
10
|
+
* onInit: (tools) => {
|
|
11
|
+
* const storeTools = tools as StoreTools;
|
|
12
|
+
* // ...
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* or when dealing with context:
|
|
16
|
+
*
|
|
17
|
+
* type ContextApi = InferContextApi<typeof <context>>;
|
|
18
|
+
*
|
|
19
|
+
* onInit: (tools) => {
|
|
20
|
+
* const storeTools = tools as ContextApi;
|
|
21
|
+
* // ...
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type AnyActions = Record<string, (...args: any[]) => any>;
|
|
2
26
|
interface CreateGlobalState {
|
|
3
27
|
/**
|
|
4
28
|
* Creates a global state hook.
|
|
@@ -89,7 +113,7 @@ interface CreateGlobalState {
|
|
|
89
113
|
<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? React.Dispatch<React.SetStateAction<State>> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
|
|
90
114
|
name?: string;
|
|
91
115
|
metadata?: Metadata;
|
|
92
|
-
callbacks?: GlobalStoreCallbacks<State,
|
|
116
|
+
callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
|
|
93
117
|
actions?: ActionsConfig;
|
|
94
118
|
}): StateHook<State, PublicStateMutator, Metadata>;
|
|
95
119
|
/**
|
|
@@ -144,7 +168,7 @@ interface CreateGlobalState {
|
|
|
144
168
|
<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata>, PublicStateMutator = ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
|
|
145
169
|
name?: string;
|
|
146
170
|
metadata?: Metadata;
|
|
147
|
-
callbacks?: GlobalStoreCallbacks<State,
|
|
171
|
+
callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
|
|
148
172
|
actions: ActionsConfig;
|
|
149
173
|
}): StateHook<State, PublicStateMutator, Metadata>;
|
|
150
174
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export type { StateApi, ObservableFragment, MetadataSetter, StateChanges, StoreTools, ActionCollectionResult, GlobalStoreCallbacks, UseHookOptions, UnsubscribeCallback, SubscribeCallbackConfig, SubscribeCallback, BaseMetadata, MetadataGetter, SelectorCallback, SubscriberParameters, SubscriptionCallback, StateHook, ActionCollectionConfig, } from './types';
|
|
2
2
|
export { GlobalStore } from './GlobalStore';
|
|
3
|
-
export { GlobalStoreAbstract } from './GlobalStoreAbstract';
|
|
4
3
|
export { createGlobalState, type InferActionsType, type InferStateApi } from './createGlobalState';
|
|
5
4
|
export { shallowCompare } from './shallowCompare';
|
|
6
|
-
export { uniqueId } from './uniqueId';
|
|
5
|
+
export { uniqueId, type BrandedId } from './uniqueId';
|
|
7
6
|
export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
|
|
8
7
|
export { isRecord } from './isRecord';
|
|
9
8
|
export { type ContextProvider, type ContextHook, type InferContextApi, createContext } from './createContext';
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -180,7 +180,7 @@ export type StateChanges<State> = {
|
|
|
180
180
|
**/
|
|
181
181
|
export type StoreTools<State, StateMutator = React.Dispatch<React.SetStateAction<State>>, Metadata extends BaseMetadata = BaseMetadata> = {
|
|
182
182
|
/**
|
|
183
|
-
* The actions available for the global state
|
|
183
|
+
* The actions available for the global state if provided
|
|
184
184
|
*/
|
|
185
185
|
actions: StateMutator extends AnyFunction ? null : StateMutator;
|
|
186
186
|
/**
|
|
@@ -198,7 +198,16 @@ export type StoreTools<State, StateMutator = React.Dispatch<React.SetStateAction
|
|
|
198
198
|
/**
|
|
199
199
|
* @description Function to set the state value
|
|
200
200
|
*/
|
|
201
|
-
setState: React.
|
|
201
|
+
setState: (setter: React.SetStateAction<State>, args?: {
|
|
202
|
+
/**
|
|
203
|
+
* @description Force update even if the state value did not change, this is for advanced use cases only
|
|
204
|
+
*/
|
|
205
|
+
forceUpdate?: boolean;
|
|
206
|
+
/**
|
|
207
|
+
* @description Optional identifier visible on the devtools
|
|
208
|
+
*/
|
|
209
|
+
identifier?: string;
|
|
210
|
+
}) => void;
|
|
202
211
|
/**
|
|
203
212
|
* @description Subscribe to the state changes
|
|
204
213
|
* You can subscribe to the whole state or to a fragment of the state by passing a selector as first parameter,
|
package/uniqueId.d.ts
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A branded unique identifier type
|
|
3
|
+
*/
|
|
4
|
+
export type BrandedId<T extends string | undefined> = `${T extends string ? T : ''}${string}` & {
|
|
5
|
+
__brand: T;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Generates a unique identifier string, optionally prefixed.
|
|
9
|
+
* @param prefix - Optional string to prefix the unique ID
|
|
10
|
+
* @returns A unique identifier string
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* uniqueId(); // e.g., "k9j3n5x8q2"
|
|
14
|
+
* uniqueId('user:'); // e.g., "user:k9j3n5x8q2"
|
|
15
|
+
*
|
|
16
|
+
* // Creating a reusable unique ID generator for a specific prefix
|
|
17
|
+
* const makeOrderId = uniqueId.for('order:');
|
|
18
|
+
* makeOrderId(); // e.g., "order:k9j3n5x8q2"
|
|
19
|
+
* makeOrderId.assert('order:k9j3n5x8q2'); // true
|
|
20
|
+
* makeOrderId.assert('user:k9j3n5x8q2
|
|
21
|
+
*/
|
|
22
|
+
export declare const uniqueId: {
|
|
23
|
+
<T extends string | undefined>(prefix?: T): BrandedId<T>;
|
|
24
|
+
for<T extends string>(prefix: T): {
|
|
25
|
+
(): BrandedId<T>;
|
|
26
|
+
is(value: unknown): value is BrandedId<T>;
|
|
27
|
+
assert(value: unknown): asserts value is BrandedId<T>;
|
|
28
|
+
};
|
|
29
|
+
of<T extends string>(): () => BrandedId<T>;
|
|
30
|
+
};
|
|
2
31
|
export default uniqueId;
|
package/uniqueId.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var t;t=()=>(()=>{"use strict";var t={};return((t,e)=>{var r,n,o;Object.defineProperty(e,"__esModule",{value:!0}),e.uniqueId=void 0,e.uniqueId=(r=0,n=function(t,e){return t+Date.now().toString(36)+e.toString(36)},(o=function(t){return r===Number.MAX_SAFE_INTEGER&&(r=0),n(t,r++)}).for=function(t){var e=0,r=function(){return e===Number.MAX_SAFE_INTEGER&&(e=0),n(t,e++)};return r.is=function(e){return"string"==typeof e&&e.startsWith(t)},r.assert=function(e){if(!r.is(e))throw new Error('The value "'.concat(String(e),'" does not match the expected prefix "').concat(String(t),'"'))},r},o.of=function(){return function(){return o()}},o),e.default=e.uniqueId})(0,t),t})(),"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["react-hooks-global-states"]=t():this["react-hooks-global-states"]=t();
|
package/webpack.config.js
CHANGED
|
@@ -5,7 +5,6 @@ const TerserPlugin = require('terser-webpack-plugin');
|
|
|
5
5
|
const individualEntries = {
|
|
6
6
|
createContext: './src/createContext.ts',
|
|
7
7
|
GlobalStore: './src/GlobalStore.ts',
|
|
8
|
-
GlobalStoreAbstract: './src/GlobalStoreAbstract.ts',
|
|
9
8
|
createGlobalState: './src/createGlobalState.ts',
|
|
10
9
|
types: './src/types.ts',
|
|
11
10
|
isRecord: './src/isRecord.ts',
|
package/GlobalStoreAbstract.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, StoreTools, BaseMetadata, StateChanges, ActionCollectionResult } from './types';
|
|
2
|
-
import { GlobalStore } from './GlobalStore';
|
|
3
|
-
/**
|
|
4
|
-
* @description
|
|
5
|
-
* Use this class to extends the capabilities of the GlobalStore.
|
|
6
|
-
* by implementing the abstract methods onInitialize and onChange.
|
|
7
|
-
* You can use this class to create a store with async storage.
|
|
8
|
-
*/
|
|
9
|
-
export declare abstract class GlobalStoreAbstract<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> | undefined | unknown, PublicStateMutator = keyof ActionsConfig extends never | undefined ? React.Dispatch<React.SetStateAction<State>> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>> extends GlobalStore<State, Metadata, ActionsConfig, PublicStateMutator> {
|
|
10
|
-
protected onInit: (args: StoreTools<State, PublicStateMutator, Metadata>) => void;
|
|
11
|
-
protected onStateChanged: (args: StoreTools<State, PublicStateMutator, Metadata> & StateChanges<State>) => void;
|
|
12
|
-
protected abstract onInitialize: (args: StoreTools<State, PublicStateMutator, Metadata>) => void;
|
|
13
|
-
protected abstract onChange: (args: StoreTools<State, PublicStateMutator, Metadata> & StateChanges<State>) => void;
|
|
14
|
-
}
|
|
15
|
-
export default GlobalStoreAbstract;
|
package/GlobalStoreAbstract.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var t;t=t=>(()=>{"use strict";var e={778:e=>{e.exports=t}},o={};function r(t){var n=o[t];if(void 0!==n)return n.exports;var i=o[t]={exports:{}};return e[t](i,i.exports,r),i.exports}var n={};return(()=>{var t=n;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 o(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(o=function(){return!!t})()}function i(t){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},i(t)}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStoreAbstract=void 0;var u=function(t){function r(){var t;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,r),(t=function(t,r,n){return r=i(r),function(t,o){if(o&&("object"==e(o)||"function"==typeof o))return o;if(void 0!==o)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,o()?Reflect.construct(r,n||[],i(t).constructor):r.apply(t,n))}(this,r,arguments)).onInit=function(e){t.onInitialize(e)},t.onStateChanged=function(e){t.onChange(e)},t}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&c(t,e)}(r,t),n=r,Object.defineProperty(n,"prototype",{writable:!1}),n;var n}(r(778).GlobalStore);t.GlobalStoreAbstract=u,t.default=u})(),n})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("./GlobalStore.js")):this["react-hooks-global-states"]=t(this["./GlobalStore.js"]);
|