react-hooks-global-states 15.0.15 → 15.0.17

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 CHANGED
@@ -1,8 +1,4 @@
1
1
  import type { ActionCollectionConfig, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, SubscribeCallbackConfig, SubscribeCallback, SelectorCallback, SubscriberParameters, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment, UnsubscribeCallback, AnyFunction, ReadonlyHook, ReadonlyStateApi, CleanupFunction } from './types';
2
- /**
3
- * A unique symbol used as a default value placeholder.
4
- */
5
- export declare const DEFAULT: unique symbol;
6
2
  /**
7
3
  * The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
8
4
  * */
package/GlobalStore.js CHANGED
@@ -1 +1 @@
1
- var t,e;t=this,e=(t,e,s,i,n,a)=>(()=>{"use strict";var o={12:e=>{e.exports=t},78:t=>{t.exports=e},132:function(t,e,s){var i,n=this&&this.__createBinding||(Object.create?function(t,e,s,i){void 0===i&&(i=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,i,n)}:function(t,e,s,i){void 0===i&&(i=s),t[i]=e[s]}),a=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),o=this&&this.__importStar||(i=function(t){return i=Object.getOwnPropertyNames||function(t){var e=[];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[e.length]=s);return e},i(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s=i(t),o=0;o<s.length;o++)"default"!==s[o]&&n(e,t,s[o]);return a(e,t),e}),r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0,e.createObservable=v,e.createSelectorHook=g;const l=s(12),c=r(s(506)),u=r(s(487)),d=o(s(673)),h=r(s(361)),b=r(s(78)),p=r(s(627));class f{constructor(t,e={metadata:{}}){var s,i;this.cleanupFunctions=[],this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Set,this.createSelectorHook=g,this.createObservable=v;const{metadata:n,callbacks:a,actions:o,name:r}=e;if((0,c.default)(t)&&(this.stateCallback=t),(0,c.default)(n)&&(this.metadataCallback=n),this._name=null!=r?r:(0,b.default)("gs:"),this.state=this.stateCallback?this.stateCallback():t,this.metadata=this.metadataCallback?this.metadataCallback():null!=n?n:{},this.callbacks=null!=a?a:null,this.actionsConfig=null!=o?o:null,p.default.isDevToolsPresent){const t=null!==(s=(new Error).stack)&&void 0!==s?s:"";null===(i=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(p.default,this,e,t)}this.constructor!==f||this.initialize()}async initialize(){var t,e,s,i,n,a,o,r;const l=null!==(e=null===(t=this.__devtools_initialize_getStoreActionsMapWrapped)||void 0===t?void 0:t.call(this))&&void 0!==e?e:this.getStoreActionsMap(),{actions:u,storeTools:d}=l;this.actions=u,this.storeTools=d,this.use=this.getMainHook();const h=null!==(i=null===(s=this.onInit)||void 0===s?void 0:s.call(this))&&void 0!==i?i:null;(0,c.default)(h)&&this.cleanupFunctions.push(h);const{onInit:b}=null!==(n=this.callbacks)&&void 0!==n?n:{};if(!b)return;const p=null!==(o=null===(a=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===a?void 0:a.call(this,"config/onInit/"))&&void 0!==o?o:d,f=null!==(r=null==b?void 0:b(p))&&void 0!==r?r:null;(0,c.default)(f)&&this.cleanupFunctions.push(f)}executeSetStateForSubscriber(t,e){const{selector:s,onStoreChange:i,currentState:n,isEqualRoot:a=(t,e)=>t===e,isEqual:o=(t,e)=>t===e}=t;if(!e.forceUpdate&&a(e.currentState,e.newState))return{didUpdate:!1};const r=s?s(e.newState):e.newState;return!e.forceUpdate&&o(n,r)?{didUpdate:!1}:(this.partialUpdateSubscription(t,{currentState:r}),i({state:r},{identifier:e.identifier}),{didUpdate:!0})}setActualStateWithoutValidations(t,{forceUpdate:e,identifier:s}){const i=this.state;if(!e&&i===t)return;this.state=t;const n=this.subscribers.values(),a={forceUpdate:e,newState:t,currentState:i,identifier:s};for(const t of n)this.executeSetStateForSubscriber(t,a)}setMetadata(t){const e=(0,c.default)(t)?t(this.metadata):t;this.metadata=e}getMetadata(){return this.metadata}getState(){return this.state}subscribe(...[t,e,s]){var i;const n=(0,c.default)(e),a=n?t:void 0,o=n?e:t,r=null!==(i=n?s:e)&&void 0!==i?i:void 0,l=a?a(this.state):this.state;(null==r?void 0:r.skipFirst)||o(l);const u=Object.assign({selector:a,currentState:l,onStoreChange:({state:t})=>o(t)},r);return this.subscribeCallback(u),()=>{this.subscribers.delete(u)}}subscribeCallback(t){var e,s;return null===(s=null===(e=this.callbacks)||void 0===e?void 0:e.onSubscribed)||void 0===s||s.call(e,this.storeTools,t),this.subscribers.add(t),()=>{this.subscribers.delete(t)}}partialUpdateSubscription(t,e){Object.assign(t,e)}getMainHook(){const t=`globalHook:${this._name}`,e=(e,s=[])=>{(0,l.useDebugValue)(t);const i=(0,d.isArray)(s)?{dependencies:s}:null!=s?s:{},n=(0,l.useRef)(null),a=t=>(0,c.default)(e)?e(t):t;n.current=(()=>n.current?n.current:Object.assign({selector:a,currentState:a(this.state),onStoreChange:()=>{throw new Error("Callback not set")}},i))();const o=n.current.dependencies,r=null==i?void 0:i.dependencies,u=Object.assign(Object.assign({},i),{selector:a,dependencies:r});this.partialUpdateSubscription(n.current,u);const{subscribe:h,getSnapshot:b,getServerSnapshot:p}=(0,l.useMemo)((()=>{const t=()=>n.current.currentState;return{subscribe:t=>(n.current.onStoreChange=t,this.subscribeCallback(n.current)),getSnapshot:t,getServerSnapshot:t}}),[]);return this.reselectIfDependenciesChanged({subscription:n.current,newDependencies:r,currentDependencies:o}),[(0,l.useSyncExternalStore)(h,b,p),this.getStateOrchestrator(),this.metadata]},s={actions:this.actions,createObservable:this.createObservable.bind(this),createSelectorHook:this.createSelectorHook.bind(this),dispose:this.dispose.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),reset:this.reset.bind(this),setMetadata:this.setMetadata.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),subscribers:this.subscribers,use:e,select:(...t)=>e(...t)[0]};return Object.assign(e,s),e}reselectIfDependenciesChanged({subscription:t,newDependencies:e,currentDependencies:s}){t.selector&&s!==e&&((null==s?void 0:s.length)===(null==e?void 0:e.length)&&(0,d.default)(s,e)||this.partialUpdateSubscription(t,{currentState:t.selector(this.state)}))}getStateOrchestrator(){return(()=>this.actions?this.actions:this.setState.bind(this))()}setState(t,{forceUpdate:e,identifier:s}={}){var i,n,a,o,r,l;const u=this.state,d=(0,c.default)(t)?t(u):t;if(!e&&this.state===d)return;const h=this.setActualStateWithoutValidations,b={previousState:u,state:d,identifier:s},p=Object.assign(Object.assign(Object.assign({},this.storeTools),b),{setState:h});(null===(i=this.callbacks)||void 0===i?void 0:i.computePreventStateChange)&&(null===(a=null===(n=this.callbacks)||void 0===n?void 0:n.computePreventStateChange)||void 0===a?void 0:a.call(n,p))||(this.setActualStateWithoutValidations(d,{forceUpdate:e,identifier:s}),null===(o=this.onStateChanged)||void 0===o||o.call(this,p),null===(l=null===(r=this.callbacks)||void 0===r?void 0:r.onStateChanged)||void 0===l||l.call(r,p))}getStoreActionsMap(){const t={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),actions:null};if(!(0,u.default)(this.actionsConfig))return{actions:null,storeTools:t};const e=this.actionsConfig,s={};t.actions=s;const i=Object.keys(e);for(const n of i)Object.assign(s,{[n](...i){const a=e[n].apply(s,i);return"function"!=typeof a&&(0,h.default)(n),a.call(s,t)}});return{actions:s,storeTools:t}}removeSubscriptions(){this.subscribers.clear()}executeCleanupTasks(){this.cleanupFunctions.forEach((t=>{null==t||t()})),this.cleanupFunctions.length=0}dispose(){this.removeSubscriptions(),this.executeCleanupTasks()}reset(t,s){var i,n,a,o,r,l;this.executeCleanupTasks();const u=(()=>t===e.DEFAULT?this.state:this.stateCallback?this.stateCallback():t)(),d=(()=>s===e.DEFAULT?this.metadata:this.metadataCallback?this.metadataCallback():s)();this.setActualStateWithoutValidations(u,{forceUpdate:!0}),this.metadata=d;const h=null!==(n=null===(i=this.onInit)||void 0===i?void 0:i.call(this))&&void 0!==n?n:null;(0,c.default)(h)&&this.cleanupFunctions.push(h);const{onInit:b}=null!==(a=this.callbacks)&&void 0!==a?a:{};if(!b)return;const p=null!==(r=null===(o=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===o?void 0:o.call(this,"config/onInit/"))&&void 0!==r?r:this.storeTools,f=null!==(l=null==b?void 0:b(p))&&void 0!==l?l:null;(0,c.default)(f)&&this.cleanupFunctions.push(f)}}function v(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=r.subscribe.bind(r),u={getState:r.getState.bind(r),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}function g(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=(...t)=>{const[e]=r.use(...t);return e},u={getState:()=>r.getState(),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}e.GlobalStore=f,e.default=f},361:t=>{t.exports=s},487:t=>{t.exports=i},506:t=>{t.exports=n},627:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});const s=globalThis;s.isDevToolsPresent=Boolean(s.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=s},673:t=>{t.exports=a}},r={};return function t(e){var s=r[e];if(void 0!==s)return s.exports;var i=r[e]={exports:{}};return o[e].call(i.exports,i,i.exports,t),i.exports}(132)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):"function"==typeof define&&define.amd?define(["react","./uniqueId.js","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","json-storage-formatter/isFunction","./shallowCompare.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("react"),require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):t["react-hooks-global-states"]=e(t.React,t["./uniqueId.js"],t["./throwWrongKeyOnActionCollectionConfig.js"],t["./isRecord.js"],t["json-storage-formatter/isFunction"],t["./shallowCompare.js"]);
1
+ var t,e;t=this,e=(t,e,s,i,n,a)=>(()=>{"use strict";var o={12:e=>{e.exports=t},78:t=>{t.exports=e},132:function(t,e,s){var i,n=this&&this.__createBinding||(Object.create?function(t,e,s,i){void 0===i&&(i=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,i,n)}:function(t,e,s,i){void 0===i&&(i=s),t[i]=e[s]}),a=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),o=this&&this.__importStar||(i=function(t){return i=Object.getOwnPropertyNames||function(t){var e=[];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[e.length]=s);return e},i(t)},function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s=i(t),o=0;o<s.length;o++)"default"!==s[o]&&n(e,t,s[o]);return a(e,t),e}),r=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.GlobalStore=void 0,e.createObservable=v,e.createSelectorHook=g;const l=s(12),c=r(s(506)),u=r(s(487)),d=o(s(673)),h=r(s(361)),b=r(s(78)),p=r(s(627));class f{constructor(t,e={metadata:{}}){var s,i;this.cleanupFunctions=[],this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Set,this.createSelectorHook=g,this.createObservable=v;const{metadata:n,callbacks:a,actions:o,name:r}=e;if((0,c.default)(t)&&(this.stateCallback=t),(0,c.default)(n)&&(this.metadataCallback=n),this._name=null!=r?r:(0,b.default)("gs:"),this.state=this.stateCallback?this.stateCallback():t,this.metadata=this.metadataCallback?this.metadataCallback():null!=n?n:{},this.callbacks=null!=a?a:null,this.actionsConfig=null!=o?o:null,p.default.isDevToolsPresent){const t=null!==(s=(new Error).stack)&&void 0!==s?s:"";null===(i=p.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===i||i.call(p.default,this,e,t)}this.constructor!==f||this.initialize()}async initialize(){var t,e,s,i,n,a,o,r;const l=null!==(e=null===(t=this.__devtools_initialize_getStoreActionsMapWrapped)||void 0===t?void 0:t.call(this))&&void 0!==e?e:this.getStoreActionsMap(),{actions:u,storeTools:d}=l;this.actions=u,this.storeTools=d,this.use=this.getMainHook();const h=null!==(i=null===(s=this.onInit)||void 0===s?void 0:s.call(this))&&void 0!==i?i:null;(0,c.default)(h)&&this.cleanupFunctions.push(h);const{onInit:b}=null!==(n=this.callbacks)&&void 0!==n?n:{};if(!b)return;const p=null!==(o=null===(a=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===a?void 0:a.call(this,"config/onInit/"))&&void 0!==o?o:d,f=null!==(r=null==b?void 0:b(p))&&void 0!==r?r:null;(0,c.default)(f)&&this.cleanupFunctions.push(f)}executeSetStateForSubscriber(t,e){const{selector:s,onStoreChange:i,currentState:n,isEqualRoot:a=(t,e)=>t===e,isEqual:o=(t,e)=>t===e}=t;if(!e.forceUpdate&&a(e.currentState,e.newState))return{didUpdate:!1};const r=s?s(e.newState):e.newState;return!e.forceUpdate&&o(n,r)?{didUpdate:!1}:(this.partialUpdateSubscription(t,{currentState:r}),i({state:r},{identifier:e.identifier}),{didUpdate:!0})}setActualStateWithoutValidations(t,{forceUpdate:e,identifier:s}){const i=this.state;if(!e&&i===t)return;this.state=t;const n=this.subscribers.values(),a={forceUpdate:e,newState:t,currentState:i,identifier:s};for(const t of n)this.executeSetStateForSubscriber(t,a)}setMetadata(t){const e=(0,c.default)(t)?t(this.metadata):t;this.metadata=e}getMetadata(){return this.metadata}getState(){return this.state}subscribe(...[t,e,s]){var i;const n=(0,c.default)(e),a=n?t:void 0,o=n?e:t,r=null!==(i=n?s:e)&&void 0!==i?i:void 0,l=a?a(this.state):this.state;(null==r?void 0:r.skipFirst)||o(l);const u=Object.assign({selector:a,currentState:l,onStoreChange:({state:t})=>o(t)},r);return this.subscribeCallback(u),()=>{this.subscribers.delete(u)}}subscribeCallback(t){var e,s;return null===(s=null===(e=this.callbacks)||void 0===e?void 0:e.onSubscribed)||void 0===s||s.call(e,this.storeTools,t),this.subscribers.add(t),()=>{this.subscribers.delete(t)}}partialUpdateSubscription(t,e){Object.assign(t,e)}getMainHook(){const t=`globalHook:${this._name}`,e=(e,s=[])=>{(0,l.useDebugValue)(t);const i=(0,d.isArray)(s)?{dependencies:s}:null!=s?s:{},n=(0,l.useRef)(null),a=t=>(0,c.default)(e)?e(t):t;n.current=(()=>n.current?n.current:Object.assign({selector:a,currentState:a(this.state),onStoreChange:()=>{throw new Error("Callback not set")}},i))();const o=n.current.dependencies,r=null==i?void 0:i.dependencies,u=Object.assign(Object.assign({},i),{selector:a,dependencies:r});this.partialUpdateSubscription(n.current,u);const{subscribe:h,getSnapshot:b,getServerSnapshot:p}=(0,l.useMemo)((()=>{const t=()=>n.current.currentState;return{subscribe:t=>(n.current.onStoreChange=t,this.subscribeCallback(n.current)),getSnapshot:t,getServerSnapshot:t}}),[]);return this.reselectIfDependenciesChanged({subscription:n.current,newDependencies:r,currentDependencies:o}),[(0,l.useSyncExternalStore)(h,b,p),this.getStateOrchestrator(),this.metadata]},s={actions:this.actions,createObservable:this.createObservable.bind(this),createSelectorHook:this.createSelectorHook.bind(this),dispose:this.dispose.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),reset:this.reset.bind(this),setMetadata:this.setMetadata.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),subscribers:this.subscribers,use:e,select:(...t)=>e(...t)[0]};return Object.assign(e,s),e}reselectIfDependenciesChanged({subscription:t,newDependencies:e,currentDependencies:s}){t.selector&&s!==e&&((null==s?void 0:s.length)===(null==e?void 0:e.length)&&(0,d.default)(s,e)||this.partialUpdateSubscription(t,{currentState:t.selector(this.state)}))}getStateOrchestrator(){return(()=>this.actions?this.actions:this.setState.bind(this))()}setState(t,{forceUpdate:e,identifier:s}={}){var i,n,a,o,r,l;const u=this.state,d=(0,c.default)(t)?t(u):t;if(!e&&this.state===d)return;const h=this.setActualStateWithoutValidations,b={previousState:u,state:d,identifier:s},p=Object.assign(Object.assign(Object.assign({},this.storeTools),b),{setState:h});(null===(i=this.callbacks)||void 0===i?void 0:i.computePreventStateChange)&&(null===(a=null===(n=this.callbacks)||void 0===n?void 0:n.computePreventStateChange)||void 0===a?void 0:a.call(n,p))||(this.setActualStateWithoutValidations(d,{forceUpdate:e,identifier:s}),null===(o=this.onStateChanged)||void 0===o||o.call(this,p),null===(l=null===(r=this.callbacks)||void 0===r?void 0:r.onStateChanged)||void 0===l||l.call(r,p))}getStoreActionsMap(){const t={setMetadata:this.setMetadata.bind(this),getMetadata:this.getMetadata.bind(this),getState:this.getState.bind(this),setState:this.setState.bind(this),subscribe:this.subscribe.bind(this),actions:null};if(!(0,u.default)(this.actionsConfig))return{actions:null,storeTools:t};const e=this.actionsConfig,s={};t.actions=s;const i=Object.keys(e);for(const n of i)Object.assign(s,{[n](...i){const a=e[n].apply(s,i);return"function"!=typeof a&&(0,h.default)(n),a.call(s,t)}});return{actions:s,storeTools:t}}removeSubscriptions(){this.subscribers.clear()}executeCleanupTasks(){this.cleanupFunctions.forEach((t=>{null==t||t()})),this.cleanupFunctions.length=0}dispose(){this.removeSubscriptions(),this.executeCleanupTasks()}reset(...t){var e,s,i,n,a,o;this.executeCleanupTasks();const r=t.length>0,l=(()=>r?t[0]:this.stateCallback?this.stateCallback():this.state)(),u=(()=>r?t[1]:this.metadataCallback?this.metadataCallback():this.metadata)();this.setActualStateWithoutValidations(l,{forceUpdate:!0}),this.setMetadata(u);const d=null!==(s=null===(e=this.onInit)||void 0===e?void 0:e.call(this))&&void 0!==s?s:null;(0,c.default)(d)&&this.cleanupFunctions.push(d);const{onInit:h}=null!==(i=this.callbacks)&&void 0!==i?i:{};if(!h)return;const b=null!==(a=null===(n=this.__devtools_getLifeCycleStoreToolsWrapper)||void 0===n?void 0:n.call(this,"config/onInit/"))&&void 0!==a?a:this.storeTools,p=null!==(o=null==h?void 0:h(b))&&void 0!==o?o:null;(0,c.default)(p)&&this.cleanupFunctions.push(p)}}function v(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=r.subscribe.bind(r),u={getState:r.getState.bind(r),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}function g(t,e){const s=null==e?void 0:e.name,i=null==e?void 0:e.isEqualRoot,n=null==e?void 0:e.isEqual;let a=this.getState(),o=(null!=t?t:t=>t)(a);const r=new f(o,{name:null!=s?s:(0,b.default)("sh:")}),l=this.subscribe((e=>{if((null!=i?i:Object.is)(a,e))return;a=e;const s=t(e);(null!=n?n:Object.is)(o,s)||(o=s,r.setState(s))}),{skipFirst:!0}),c=(...t)=>{const[e]=r.use(...t);return e},u={getState:()=>r.getState(),subscribe:r.subscribe.bind(r),createSelectorHook:g.bind(c),createObservable:v.bind(c),dispose:()=>{l(),r.dispose()},subscribers:r.subscribers};return Object.assign(c,u),c}e.GlobalStore=f,e.default=f},361:t=>{t.exports=s},487:t=>{t.exports=i},506:t=>{t.exports=n},627:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});const s=globalThis;s.isDevToolsPresent=Boolean(s.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=s},673:t=>{t.exports=a}},r={};return function t(e){var s=r[e];if(void 0!==s)return s.exports;var i=r[e]={exports:{}};return o[e].call(i.exports,i,i.exports,t),i.exports}(132)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):"function"==typeof define&&define.amd?define(["react","./uniqueId.js","./throwWrongKeyOnActionCollectionConfig.js","./isRecord.js","json-storage-formatter/isFunction","./shallowCompare.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("react"),require("./uniqueId.js"),require("./throwWrongKeyOnActionCollectionConfig.js"),require("./isRecord.js"),require("json-storage-formatter/isFunction"),require("./shallowCompare.js")):t["react-hooks-global-states"]=e(t.React,t["./uniqueId.js"],t["./throwWrongKeyOnActionCollectionConfig.js"],t["./isRecord.js"],t["json-storage-formatter/isFunction"],t["./shallowCompare.js"]);
package/createContext.js CHANGED
@@ -1 +1 @@
1
- var e,t;e=this,t=(e,t,r,o,a)=>(()=>{"use strict";var s={12:t=>{t.exports=e},78:e=>{e.exports=t},506:e=>{e.exports=r},695:function(e,t,r){var o=this&&this.__rest||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 a=0;for(o=Object.getOwnPropertySymbols(e);a<o.length;a++)t.indexOf(o[a])<0&&Object.prototype.propertyIsEnumerable.call(e,o[a])&&(r[o[a]]=e[o[a]])}return r},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;const s=r(12),n=r(778),l=a(r(506)),u=a(r(773)),i=a(r(78));function c(e,t){var r;const o=null!==(r=null==t?void 0:t.name)&&void 0!==r?r:(0,i.default)("sh:"),a=(...r)=>{const a=this.api();return(0,s.useDebugValue)(`${this.displayName}:selector`),(0,s.useDebugValue)(`${o}:use`),(0,s.useMemo)((()=>a.createSelectorHook(e,t)),[a])(...r)},l={displayName:this.displayName,createSelectorHook:c.bind(a),api:()=>{(0,s.useDebugValue)(`${this.displayName}:selector`),(0,s.useDebugValue)(`${o}:api`);const r=this.api();return(0,s.useMemo)((()=>{const o=r.createObservable(e,t);return Object.assign(Object.assign({},r),{getState:()=>o.getState(),subscribe:o.subscribe.bind(o),createSelectorHook:n.createSelectorHook.bind(o),createObservable:n.createObservable.bind(o)})}),[r])}};return Object.assign(a,l),a}t.createContext=(e,t={})=>{var r;const a=(0,s.createContext)(null),d=null!==(r=t.name)&&void 0!==r?r:(0,i.default)("ctx:"),v=r=>{const i=(r=>{var{value:a}=r,i=o(r,["value"]);(0,s.useDebugValue)(`${d}:Provider`);const c=(0,s.useMemo)((()=>{var r,o,s,c;const d=()=>(0,l.default)(e)?e():e,v=(0,u.default)(a)?d():(0,l.default)(a)?a(d()):a,b=new n.GlobalStore(v,Object.assign(Object.assign({},t),{metadata:null!==(r=(0,l.default)(t.metadata)?t.metadata():t.metadata)&&void 0!==r?r:{}})),f={use:p};return Object.assign(b.storeTools,f),null===(s=null===(o=t.callbacks)||void 0===o?void 0:o.onCreated)||void 0===s||s.call(o,b.storeTools,b),null===(c=i.onCreated)||void 0===c||c.call(i,b.storeTools,b),b}),[]);return(0,s.useEffect)((()=>{var e,r,o;const a=[null===(r=null===(e=t.callbacks)||void 0===e?void 0:e.onMounted)||void 0===r?void 0:r.call(e,c.storeTools,c),null===(o=i.onMounted)||void 0===o?void 0:o.call(i,c.storeTools,c)].filter(Boolean);return()=>{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),a.forEach((e=>null==e?void 0:e())),null===(o=(r=c).__onUnMountContext)||void 0===o||o.call(r,c)}}),[c]),c})(r);return(0,s.createElement)(a.Provider,{value:i.use},r.children)};v.displayName=`${d}:Provider`,a.displayName=`${d}`;const b={makeProviderWrapper:e=>{const t={current:void 0,instance:void 0};return{wrapper:r=>{var{children:a}=r,n=o(r,["children"]);return(0,s.createElement)(v,Object.assign(Object.assign(Object.assign({},e),n),{onCreated:(r,o)=>{var a;t.current=r,t.instance=o,null===(a=null==e?void 0:e.onCreated)||void 0===a||a.call(e,r,o)}}),a)},context:t}}},p=(...e)=>{(0,s.useDebugValue)(`${d}:use`);const t=(0,s.useContext)(a);if(!t)throw new Error("use hook must be used within a ContextProvider");return t(...e)},f=()=>{(0,s.useDebugValue)(`${d}:api`);const e=(0,s.useContext)(a);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},m={displayName:a.displayName,createSelectorHook:c.bind(p),api:f,select:(...e)=>p(...e)[0],observable:(...e)=>{(0,s.useDebugValue)(`${d}:observable`);const t=f(),r=(0,s.useRef)(e);return r.current=e,(0,s.useMemo)((()=>{var e,o,a;return t.createObservable((e=>(0,r.current[0])(e)),{isEqualRoot:(null===(e=r.current[1])||void 0===e?void 0:e.isEqualRoot)?(e,t)=>{const o=r.current[1].isEqualRoot;return Boolean(o(e,t))}:void 0,isEqual:(null===(o=r.current[1])||void 0===o?void 0:o.isEqual)?(e,t)=>{const o=r.current[1].isEqual;return Boolean(o(e,t))}:void 0,name:null===(a=r.current[1])||void 0===a?void 0:a.name})}),[t])},actions:()=>f().actions};return Object.assign(v,b),Object.assign(p,m),{use:p,Provider:v,Context:a}},t.default=t.createContext},773:e=>{e.exports=o},778:e=>{e.exports=a}},n={};return function e(t){var r=n[t];if(void 0!==r)return r.exports;var o=n[t]={exports:{}};return s[t].call(o.exports,o,o.exports,e),o.exports}(695)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["react","./uniqueId.js","json-storage-formatter/isFunction","json-storage-formatter/isNil","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e.React,e["./uniqueId.js"],e["json-storage-formatter/isFunction"],e["json-storage-formatter/isNil"],e["./GlobalStore.js"]);
1
+ var e,t;e=this,t=(e,t,o,r)=>(()=>{"use strict";var a={12:t=>{t.exports=e},78:e=>{e.exports=t},506:e=>{e.exports=o},695:function(e,t,o){var r=this&&this.__rest||function(e,t){var o={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(r=Object.getOwnPropertySymbols(e);a<r.length;a++)t.indexOf(r[a])<0&&Object.prototype.propertyIsEnumerable.call(e,r[a])&&(o[r[a]]=e[r[a]])}return o},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;const n=o(12),s=o(778),l=a(o(506)),u=a(o(78));function i(e,t){var o;const r=null!==(o=null==t?void 0:t.name)&&void 0!==o?o:(0,u.default)("sh:"),a=(...o)=>{const a=this.api();return(0,n.useDebugValue)(`${this.displayName}:selector`),(0,n.useDebugValue)(`${r}:use`),(0,n.useMemo)((()=>a.createSelectorHook(e,t)),[a])(...o)},l={displayName:this.displayName,createSelectorHook:i.bind(a),api:()=>{(0,n.useDebugValue)(`${this.displayName}:selector`),(0,n.useDebugValue)(`${r}:api`);const o=this.api();return(0,n.useMemo)((()=>{const r=o.createObservable(e,t);return Object.assign(Object.assign({},o),{getState:()=>r.getState(),subscribe:r.subscribe.bind(r),createSelectorHook:s.createSelectorHook.bind(r),createObservable:s.createObservable.bind(r)})}),[o])}};return Object.assign(a,l),a}t.createContext=(e,t={})=>{var o;const a=(0,n.createContext)(null),c=null!==(o=t.name)&&void 0!==o?o:(0,u.default)("ctx:"),d=()=>(0,l.default)(e)?e():e,v=e=>Object.prototype.hasOwnProperty.call(e,"value"),b=e=>{const o=(e=>{(0,n.useDebugValue)(`${c}:Provider`);const[o]=(0,n.useState)((()=>{var o,r,a,n;const u=v(e)?(0,l.default)(e.value)?e.value(d()):e.value:d(),i=new s.GlobalStore(u,Object.assign(Object.assign({},t),{metadata:null!==(o=(0,l.default)(t.metadata)?t.metadata():t.metadata)&&void 0!==o?o:{}})),c={use:f};return Object.assign(i.storeTools,c),null===(a=null===(r=t.callbacks)||void 0===r?void 0:r.onCreated)||void 0===a||a.call(r,i.storeTools,i),null===(n=e.onCreated)||void 0===n||n.call(e,i.storeTools,i),i}));return(0,n.useLayoutEffect)((()=>{v(e)&&!(0,l.default)(e.value)&&o.setState(e.value)}),[o,e.value]),(0,n.useEffect)((()=>{var r,a,n;const s=[null===(a=null===(r=t.callbacks)||void 0===r?void 0:r.onMounted)||void 0===a?void 0:a.call(r,o.storeTools,o),null===(n=e.onMounted)||void 0===n?void 0:n.call(e,o.storeTools,o)].filter(Boolean);return()=>{var e,t,r,a;null===(t=null===(e=o.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e,o),s.forEach((e=>null==e?void 0:e())),null===(a=(r=o).__onUnMountContext)||void 0===a||a.call(r,o)}}),[o]),o})(e);return(0,n.createElement)(a.Provider,{value:o.use},e.children)};b.displayName=`${c}:Provider`,a.displayName=`${c}`;const p={makeProviderWrapper:e=>{const t={current:void 0,instance:void 0};return{wrapper:o=>{var{children:a}=o,s=r(o,["children"]);return(0,n.createElement)(b,Object.assign(Object.assign(Object.assign({},e),s),{onCreated:(o,r)=>{var a;t.current=o,t.instance=r,null===(a=null==e?void 0:e.onCreated)||void 0===a||a.call(e,o,r)}}),a)},context:t}}},f=(...e)=>{(0,n.useDebugValue)(`${c}:use`);const t=(0,n.useContext)(a);if(!t)throw new Error("use hook must be used within a ContextProvider");return t(...e)},m=()=>{(0,n.useDebugValue)(`${c}:api`);const e=(0,n.useContext)(a);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},j={displayName:a.displayName,createSelectorHook:i.bind(f),api:m,select:(...e)=>f(...e)[0],observable:(...e)=>{(0,n.useDebugValue)(`${c}:observable`);const t=m(),o=(0,n.useRef)(e);return o.current=e,(0,n.useMemo)((()=>{var e,r,a;return t.createObservable((e=>(0,o.current[0])(e)),{isEqualRoot:(null===(e=o.current[1])||void 0===e?void 0:e.isEqualRoot)?(e,t)=>{const r=o.current[1].isEqualRoot;return Boolean(r(e,t))}:void 0,isEqual:(null===(r=o.current[1])||void 0===r?void 0:r.isEqual)?(e,t)=>{const r=o.current[1].isEqual;return Boolean(r(e,t))}:void 0,name:null===(a=o.current[1])||void 0===a?void 0:a.name})}),[t])},actions:()=>m().actions};return Object.assign(b,p),Object.assign(f,j),{use:f,Provider:b,Context:a}},t.default=t.createContext},778:e=>{e.exports=r}},n={};return function e(t){var o=n[t];if(void 0!==o)return o.exports;var r=n[t]={exports:{}};return a[t].call(r.exports,r,r.exports,e),r.exports}(695)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["react","./uniqueId.js","json-storage-formatter/isFunction","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e.React,e["./uniqueId.js"],e["json-storage-formatter/isFunction"],e["./GlobalStore.js"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hooks-global-states",
3
- "version": "15.0.15",
3
+ "version": "15.0.17",
4
4
  "description": "A package to easily handle global state across your React components using hooks.",
5
5
  "main": "./bundle.js",
6
6
  "types": "./index.d.ts",
@@ -68,12 +68,13 @@
68
68
  ],
69
69
  "scripts": {
70
70
  "format": "prettier --write . --log-level silent && yarn lint:fix",
71
- "test": "yarn build && yarn jest",
71
+ "test": "yarn build && yarn ts-check:tests && yarn jest",
72
+ "test:logs": "sh -c 'yarn build && yarn jest \"$@\" 2>&1 | grep -i \"console\" -A 2' --",
72
73
  "test:debug:no-watch": "node --inspect-brk node_modules/.bin/jest --no-watchman --runInBand",
73
74
  "test:debug": "node --inspect-brk node_modules/.bin/jest --watch --runInBand",
74
- "test:quick": "yarn test --no-coverage --maxWorkers=4 -c --no-watchman -u",
75
+ "test:quick": "yarn ts-check:tests && yarn test --no-coverage --maxWorkers=4 -c --no-watchman -u",
75
76
  "test:coverage": "yarn test --maxWorkers=4 -c --colors --no-watchman --verbose --coverage",
76
- "build": "yarn ts-check && yarn clean && webpack --config webpack.config.js && yarn ts-check:tests",
77
+ "build": "yarn ts-check && yarn clean && webpack --config webpack.config.js",
77
78
  "prepare": "yarn format && npm run build && yarn test:quick",
78
79
  "version": "npm run format && yarn build && yarn jest && git add -A",
79
80
  "postversion": "git push && git push --tags",
package/types.d.ts CHANGED
@@ -1139,12 +1139,11 @@ export type InferActionsType<Hook extends StateHook<Any, Any, Any>> = ReturnType
1139
1139
  * ```
1140
1140
  */
1141
1141
  export type InferStateApi<Hook extends StateHook<Any, Any, Any>> = Hook extends StateHook<infer State, infer PublicStateMutator, infer Metadata> ? StoreTools<State, PublicStateMutator, Metadata> : never;
1142
- export declare const __uniqueIdBrand: unique symbol;
1143
1142
  /**
1144
1143
  * Branded unique identifier
1145
1144
  */
1146
1145
  export type BrandedId<T extends string | undefined> = `${T extends string ? T : ''}${string}` & {
1147
- [__uniqueIdBrand]: T;
1146
+ __brand: T;
1148
1147
  };
1149
1148
  export interface UniqueId {
1150
1149
  /**
@@ -1152,10 +1151,10 @@ export interface UniqueId {
1152
1151
  *
1153
1152
  * @example
1154
1153
  * uniqueId(); // "k9j3n5x8q2"
1155
- * type Id1 = `${string}` & { [__uniqueIdBrand]: undefined };
1154
+ * type Id1 = `${string}` & { __brand: undefined };
1156
1155
  *
1157
1156
  * uniqueId('user:'); // "user:k9j3n5x8q2"
1158
- * type Id2 = `user:${string}` & { [__uniqueIdBrand]: 'user:' };
1157
+ * type Id2 = `user:${string}` & { __brand: 'user:' };
1159
1158
  */
1160
1159
  <T extends string | undefined>(prefix?: T): BrandedId<T>;
1161
1160
  /**
@@ -1164,36 +1163,36 @@ export interface UniqueId {
1164
1163
  * @example
1165
1164
  * const makeOrderId = uniqueId.for('order:');
1166
1165
  * const id = makeOrderId(); // "order:k9j3n5x8q2"
1167
- * type OrderId = `order:${string}` & { [__brand]: 'order:' };
1166
+ * type OrderId = `order:${string}` & { __brand: 'order:' };
1168
1167
  */
1169
1168
  for<T extends string>(prefix: T): {
1170
1169
  (): `${T}${string}` & {
1171
- [__uniqueIdBrand]: T;
1170
+ __brand: T;
1172
1171
  };
1173
1172
  /**
1174
1173
  * Checks if the given value matches the branded ID for this prefix.
1175
1174
  */
1176
1175
  is(value: unknown): value is `${T}${string}` & {
1177
- [__uniqueIdBrand]: T;
1176
+ __brand: T;
1178
1177
  };
1179
1178
  /**
1180
1179
  * Asserts that the value matches this branded ID, throws otherwise.
1181
1180
  */
1182
1181
  assert(value: unknown): asserts value is `${T}${string}` & {
1183
- [__uniqueIdBrand]: T;
1182
+ __brand: T;
1184
1183
  };
1185
1184
  /**
1186
1185
  * Returns a strictly branded generator using a custom symbol brand.
1187
1186
  */
1188
1187
  strict<Brand extends symbol>(): {
1189
1188
  (): `${T}${string}` & {
1190
- [__uniqueIdBrand]: Brand;
1189
+ __brand: Brand;
1191
1190
  };
1192
1191
  is(value: unknown): value is `${T}${string}` & {
1193
- [__uniqueIdBrand]: Brand;
1192
+ __brand: Brand;
1194
1193
  };
1195
1194
  assert(value: unknown): asserts value is `${T}${string}` & {
1196
- [__uniqueIdBrand]: Brand;
1195
+ __brand: Brand;
1197
1196
  };
1198
1197
  };
1199
1198
  };
@@ -1201,13 +1200,13 @@ export interface UniqueId {
1201
1200
  * Creates a reusable unique ID generator without a prefix.
1202
1201
  */
1203
1202
  of<T extends string>(): () => string & {
1204
- [__uniqueIdBrand]: T;
1203
+ __brand: T;
1205
1204
  };
1206
1205
  /**
1207
1206
  * Creates a strictly branded unique ID generator without a prefix.
1208
1207
  */
1209
1208
  strict<Brand extends symbol>(): () => string & {
1210
- [__uniqueIdBrand]: Brand;
1209
+ __brand: Brand;
1211
1210
  };
1212
1211
  }
1213
1212
  /**
package/uniqueId.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { UniqueId, BrandedId, __uniqueIdBrand } from './types';
1
+ import type { UniqueId, BrandedId } from './types';
2
2
  /**
3
3
  * Generates a unique identifier string, optionally prefixed.
4
4
  */
5
5
  export declare const uniqueId: UniqueId;
6
6
  export default uniqueId;
7
- export type { UniqueId, BrandedId, __uniqueIdBrand };
7
+ export type { UniqueId, BrandedId };