react-hooks-global-states 10.0.0 → 10.0.1
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.debugProps.d.ts +0 -5
- package/GlobalStore.js +1 -1
- package/createContext.d.ts +33 -10
- package/createContext.js +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
declare const debugProps: typeof globalThis & {
|
|
2
2
|
REACT_GLOBAL_STATE_HOOK_DEBUG?: ($this: unknown, args: undefined | {}, invokerStackHash: string) => void;
|
|
3
|
-
REACT_GLOBAL_STATE_TEMP_HOOKS: Map<string, [unknown, unknown]> | null;
|
|
4
|
-
sessionStorage?: {
|
|
5
|
-
getItem: (key: string) => string | null;
|
|
6
|
-
};
|
|
7
3
|
isDevToolsPresent: boolean;
|
|
8
|
-
reportToDevTools: (store: Record<string, unknown>, args: Record<string, unknown>, globalHookStackHash: string) => void;
|
|
9
4
|
};
|
|
10
5
|
export default debugProps;
|
package/GlobalStore.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e;t=this,e=(t,e,r,n,o,i,a,s)=>(()=>{"use strict";var u={45:e=>{e.exports=t},78:t=>{t.exports=e},155:t=>{t.exports=r},361:t=>{t.exports=n},487:t=>{t.exports=o},506:t=>{t.exports=i},673:t=>{t.exports=a},773:t=>{t.exports=s},811:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0});var n=r(773),o=globalThis;!function(){if(!o.REACT_GLOBAL_STATE_HOOK_DEBUG&&!(0,n.isNil)(o.sessionStorage)){try{if(!o.sessionStorage.getItem("REACT_GLOBAL_STATE_HOOK_DEBUG"))return}catch(t){return}o.REACT_GLOBAL_STATE_TEMP_HOOKS=new Map,setTimeout((function(){o.REACT_GLOBAL_STATE_TEMP_HOOKS=null}),6e4)}}(),o.isDevToolsPresent=Boolean(o.REACT_GLOBAL_STATE_HOOK_DEBUG||o.REACT_GLOBAL_STATE_TEMP_HOOKS),o.reportToDevTools=function(t,e,r){if(o.REACT_GLOBAL_STATE_HOOK_DEBUG)o.REACT_GLOBAL_STATE_HOOK_DEBUG(t,e,r);else if(o.REACT_GLOBAL_STATE_TEMP_HOOKS){var n=void 0!==globalThis.WeakRef?new globalThis.WeakRef(t):t,i=void 0!==globalThis.WeakRef?new globalThis.WeakRef(e):e;o.REACT_GLOBAL_STATE_TEMP_HOOKS.set(r,[n,i])}},e.default=o}},c={};function l(t){var e=c[t];if(void 0!==e)return e.exports;var r=c[t]={exports:{}};return u[t](r,r.exports,l),r.exports}var f={};return(()=>{var t=f;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){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,s=[],u=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=i.call(r)).done)&&(s.push(n.value),s.length!==e);u=!0);}catch(t){c=!0,o=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw o}}return s}}(t,e)||n(t,e)||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.")}()}function n(t,e){if(t){if("string"==typeof t)return o(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)?o(t,e):void 0}}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function i(){i=function(){return r};var t,r={},n=Object.prototype,o=n.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},u=s.iterator||"@@iterator",c=s.asyncIterator||"@@asyncIterator",l=s.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var o=e&&e.prototype instanceof m?e:m,i=Object.create(o.prototype),s=new q(n||[]);return a(i,"_invoke",{value:k(t,r,s)}),i}function d(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=h;var p="suspendedStart",v="suspendedYield",b="executing",g="completed",y={};function m(){}function S(){}function w(){}var O={};f(O,u,(function(){return this}));var E=Object.getPrototypeOf,j=E&&E(E(M([])));j&&j!==n&&o.call(j,u)&&(O=j);var _=w.prototype=m.prototype=Object.create(O);function T(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function C(t,r){function n(i,a,s,u){var c=d(t[i],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,s,u)}),(function(t){n("throw",t,s,u)})):r.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,u)}))}u(c.arg)}var i;a(this,"_invoke",{value:function(t,e){function o(){return new r((function(r,o){n(t,e,r,o)}))}return i=i?i.then(o,o):o()}})}function k(e,r,n){var o=p;return function(i,a){if(o===b)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=x(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===p)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=b;var c=d(e,r,n);if("normal"===c.type){if(o=n.done?g:v,c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=g,n.method="throw",n.arg=c.arg)}}}function x(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,x(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=d(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 L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function q(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function M(r){if(r||""===r){var n=r[u];if(n)return n.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var i=-1,a=function e(){for(;++i<r.length;)if(o.call(r,i))return e.value=r[i],e.done=!1,e;return e.value=t,e.done=!0,e};return a.next=a}}throw new TypeError(e(r)+" is not iterable")}return S.prototype=w,a(_,"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(_),t},r.awrap=function(t){return{__await:t}},T(C.prototype),f(C.prototype,c,(function(){return this})),r.AsyncIterator=C,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new C(h(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},T(_),f(_,l,"Generator"),f(_,u,(function(){return this})),f(_,"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=M,q.prototype={constructor:q,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(L),!e)for(var r in this)"t"===r.charAt(0)&&o.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,o){return s.type="throw",s.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),c=o.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(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;L(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:M(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},r}function a(t){var r=function(t){if("object"!=e(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=e(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==e(r)?r:r+""}Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var s,u=l(155),c=l(506),h=l(773),d=l(487),p=l(673),v=l(361),b=l(78),g=l(45),y=(s=l(811))&&s.__esModule?s:{default:s},m=function(){return t=function t(e){var o,s=this,l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{metadata:{}};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.wasDisposed=!1,this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Map,this.initialize=function(){return t=s,e=void 0,r=i().mark((function t(){var e,r,n,o,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Object.keys(null!==(e=this.actionsConfig)&&void 0!==e?e:{}).length>0&&(this.actions=this.getStoreActionsMap()),n=this.onInit,o=null!==(r=this.callbacks)&&void 0!==r?r:{},a=o.onInit,n||a){t.next=6;break}return t.abrupt("return");case 6:s=this.getConfigCallbackParam(),null==n||n(s),(0,h.isNil)(a)||null==a||a(s);case 9: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,n,o,i=t.selector,a=t.callback,u=t.currentState,c=t.getConfig,l=null!==(r=null==c?void 0:c())&&void 0!==r?r:{};if(!e.forceUpdate&&(null!==(n=null==l?void 0:l.isEqualRoot)&&void 0!==n?n:function(t,e){return t===e})(e.currentState,e.newState))return{didUpdate:!1};var f=i?i(e.newState):e.newState;return!e.forceUpdate&&(null!==(o=null==l?void 0:l.isEqual)&&void 0!==o?o:function(t,e){return t===e})(u,f)?{didUpdate:!1}:(s.partialUpdateSubscription(t.subscriptionId,{currentState:f}),a({state:f},{identifier:e.identifier}),{didUpdate:!0})},this.setState=function(t,e){var r=e.forceUpdate,o=e.identifier,i=s.state;if(r||i!==t){s.state=t;var a,u={forceUpdate:r,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=n(t))){e&&(t=e);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},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}}}}(s.subscribers.values());try{for(c.s();!(a=c.n()).done;){var l=a.value;s.executeSetStateForSubscriber(l,u)}}catch(t){c.e(t)}finally{c.f()}}},this.setMetadata=function(t){var e=(0,c.isFunction)(t)?t(s.metadata):t;s.metadata=e},this.getMetadata=function(){return s.metadata},this.getState=function(t,e,r){var n;if(!t)return s.state;var o=(0,c.isFunction)(e),i=o?t:void 0,a=o?e:t,u=null!==(n=o?r:e)&&void 0!==n?n:void 0,l=i?i(s.state):s.state;(null==u?void 0:u.skipFirst)||a(l);var f=(0,b.uniqueId)("gs:");return s.subscribe({subscriptionId:f,selector:i,getConfig:function(){return u},currentState:l,callback:function(t){var e=t.state;return a(e)}}),function(){s.subscribers.delete(f)}},this.getConfigCallbackParam=function(){var t=s.setMetadata,e=s.getMetadata,r=s.getState,n=s.actions;return{setMetadata:t,getMetadata:e,getState:r,setState:s.setStateWrapper,actions:n}},this.lastSubscriptionId=null,this.subscribe=function(t){var e=t.subscriptionId;return s.executeOnSubscribed(t),s.subscribers.set(e,t),s.lastSubscriptionId=e,function(){s.subscribers.delete(e)}},this.partialUpdateSubscription=function(t,e){var r=s.subscribers.get(t);(0,d.isRecord)(r)&&Object.assign(r,e)},this.executeOnSubscribed=function(t){var e,r=s.onSubscribed,n=null===(e=s.callbacks)||void 0===e?void 0:e.onSubscribed;if(r||n){var o=s.getConfigCallbackParam();null==r||r(o,t),null==n||n(o,t)}},this.getHook=function(){var t=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(s.wasDisposed)throw new Error("The global state was disposed");var r=(0,p.isArray)(e)?{dependencies:e}:null!=e?e:{},n=(0,u.useRef)({selector:t,config:r}),o=n.current.config.dependencies;n.current.selector=t,n.current.config=r;var i=(0,u.useMemo)((function(){var t=function(t){var e=n.current.selector;return(0,c.isFunction)(e)?e(t):t},e={subscriptionId:(0,b.uniqueId)("ss:"),currentState:t(s.state),selector:t,getConfig:function(){return n.current.config},callback:function(){throw new Error("Callback not set")}};return{subscribe:function(t){return e.callback=t,s.subscribe(e)},getSnapshot:function(){return e.currentState},subscription:e}}),[]),a=i.subscribe,l=i.getSnapshot,f=i.subscription;return(0,u.useSyncExternalStore)(a,l,l),[s.computeSelectedState({subscriptionRef:f,currentDependencies:o}),s.getStateOrchestrator(),s.metadata]};return t.stateControls=s.stateControls,t.createSelectorHook=s.createSelectorHook,t.createObservable=s.createObservable,t.removeSubscriptions=s.removeSubscriptions,t.dispose=s.dispose,t},this.computeSelectedState=function(t){var e,r=t.subscriptionRef,n=t.currentDependencies;if(!r.selector)return r.currentState;var o=(null!==(e=r.getConfig())&&void 0!==e?e:{}).dependencies;return n===o||(null==n?void 0:n.length)===(null==o?void 0:o.length)&&(0,p.shallowCompare)(n,o)||s.partialUpdateSubscription(r.subscriptionId,{currentState:r.selector(s.state)}),r.currentState},this.createSelectorHook=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.isEqualRoot,i=n.isEqual,a=n.name,u=r(s.stateControls(),3),l=u[0],f=u[1],h=u[2],d=l(),p=(null!=e?e:function(t){return t})(d),v=new t(p,{name:null!=a?a:(0,b.uniqueId)("sh:")}),g=r(v.stateControls(),2),y=g[0],m=g[1],S=l((function(t){if(!(null!=o?o:Object.is)(d,t)){d=t;var r=e(t);(null!=i?i:Object.is)(p,r)||(p=r,m(r))}}),{skipFirst:!0}),w=v.getHook(),O=function(t,e){return[r((0,c.isFunction)(t)?w(t,e):w(),1)[0],f,h()]};return O.stateControls=function(){return[y,f,h]},O.createSelectorHook=v.createSelectorHook,O.createObservable=s.createObservable.bind(O),O.removeSubscriptions=function(){S(),v.removeSubscriptions()},O.dispose=function(){O.removeSubscriptions(),v.dispose()},O},this.stateControls=function(){var t=s.getStateOrchestrator(),e=s.getMetadata;return[s.getState,t,e]},this.getStateOrchestrator=function(){return s.actions?s.actions:s.setStateWrapper},this.setStateWrapper=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.forceUpdate,i=n.identifier,a=s.state,u=(0,c.isFunction)(t)?t(a):t;if(o||s.state!==u){var l=s.setMetadata,f=s.getMetadata,h=s.getState,d=s.actions,p={setMetadata:l,getMetadata:f,setState:s.setState,getState:h,actions:d,previousState:a,state:u,identifier:i},v=s.computePreventStateChange,b=null===(e=s.callbacks)||void 0===e?void 0:e.computePreventStateChange;if((v||b)&&((null==v?void 0:v(p))||(null==b?void 0:b(p))))return;s.setState(u,{forceUpdate:o,identifier:i});var g=s.onStateChanged,y=null===(r=s.callbacks)||void 0===r?void 0:r.onStateChanged;(g||y)&&(null==g||g(p),null==y||y(p))}},this.getStoreActionsMap=function(){if(!(0,d.isRecord)(s.actionsConfig))return null;var t=s.actionsConfig,e=s.setMetadata,r=s.setStateWrapper,n=s.getState,o=s.getMetadata,i=Object.keys(t).reduce((function(s,u){var c,l,f;return Object.assign(s,(c={},f=function(){for(var a=t[u],s=arguments.length,c=new Array(s),l=0;l<s;l++)c[l]=arguments[l];var f=a.apply(i,c);return"function"!=typeof f&&(0,v.throwWrongKeyOnActionCollectionConfig)(u),f.call(i,{setState:r,getState:n,setMetadata:e,getMetadata:o,actions:i})},(l=a(l=u))in c?Object.defineProperty(c,l,{value:f,enumerable:!0,configurable:!0,writable:!0}):c[l]=f,c)),s}),{});return i},this.removeSubscriptions=function(){s.subscribers.clear()},this.dispose=function(){s.wasDisposed=!0,s.removeSubscriptions(),s._name="",s.actionsConfig=null,s.callbacks=null,s.metadata={},s.actions=null,s.state=Object.create(null)};var f=l.metadata,m=l.callbacks,S=l.actions,w=l.name;this.state=e,this._name=null!=w?w:(0,b.uniqueId)("gs:"),this.metadata=null!=f?f:{},this.callbacks=null!=m?m:null,this.actionsConfig=null!=S?S:null;var O="";y.default.isDevToolsPresent&&(O=(0,g.generateStackHash)(null!==(o=(new Error).stack)&&void 0!==o?o:"")),y.default.reportToDevTools(this,l,O),this.constructor!==t||this.initialize()},e=[{key:"createObservable",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.isEqualRoot,o=e.isEqual,i=e.name,a=r(this.stateControls(),1)[0],s=[],u=a(),l=(null!=t?t:function(t){return t})(a()),f=a((function(e){if(!(null!=n?n:Object.is)(u,e)){u=e;var r=t(e);(null!=o?o:Object.is)(l,r)||(l=r,s.forEach((function(t){return t()})))}}),{skipFirst:!0}),h=function(t,e,r){var n;if(!t)return l;var o=(0,c.isFunction)(e),i=o?t:void 0,a=o?e:t,u=null!==(n=o?r:e)&&void 0!==n?n:void 0,f=function(){return a(i?i(l):l)};(null==u?void 0:u.skipFirst)||f();var h=function(){f()};return s.push(h),function(){s.splice(s.indexOf(h),1)}};return h._name=null!=i?i:(0,b.uniqueId)("ob:"),h.createObservable=this.createObservable.bind(h),h.removeSubscriptions=function(){s.forEach((function(t){return t()})),s.length=0,f()},h.stateControls=function(){return[h]},h}}],e&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,a(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();t.GlobalStore=m,t.default=m})(),f})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./generateStackHash.js"),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(["./generateStackHash.js","./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("./generateStackHash.js"),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["./generateStackHash.js"],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,a,s)=>(()=>{"use strict";var u={45:e=>{e.exports=t},78:t=>{t.exports=e},155:t=>{t.exports=r},361:t=>{t.exports=n},487:t=>{t.exports=o},506:t=>{t.exports=i},673:t=>{t.exports=a},773:t=>{t.exports=s},811:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});var r=globalThis;r.isDevToolsPresent=Boolean(r.REACT_GLOBAL_STATE_HOOK_DEBUG),e.default=r}},c={};function l(t){var e=c[t];if(void 0!==e)return e.exports;var r=c[t]={exports:{}};return u[t](r,r.exports,l),r.exports}var f={};return(()=>{var t=f;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){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,s=[],u=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=i.call(r)).done)&&(s.push(n.value),s.length!==e);u=!0);}catch(t){c=!0,o=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw o}}return s}}(t,e)||n(t,e)||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.")}()}function n(t,e){if(t){if("string"==typeof t)return o(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)?o(t,e):void 0}}function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function i(){i=function(){return r};var t,r={},n=Object.prototype,o=n.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},u=s.iterator||"@@iterator",c=s.asyncIterator||"@@asyncIterator",l=s.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var o=e&&e.prototype instanceof m?e:m,i=Object.create(o.prototype),s=new A(n||[]);return a(i,"_invoke",{value:L(t,r,s)}),i}function d(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=h;var p="suspendedStart",v="suspendedYield",b="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(M([])));x&&x!==n&&o.call(x,u)&&(j=x);var k=w.prototype=m.prototype=Object.create(j);function C(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,r){function n(i,a,s,u){var c=d(t[i],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,s,u)}),(function(t){n("throw",t,s,u)})):r.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,u)}))}u(c.arg)}var i;a(this,"_invoke",{value:function(t,e){function o(){return new r((function(r,o){n(t,e,r,o)}))}return i=i?i.then(o,o):o()}})}function L(e,r,n){var o=p;return function(i,a){if(o===b)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=_(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===p)throw o=g,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=b;var c=d(e,r,n);if("normal"===c.type){if(o=n.done?g:v,c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=g,n.method="throw",n.arg=c.arg)}}}function _(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,_(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=d(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 q(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 I(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(q,this),this.reset(!0)}function M(r){if(r||""===r){var n=r[u];if(n)return n.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var i=-1,a=function e(){for(;++i<r.length;)if(o.call(r,i))return e.value=r[i],e.done=!1,e;return e.value=t,e.done=!0,e};return a.next=a}}throw new TypeError(e(r)+" is not iterable")}return S.prototype=w,a(k,"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(k),t},r.awrap=function(t){return{__await:t}},C(E.prototype),f(E.prototype,c,(function(){return this})),r.AsyncIterator=E,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new E(h(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},C(k),f(k,l,"Generator"),f(k,u,(function(){return this})),f(k,"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=M,A.prototype={constructor:A,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(I),!e)for(var r in this)"t"===r.charAt(0)&&o.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,o){return s.type="throw",s.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),c=o.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),I(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;I(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:M(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},r}function a(t){var r=function(t){if("object"!=e(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=e(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==e(r)?r:r+""}Object.defineProperty(t,"__esModule",{value:!0}),t.GlobalStore=void 0;var s,u=l(155),c=l(506),h=l(773),d=l(487),p=l(673),v=l(361),b=l(78),g=l(45),y=(s=l(811))&&s.__esModule?s:{default:s},m=function(){return t=function t(e){var o,s,l=this,f=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.wasDisposed=!1,this.actionsConfig=null,this.callbacks=null,this.actions=null,this.subscribers=new Map,this.initialize=function(){return t=l,e=void 0,r=i().mark((function t(){var e,r,n,o,a,s;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Object.keys(null!==(e=this.actionsConfig)&&void 0!==e?e:{}).length>0&&(this.actions=this.getStoreActionsMap()),n=this.onInit,o=null!==(r=this.callbacks)&&void 0!==r?r:{},a=o.onInit,n||a){t.next=6;break}return t.abrupt("return");case 6:s=this.getConfigCallbackParam(),null==n||n(s),(0,h.isNil)(a)||null==a||a(s);case 9: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,n,o,i=t.selector,a=t.callback,s=t.currentState,u=t.getConfig,c=null!==(r=null==u?void 0:u())&&void 0!==r?r:{};if(!e.forceUpdate&&(null!==(n=null==c?void 0:c.isEqualRoot)&&void 0!==n?n:function(t,e){return t===e})(e.currentState,e.newState))return{didUpdate:!1};var f=i?i(e.newState):e.newState;return!e.forceUpdate&&(null!==(o=null==c?void 0:c.isEqual)&&void 0!==o?o:function(t,e){return t===e})(s,f)?{didUpdate:!1}:(l.partialUpdateSubscription(t.subscriptionId,{currentState:f}),a({state:f},{identifier:e.identifier}),{didUpdate:!0})},this.setState=function(t,e){var r=e.forceUpdate,o=e.identifier,i=l.state;if(r||i!==t){l.state=t;var a,s={forceUpdate:r,newState:t,currentState:i,identifier:o},u=function(t){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=n(t))){e&&(t=e);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},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}}}}(l.subscribers.values());try{for(u.s();!(a=u.n()).done;){var c=a.value;l.executeSetStateForSubscriber(c,s)}}catch(t){u.e(t)}finally{u.f()}}},this.setMetadata=function(t){var e=(0,c.isFunction)(t)?t(l.metadata):t;l.metadata=e},this.getMetadata=function(){return l.metadata},this.getState=function(t,e,r){var n;if(!t)return l.state;var o=(0,c.isFunction)(e),i=o?t:void 0,a=o?e:t,s=null!==(n=o?r:e)&&void 0!==n?n:void 0,u=i?i(l.state):l.state;(null==s?void 0:s.skipFirst)||a(u);var f=(0,b.uniqueId)("gs:");return l.subscribe({subscriptionId:f,selector:i,getConfig:function(){return s},currentState:u,callback:function(t){var e=t.state;return a(e)}}),function(){l.subscribers.delete(f)}},this.getConfigCallbackParam=function(){var t=l.setMetadata,e=l.getMetadata,r=l.getState,n=l.actions;return{setMetadata:t,getMetadata:e,getState:r,setState:l.setStateWrapper,actions:n}},this.lastSubscriptionId=null,this.subscribe=function(t){var e=t.subscriptionId;return l.executeOnSubscribed(t),l.subscribers.set(e,t),l.lastSubscriptionId=e,function(){l.subscribers.delete(e)}},this.partialUpdateSubscription=function(t,e){var r=l.subscribers.get(t);(0,d.isRecord)(r)&&Object.assign(r,e)},this.executeOnSubscribed=function(t){var e,r=l.onSubscribed,n=null===(e=l.callbacks)||void 0===e?void 0:e.onSubscribed;if(r||n){var o=l.getConfigCallbackParam();null==r||r(o,t),null==n||n(o,t)}},this.getHook=function(){var t=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(l.wasDisposed)throw new Error("The global state was disposed");var r=(0,p.isArray)(e)?{dependencies:e}:null!=e?e:{},n=(0,u.useRef)({selector:t,config:r}),o=n.current.config.dependencies;n.current.selector=t,n.current.config=r;var i=(0,u.useMemo)((function(){var t=function(t){var e=n.current.selector;return(0,c.isFunction)(e)?e(t):t},e={subscriptionId:(0,b.uniqueId)("ss:"),currentState:t(l.state),selector:t,getConfig:function(){return n.current.config},callback:function(){throw new Error("Callback not set")}};return{subscribe:function(t){return e.callback=t,l.subscribe(e)},getSnapshot:function(){return e.currentState},subscription:e}}),[]),a=i.subscribe,s=i.getSnapshot,f=i.subscription;return(0,u.useSyncExternalStore)(a,s,s),[l.computeSelectedState({subscriptionRef:f,currentDependencies:o}),l.getStateOrchestrator(),l.metadata]};return t.stateControls=l.stateControls,t.createSelectorHook=l.createSelectorHook,t.createObservable=l.createObservable,t.removeSubscriptions=l.removeSubscriptions,t.dispose=l.dispose,t},this.computeSelectedState=function(t){var e,r=t.subscriptionRef,n=t.currentDependencies;if(!r.selector)return r.currentState;var o=(null!==(e=r.getConfig())&&void 0!==e?e:{}).dependencies;return n===o||(null==n?void 0:n.length)===(null==o?void 0:o.length)&&(0,p.shallowCompare)(n,o)||l.partialUpdateSubscription(r.subscriptionId,{currentState:r.selector(l.state)}),r.currentState},this.createSelectorHook=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.isEqualRoot,i=n.isEqual,a=n.name,s=r(l.stateControls(),3),u=s[0],f=s[1],h=s[2],d=u(),p=(null!=e?e:function(t){return t})(d),v=new t(p,{name:null!=a?a:(0,b.uniqueId)("sh:")}),g=r(v.stateControls(),2),y=g[0],m=g[1],S=u((function(t){if(!(null!=o?o:Object.is)(d,t)){d=t;var r=e(t);(null!=i?i:Object.is)(p,r)||(p=r,m(r))}}),{skipFirst:!0}),w=v.getHook(),j=function(t,e){return[r((0,c.isFunction)(t)?w(t,e):w(),1)[0],f,h()]};return j.stateControls=function(){return[y,f,h]},j.createSelectorHook=v.createSelectorHook,j.createObservable=l.createObservable.bind(j),j.removeSubscriptions=function(){S(),v.removeSubscriptions()},j.dispose=function(){j.removeSubscriptions(),v.dispose()},j},this.stateControls=function(){var t=l.getStateOrchestrator(),e=l.getMetadata;return[l.getState,t,e]},this.getStateOrchestrator=function(){return l.actions?l.actions:l.setStateWrapper},this.setStateWrapper=function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.forceUpdate,i=n.identifier,a=l.state,s=(0,c.isFunction)(t)?t(a):t;if(o||l.state!==s){var u=l.setMetadata,f=l.getMetadata,h=l.getState,d=l.actions,p={setMetadata:u,getMetadata:f,setState:l.setState,getState:h,actions:d,previousState:a,state:s,identifier:i},v=l.computePreventStateChange,b=null===(e=l.callbacks)||void 0===e?void 0:e.computePreventStateChange;if((v||b)&&((null==v?void 0:v(p))||(null==b?void 0:b(p))))return;l.setState(s,{forceUpdate:o,identifier:i});var g=l.onStateChanged,y=null===(r=l.callbacks)||void 0===r?void 0:r.onStateChanged;(g||y)&&(null==g||g(p),null==y||y(p))}},this.getStoreActionsMap=function(){if(!(0,d.isRecord)(l.actionsConfig))return null;var t=l.actionsConfig,e=l.setMetadata,r=l.setStateWrapper,n=l.getState,o=l.getMetadata,i=Object.keys(t).reduce((function(s,u){var c,l,f;return Object.assign(s,(c={},f=function(){for(var a=t[u],s=arguments.length,c=new Array(s),l=0;l<s;l++)c[l]=arguments[l];var f=a.apply(i,c);return"function"!=typeof f&&(0,v.throwWrongKeyOnActionCollectionConfig)(u),f.call(i,{setState:r,getState:n,setMetadata:e,getMetadata:o,actions:i})},(l=a(l=u))in c?Object.defineProperty(c,l,{value:f,enumerable:!0,configurable:!0,writable:!0}):c[l]=f,c)),s}),{});return i},this.removeSubscriptions=function(){l.subscribers.clear()},this.dispose=function(){l.wasDisposed=!0,l.removeSubscriptions(),l._name="",l.actionsConfig=null,l.callbacks=null,l.metadata={},l.actions=null,l.state=Object.create(null)};var m=f.metadata,S=f.callbacks,w=f.actions,j=f.name;if(this.state=e,this._name=null!=j?j:(0,b.uniqueId)("gs:"),this.metadata=null!=m?m:{},this.callbacks=null!=S?S:null,this.actionsConfig=null!=w?w:null,y.default.isDevToolsPresent){var O=(0,g.generateStackHash)(null!==(o=(new Error).stack)&&void 0!==o?o:"");null===(s=y.default.REACT_GLOBAL_STATE_HOOK_DEBUG)||void 0===s||s.call(y.default,this,f,O)}this.constructor!==t||this.initialize()},e=[{key:"createObservable",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.isEqualRoot,o=e.isEqual,i=e.name,a=r(this.stateControls(),1)[0],s=[],u=a(),l=(null!=t?t:function(t){return t})(a()),f=a((function(e){if(!(null!=n?n:Object.is)(u,e)){u=e;var r=t(e);(null!=o?o:Object.is)(l,r)||(l=r,s.forEach((function(t){return t()})))}}),{skipFirst:!0}),h=function(t,e,r){var n;if(!t)return l;var o=(0,c.isFunction)(e),i=o?t:void 0,a=o?e:t,u=null!==(n=o?r:e)&&void 0!==n?n:void 0,f=function(){return a(i?i(l):l)};(null==u?void 0:u.skipFirst)||f();var h=function(){f()};return s.push(h),function(){s.splice(s.indexOf(h),1)}};return h._name=null!=i?i:(0,b.uniqueId)("ob:"),h.createObservable=this.createObservable.bind(h),h.removeSubscriptions=function(){s.forEach((function(t){return t()})),s.length=0,f()},h.stateControls=function(){return[h]},h}}],e&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,a(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t;var t,e}();t.GlobalStore=m,t.default=m})(),f})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./generateStackHash.js"),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(["./generateStackHash.js","./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("./generateStackHash.js"),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["./generateStackHash.js"],t["./uniqueId.js"],t.react,t["./throwWrongKeyOnActionCollectionConfig.js"],t["./isRecord.js"],t["json-storage-formatter/isFunction"],t["./shallowCompare.js"],t["json-storage-formatter/isNil"]);
|
package/createContext.d.ts
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
|
-
import { type PropsWithChildren } from 'react';
|
|
2
|
-
import type { ActionCollectionConfig, StateSetter, ActionCollectionResult, StateGetter, BaseMetadata, MetadataSetter, GlobalStoreCallbacks, UseHookConfig, ObservableFragment, MetadataGetter } from './types';
|
|
3
|
-
export type
|
|
1
|
+
import { type PropsWithChildren, Context as ReactContext } from 'react';
|
|
2
|
+
import type { ActionCollectionConfig, StateHook, StateSetter, ActionCollectionResult, StateGetter, BaseMetadata, MetadataSetter, GlobalStoreCallbacks, UseHookConfig, ObservableFragment, MetadataGetter } from './types';
|
|
3
|
+
export type Context<State, Actions, Metadata extends BaseMetadata | unknown> = {
|
|
4
4
|
setMetadata: MetadataSetter<Metadata>;
|
|
5
5
|
setState: StateSetter<State>;
|
|
6
6
|
getState: StateGetter<State>;
|
|
7
7
|
getMetadata: () => Metadata;
|
|
8
|
-
actions:
|
|
8
|
+
actions: Actions;
|
|
9
9
|
};
|
|
10
|
-
export type ContextProvider<State, Metadata extends BaseMetadata | unknown> = React.FC<PropsWithChildren<{
|
|
10
|
+
export type ContextProvider<State, Actions, Metadata extends BaseMetadata | unknown> = React.FC<PropsWithChildren<{
|
|
11
11
|
value?: State | ((initialValue: State) => State);
|
|
12
|
-
|
|
13
|
-
}
|
|
12
|
+
__onCreate?: (context: Context<State, Actions, Metadata>) => void;
|
|
13
|
+
}>> & {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a provider wrapper which allows to capture the context value,
|
|
16
|
+
* useful for testing purposes.
|
|
17
|
+
*/
|
|
18
|
+
makeProviderWrapper: (options?: {
|
|
19
|
+
value?: State | ((initialValue: State) => State);
|
|
20
|
+
onCreated?: (context: Context<State, Actions, Metadata>) => void;
|
|
21
|
+
}) => {
|
|
22
|
+
wrapper: React.FC<PropsWithChildren<{
|
|
23
|
+
value?: State | ((initialValue: State) => State);
|
|
24
|
+
}>>;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the last captured context value.
|
|
27
|
+
*/
|
|
28
|
+
getContext: () => Context<State, Actions, Metadata>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
14
31
|
export type StateControlsHook<State, StateMutator, Metadata extends BaseMetadata | unknown> = () => Readonly<[
|
|
15
32
|
retriever: StateGetter<State>,
|
|
16
33
|
mutator: StateMutator,
|
|
@@ -48,7 +65,8 @@ export type HookExtensions<State, StateMutator, Metadata extends BaseMetadata |
|
|
|
48
65
|
export interface CreateContext {
|
|
49
66
|
<State>(value: State | (() => State)): [
|
|
50
67
|
ContextHook<State, StateSetter<State>, BaseMetadata>,
|
|
51
|
-
ContextProvider<State, BaseMetadata
|
|
68
|
+
ContextProvider<State, null, BaseMetadata>,
|
|
69
|
+
ReactContext<StateHook<State, StateSetter<State>, BaseMetadata> | null>
|
|
52
70
|
];
|
|
53
71
|
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(value: State | (() => State), args: {
|
|
54
72
|
name?: string;
|
|
@@ -57,7 +75,11 @@ export interface CreateContext {
|
|
|
57
75
|
onUnMount?: () => void;
|
|
58
76
|
};
|
|
59
77
|
actions?: ActionsConfig;
|
|
60
|
-
}): [
|
|
78
|
+
}): [
|
|
79
|
+
ContextHook<State, PublicStateMutator, Metadata>,
|
|
80
|
+
ContextProvider<State, PublicStateMutator, Metadata>,
|
|
81
|
+
ReactContext<StateHook<State, PublicStateMutator, Metadata> | null>
|
|
82
|
+
];
|
|
61
83
|
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata>>(value: State | (() => State), args: {
|
|
62
84
|
name?: string;
|
|
63
85
|
metadata?: Metadata | (() => Metadata);
|
|
@@ -67,7 +89,8 @@ export interface CreateContext {
|
|
|
67
89
|
actions: ActionsConfig;
|
|
68
90
|
}): [
|
|
69
91
|
ContextHook<State, ActionCollectionResult<State, Metadata, ActionsConfig>, Metadata>,
|
|
70
|
-
ContextProvider<State, Metadata
|
|
92
|
+
ContextProvider<State, ActionCollectionResult<State, Metadata, ActionsConfig>, Metadata>,
|
|
93
|
+
ReactContext<StateHook<State, ActionCollectionResult<State, Metadata, ActionsConfig>, Metadata> | null>
|
|
71
94
|
];
|
|
72
95
|
}
|
|
73
96
|
export declare const createContext: CreateContext;
|
package/createContext.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=(e,t,r,o)=>(()=>{"use strict";var n={155:t=>{t.exports=e},506:e=>{e.exports=t},639:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var o=r(155),n=r(778),
|
|
1
|
+
var e,t;e=this,t=(e,t,r,o)=>(()=>{"use strict";var n={155:t=>{t.exports=e},506:e=>{e.exports=t},639:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var o=r(155),n=r(778),a=r(506),i=r(773);t.createContext=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=(0,o.createContext)(null),u=function(u){var s=u.children,l=u.value,c=u.__onCreate,f=(0,o.useMemo)((function(){var r,o=function(){return(0,a.isFunction)(e)?e():e},u=(0,i.isNil)(l)?o():(0,a.isFunction)(l)?l(o()):l,s=new n.GlobalStore(u,Object.assign(Object.assign({},t),{metadata:null!==(r=(0,a.isFunction)(t.metadata)?t.metadata():t.metadata)&&void 0!==r?r:{}})),c=s.getHook();return{store:s,parentHook:c}}),[]),v=f.store,d=f.parentHook;return(0,o.useEffect)((function(){return function(){var e,t,r,o;null===(t=null===(e=v.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e),null===(o=(r=v).__onUnMountContext)||void 0===o||o.call(r,v,d),v.dispose()}}),[v,d]),null==c||c(v.getConfigCallbackParam()),(0,o.createElement)(r.Provider,{value:d},s)},s=function(){var e=(0,o.useContext)(r);if(!e)throw new Error("ContextHook must be used within a ContextProvider");return e()};s.createSelectorHook=function(e,t){return function(){var n=(0,o.useContext)(r);if((0,i.isNil)(n))throw new Error("SelectorHook must be used within a ContextProvider");var a=(0,o.useRef)(e);a.current=e;var u=(0,o.useMemo)((function(){return(0,i.isNil)(e)?n:n.createSelectorHook((function(){return a.current.apply(a,arguments)}),t)}),[n]);return(0,o.useEffect)((function(){return function(){null==u||u.dispose()}}),[u]),u.apply(void 0,arguments)}};var l=function(){var e=(0,o.useContext)(r);if((0,i.isNil)(e))throw new Error("useStateControls must be used within a ContextProvider");return e.stateControls()},c=function(e,t){var n=(0,o.useContext)(r);if((0,i.isNil)(n))throw new Error("useObservableBuilder must be used within a ContextProvider");return n.createObservable(e,t)};return s.stateControls=function(){return[l,c]},u.makeProviderWrapper=function(e){var t=null;return{wrapper:function(r){var n=r.children;return(0,o.createElement)(u,{value:null==e?void 0:e.value,__onCreate:function(r){var o;t=r,null===(o=null==e?void 0:e.onCreated)||void 0===o||o.call(e,r)}},n)},getContext:function(){return t}}},[s,u,r]},t.default=t.createContext},773:e=>{e.exports=r},778:e=>{e.exports=o}},a={};return function e(t){var r=a[t];if(void 0!==r)return r.exports;var o=a[t]={exports:{}};return n[t](o,o.exports,e),o.exports}(639)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["react","json-storage-formatter/isFunction","json-storage-formatter/isNil","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e.react,e["json-storage-formatter/isFunction"],e["json-storage-formatter/isNil"],e["./GlobalStore.js"]);
|
package/index.d.ts
CHANGED
|
@@ -8,5 +8,5 @@ export { uniqueId } from './uniqueId';
|
|
|
8
8
|
export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
|
|
9
9
|
export { isRecord } from './isRecord';
|
|
10
10
|
export { useStableState } from './useStableState';
|
|
11
|
-
export {
|
|
11
|
+
export { Context, ContextProvider, ContextHook, CreateContext, createContext } from './createContext';
|
|
12
12
|
export { generateStackHash } from './generateStackHash';
|
package/package.json
CHANGED