state-decorator
Version:
React state management library
3 lines (2 loc) • 12.9 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("./impl"),t=require("./development");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=n(e),o=function(){return o=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var u in r=arguments[t])Object.prototype.hasOwnProperty.call(r,u)&&(e[u]=r[u]);return e},o.apply(this,arguments)};function c(e){var r={exports:{}};return e(r,r.exports),r.exports}"function"==typeof SuppressedError&&SuppressedError;var i="function"==typeof Object.is?Object.is:function(e,r){return e===r&&(0!==e||1/e==1/r)||e!=e&&r!=r},a=u.default.useState,l=u.default.useEffect,f=u.default.useLayoutEffect,s=u.default.useDebugValue;function d(e){var r=e.getSnapshot;e=e.value;try{var t=r();return!i(e,t)}catch(e){return!0}}var p="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,r){return r()}:function(e,r){var t=r(),n=a({inst:{value:t,getSnapshot:r}}),u=n[0].inst,o=n[1];return f((function(){u.value=t,u.getSnapshot=r,d(u)&&o({inst:u})}),[e,t,r]),l((function(){return d(u)&&o({inst:u}),e((function(){d(u)&&o({inst:u})}))}),[e]),s(t),t},v={useSyncExternalStore:void 0!==u.default.useSyncExternalStore?u.default.useSyncExternalStore:p},O=c((function(e,r){"production"!==process.env.NODE_ENV&&function(){function e(e){var r=e.getSnapshot;e=e.value;try{var t=r();return!n(e,t)}catch(e){return!0}}"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var t=u.default,n="function"==typeof Object.is?Object.is:function(e,r){return e===r&&(0!==e||1/e==1/r)||e!=e&&r!=r},o=t.useState,c=t.useEffect,i=t.useLayoutEffect,a=t.useDebugValue,l=!1,f=!1,s="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,r){return r()}:function(r,u){l||void 0===t.startTransition||(l=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var s=u();if(!f){var d=u();n(s,d)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),f=!0)}var p=(d=o({inst:{value:s,getSnapshot:u}}))[0].inst,v=d[1];return i((function(){p.value=s,p.getSnapshot=u,e(p)&&v({inst:p})}),[r,s,u]),c((function(){return e(p)&&v({inst:p}),r((function(){e(p)&&v({inst:p})}))}),[r]),a(s),s};r.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:s,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()})),E=v,_=O,S=c((function(e){"production"===process.env.NODE_ENV?e.exports=E:e.exports=_})),y=S;var g,L="function"==typeof Object.is?Object.is:function(e,r){return e===r&&(0!==e||1/e==1/r)||e!=e&&r!=r},b=S.useSyncExternalStore,A=u.default.useRef,h=u.default.useEffect,R=u.default.useMemo,x=u.default.useDebugValue,D={useSyncExternalStoreWithSelector:function(e,r,t,n,u){var o=A(null);if(null===o.current){var c={hasValue:!1,value:null};o.current=c}else c=o.current;o=R((function(){function e(e){if(!a){if(a=!0,o=e,e=n(e),void 0!==u&&c.hasValue){var r=c.value;if(u(r,e))return i=r}return i=e}if(r=i,L(o,e))return r;var t=n(e);return void 0!==u&&u(r,t)?(o=e,r):(o=e,i=t)}var o,i,a=!1,l=void 0===t?null:t;return[function(){return e(r())},null===l?void 0:function(){return e(l())}]}),[r,t,n,u]);var i=b(e,o[0],o[1]);return h((function(){c.hasValue=!0,c.value=i}),[i]),x(i),i}},M=c((function(e,r){"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var e=u.default,t=S,n="function"==typeof Object.is?Object.is:function(e,r){return e===r&&(0!==e||1/e==1/r)||e!=e&&r!=r},o=t.useSyncExternalStore,c=e.useRef,i=e.useEffect,a=e.useMemo,l=e.useDebugValue;r.useSyncExternalStoreWithSelector=function(e,r,t,u,f){var s=c(null);if(null===s.current){var d={hasValue:!1,value:null};s.current=d}else d=s.current;s=a((function(){function e(e){if(!i){if(i=!0,o=e,e=u(e),void 0!==f&&d.hasValue){var r=d.value;if(f(r,e))return c=r}return c=e}if(r=c,n(o,e))return r;var t=u(e);return void 0!==f&&f(r,t)?(o=e,r):(o=e,c=t)}var o,c,i=!1,a=void 0===t?null:t;return[function(){return e(r())},null===a?void 0:function(){return e(a())}]}),[r,t,u,f]);var p=o(e,s[0],s[1]);return i((function(){d.hasValue=!0,d.value=p}),[p]),l(p),p},"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()})),m=D,T=M,j=c((function(e){"production"===process.env.NODE_ENV?e.exports=m:e.exports=T}));exports.ConflictPolicy=void 0,(g=exports.ConflictPolicy||(exports.ConflictPolicy={})).REJECT="reject",g.IGNORE="ignore",g.KEEP_LAST="keepLast",g.KEEP_ALL="keepAll",g.PARALLEL="parallel",g.REUSE="reuse",g.ABORT="abort";var P=j.useSyncExternalStoreWithSelector,C=y.useSyncExternalStore;function V(e){var n=r.createRef(),u=r.createRef(),c=r.createRef(),i=r.createRef(),a=r.createRef(),l=r.createRef(),f=r.createRef(),s=r.createRef(),d=r.createRef(),p=r.createRef(),v=r.createRef(),O=r.createRef(!1),E=r.createRef(!1),_=r.createRef(),S=r.createRef([]),y=e.actions,g=e.middlewares,L=void 0===g?[]:g,b=function(e,r){var t={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&r.indexOf(n)<0&&(t[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(n=Object.getOwnPropertySymbols(e);u<n.length;u++)r.indexOf(n[u])<0&&Object.prototype.propertyIsEnumerable.call(e,n[u])&&(t[n[u]]=e[n[u]])}return t}(e,["actions","middlewares"]),A=r.isFuncConfig(e)?e.getInitialState:function(){return e.initialState},h={},R=0;function x(){_.current=o(o(o(o(o({},c.current),i.current),n.current),u.current.state),{isLoading:C,abortAction:V,loading:s.current,loadingMap:f.current,loadingParallelMap:l.current,errorMap:w(),errorParallelMap:d.current}),E.current=!1,Object.keys(h).forEach((function(e){var r;null===(r=h[e])||void 0===r||r.call(h)}))}function D(e){n.current=e,r.computeDerivedValues(n,c,u,b),x()}function M(e){l.current=e,f.current=function(){if(O.current){var e=l.current;return Object.keys(e).reduce((function(r,t){var n=e[t];return r[t]=null!=Object.keys(n).find((function(e){return!!n[e]})),r}),{})}return null}(),s.current=function(){if(O.current){var e=l.current;return null!=Object.keys(e).find((function(r){var t=e[r];return Object.keys(t).find((function(e){return!!t[e]}))}))}return null}()}function m(o){var l;if(!O.current){c.current=o,O.current=!0,n.current=A(o);var f=new Set(null==b?void 0:b.initialActionsMarkedLoading),s=Object.keys(y).reduce((function(e,t){var n;return e[t]=f.has(t)?((n={})[r.DEFAULT_PROMISE_ID]=!0,n):{},e}),{});M(s),d.current=Object.keys(y).reduce((function(e,r){return e[r]={},e}),{}),a.current={},p.current={},v.current={},u.current={state:null,deps:{}},(null==b?void 0:b.derivedState)&&r.fixDerivedDeps(b),S.current=[];var E={options:b,get state(){return n.current},actions:y,setState:D};e.logEnabled&&t.logDetailedEffects&&L.push(t.logDetailedEffects());var _=function(e){var r,t=e();S.current.push(t),null===(r=t.init)||void 0===r||r.call(t,E)};null===(l=r.globalConfig.defaultMiddlewares)||void 0===l||l.forEach(_),null==L||L.forEach(_),r.computeDerivedValues(n,c,u,b),r.onPropChange(n,u,c,null,i,b,A,j,!0,!1),(null==b?void 0:b.onMount)&&b.onMount(r.buildOnMountInvocationContext(n,u,c,i)),x()}}function T(e,t){void 0===t&&(t=r.DEFAULT_PROMISE_ID);var n=d.current[e];(null==n?void 0:n[t])&&delete n[t],x()}function j(e,t,o,i,a,f,s,d,p){void 0===s&&(s=!1),void 0===d&&(d=!1),void 0===p&&(p=!1);var v=!1;if(!O.current)return x;var E=t,_=!!a&&r.isLoadingImpl(E||l.current,[o,null==f?void 0:f.promiseId]),y=r.runMiddlewares(S.current,n.current,e,_,o,i,a,f),g=y.newState,L=y.newLoading;if(null!=g&&(v=!0,n.current=g),_!==L&&(E=r.buildLoadingMap(E,o,(null==f?void 0:f.promiseId)||r.DEFAULT_PROMISE_ID,L),0===Object.keys(E).length&&(E=void 0)),null!=E&&(v=!0,M(E)),v)r.computeDerivedValues(n,c,u,b),d||(n.current=g||n.current,p||x());else if(s){!r.computeDerivedValues(n,c,u,b)||d||p||x()}return x}var P=Object.keys(y);function C(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return r.isLoadingImpl.apply(void 0,function(e,r,t){if(t||2===arguments.length)for(var n,u=0,o=r.length;u<o;u++)!n&&u in r||(n||(n=Array.prototype.slice.call(r,0,u)),n[u]=r[u]);return e.concat(n||Array.prototype.slice.call(r))}([l.current],e,!1))}function V(e,t){var n,u;void 0===t&&(t=r.DEFAULT_PROMISE_ID);var o=null===(u=null===(n=p.current[e])||void 0===n?void 0:n[t])||void 0===u?void 0:u.abortController,c=!1;return o&&(o.abort(),c=!0),c}function w(){return O.current?r.buildErrorMap(d.current):null}return i.current=P.reduce((function(e,t){var o=y[t];return r.isSimpleSyncAction(o)?e[t]=r.decorateSimpleSyncAction(t,o,n,u,c,O,b,j):r.isAsyncAction(o)?e[t]=r.decorateAsyncAction({actionName:t,stateRef:n,derivedStateRef:u,propsRef:c,loadingParallelMapRef:l,errorMapRef:d,actionsRef:i,promisesRef:p,conflictActionsRef:v,initializedRef:O,timeoutRef:a,needNotifyListenersRef:E,options:b,setState:j,clearError:T,action:r.computeAsyncActionInput(o)}):r.isSyncAction(o)&&(e[t]=r.decorateSyncAction(t,o,n,u,c,i,O,a,E,b,j,T)),e}),{}),{getConfig:function(){return o({},e)},setProps:function(e){var t=c.current;c.current=e,O.current?r.onPropChange(n,u,c,t,i,b,A,j,!1,!1):m(e)},addStateListener:function(e){var r="".concat(R++);return h[r]=e,function(){delete h[r]}},init:m,destroy:function(){var e;if(O.current){var t=Object.keys(y).reduce((function(e,t){var n=y[t];if(r.isAsyncAction(n)&&n.abortable){var u=l.current[t],o=Object.keys(u).reduce((function(e,r){return u[r]&&(V(t,r),e.push(r)),e}),[]);o.length>0&&(e[t]=o)}return e}),{});null===(e=null==b?void 0:b.onUnmount)||void 0===e||e.call(b,r.buildOnUnMountInvocationContext(n,u,c,t)),Object.keys(a.current).forEach((function(e){clearTimeout(a.current[e])})),c.current=null,n.current=null,l.current=null,f.current=null,s.current=null,d.current=null,p.current=null,v.current=null,u.current=null,_.current=null,a.current=null,h={},O.current=!1,S.current.forEach((function(e){var r;null===(r=e.destroy)||void 0===r||r.call(e)})),S.current=[]}},isLoading:C,abortAction:V,clearError:T,getSnapshot:function(){return _.current},invokeOnMountDeferred:function(){r.onPropChange(n,u,c,null,i,b,A,j,!0,!0),(null==b?void 0:b.onMountDeferred)&&b.onMountDeferred(r.buildOnMountInvocationContext(n,u,c,i))},get actions(){return i.current},get state(){return O.current?o(o({},n.current),u.current.state):null},get loading(){return s.current},get loadingMap(){return f.current},get errorMap(){return w()},get loadingParallelMap(){return O.current?l.current:null},get errorParallelMap(){return O.current?d.current:null}}}function w(t,n){var u=e.useMemo((function(){return n instanceof Function?n:function(e){return k(e,n)}}),[n]);return P(t.addStateListener,t.getSnapshot,void 0,u,r.globalConfig.comparator)}function I(r,t,n){void 0===n&&(n=!0);var u=e.useRef();return null==u.current&&(u.current=V(r)),e.useEffect((function(){return u.current.invokeOnMountDeferred(),function(){var e;null===(e=u.current)||void 0===e||e.destroy()}}),[]),function(r,t,n){void 0===t&&(t=null),void 0===n&&(n=!0);var u=e.useReducer((function(e){return e>100?0:e+1}),0)[1],o=e.useRef(r);return r.setProps(t),e.useLayoutEffect((function(){r.init(t)}),[]),e.useLayoutEffect((function(){if(n)return o.current.addStateListener((function(){u()}))}),[n]),r}(u.current,t,n)}function k(e,r){return r.reduce((function(r,t){return e.hasOwnProperty(t)&&(r[t]=e[t]),r}),{})}Object.defineProperty(exports,"EffectError",{enumerable:!0,get:function(){return r.EffectError}}),Object.defineProperty(exports,"ParallelActionError",{enumerable:!0,get:function(){return r.ParallelActionError}}),Object.defineProperty(exports,"setGlobalConfig",{enumerable:!0,get:function(){return r.setGlobalConfig}}),exports.createStore=V,exports.default=I,exports.pick=k,exports.slice=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return function(r){return k(r,e)}},exports.useBindStore=function(r,t){return r.setProps(t),C(r.addStateListener,r.getSnapshot),e.useLayoutEffect((function(){r.init(t)}),[]),e.useEffect((function(){r.invokeOnMountDeferred()}),[r]),r},exports.useLocalStore=I,exports.useStore=function(e){return C(e.addStateListener,e.getSnapshot),e},exports.useStoreContextSlice=function(r,t){return w(e.useContext(r),t)},exports.useStoreSlice=w;
//# sourceMappingURL=index.js.map