UNPKG

state-decorator

Version:
3 lines (2 loc) 12.1 kB
import r,{useMemo as e,useContext as n,useRef as t,useEffect as u,useLayoutEffect as o,useReducer as c}from"react";import{createRef as i,isFuncConfig as a,isSimpleSyncAction as l,decorateSimpleSyncAction as f,isAsyncAction as s,decorateAsyncAction as d,computeAsyncActionInput as v,isSyncAction as p,decorateSyncAction as O,globalConfig as _,onPropChange as E,DEFAULT_PROMISE_ID as S,fixDerivedDeps as y,computeDerivedValues as g,buildOnMountInvocationContext as L,buildOnUnMountInvocationContext as h,isLoadingImpl as b,runMiddlewares as A,buildLoadingMap as m,buildErrorMap as R}from"./impl";export{EffectError,ParallelActionError,setGlobalConfig}from"./impl";import{logDetailedEffects as T}from"./development";var M=function(){return M=Object.assign||function(r){for(var e,n=1,t=arguments.length;n<t;n++)for(var u in e=arguments[n])Object.prototype.hasOwnProperty.call(e,u)&&(r[u]=e[u]);return r},M.apply(this,arguments)};function j(r){var e={exports:{}};return r(e,e.exports),e.exports}"function"==typeof SuppressedError&&SuppressedError;var w="function"==typeof Object.is?Object.is:function(r,e){return r===e&&(0!==r||1/r==1/e)||r!=r&&e!=e},V=r.useState,x=r.useEffect,D=r.useLayoutEffect,k=r.useDebugValue;function P(r){var e=r.getSnapshot;r=r.value;try{var n=e();return!w(r,n)}catch(r){return!0}}var C="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(r,e){return e()}:function(r,e){var n=e(),t=V({inst:{value:n,getSnapshot:e}}),u=t[0].inst,o=t[1];return D((function(){u.value=n,u.getSnapshot=e,P(u)&&o({inst:u})}),[r,n,e]),x((function(){return P(u)&&o({inst:u}),r((function(){P(u)&&o({inst:u})}))}),[r]),k(n),n},G={useSyncExternalStore:void 0!==r.useSyncExternalStore?r.useSyncExternalStore:C},K=j((function(e,n){"production"!==process.env.NODE_ENV&&function(){function e(r){var e=r.getSnapshot;r=r.value;try{var n=e();return!u(r,n)}catch(r){return!0}}"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var t=r,u="function"==typeof Object.is?Object.is:function(r,e){return r===e&&(0!==r||1/r==1/e)||r!=r&&e!=e},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(r,e){return e()}:function(r,n){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=n();if(!f){var d=n();u(s,d)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),f=!0)}var v=(d=o({inst:{value:s,getSnapshot:n}}))[0].inst,p=d[1];return i((function(){v.value=s,v.getSnapshot=n,e(v)&&p({inst:v})}),[r,s,n]),c((function(){return e(v)&&p({inst:v}),r((function(){e(v)&&p({inst:v})}))}),[r]),a(s),s};n.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())}()})),B=G,I=K,H=j((function(r){"production"===process.env.NODE_ENV?r.exports=B:r.exports=I})),N=H;var U,W="function"==typeof Object.is?Object.is:function(r,e){return r===e&&(0!==r||1/r==1/e)||r!=r&&e!=e},z=H.useSyncExternalStore,F=r.useRef,J=r.useEffect,Y=r.useMemo,q=r.useDebugValue,Q={useSyncExternalStoreWithSelector:function(r,e,n,t,u){var o=F(null);if(null===o.current){var c={hasValue:!1,value:null};o.current=c}else c=o.current;o=Y((function(){function r(r){if(!a){if(a=!0,o=r,r=t(r),void 0!==u&&c.hasValue){var e=c.value;if(u(e,r))return i=e}return i=r}if(e=i,W(o,r))return e;var n=t(r);return void 0!==u&&u(e,n)?(o=r,e):(o=r,i=n)}var o,i,a=!1,l=void 0===n?null:n;return[function(){return r(e())},null===l?void 0:function(){return r(l())}]}),[e,n,t,u]);var i=z(r,o[0],o[1]);return J((function(){c.hasValue=!0,c.value=i}),[i]),q(i),i}},X=j((function(e,n){"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=r,t=H,u="function"==typeof Object.is?Object.is:function(r,e){return r===e&&(0!==r||1/r==1/e)||r!=r&&e!=e},o=t.useSyncExternalStore,c=e.useRef,i=e.useEffect,a=e.useMemo,l=e.useDebugValue;n.useSyncExternalStoreWithSelector=function(r,e,n,t,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 r(r){if(!i){if(i=!0,o=r,r=t(r),void 0!==f&&d.hasValue){var e=d.value;if(f(e,r))return c=e}return c=r}if(e=c,u(o,r))return e;var n=t(r);return void 0!==f&&f(e,n)?(o=r,e):(o=r,c=n)}var o,c,i=!1,a=void 0===n?null:n;return[function(){return r(e())},null===a?void 0:function(){return r(a())}]}),[e,n,t,f]);var v=o(r,s[0],s[1]);return i((function(){d.hasValue=!0,d.value=v}),[v]),l(v),v},"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()})),Z=Q,$=X,rr=j((function(r){"production"===process.env.NODE_ENV?r.exports=Z:r.exports=$}));!function(r){r.REJECT="reject",r.IGNORE="ignore",r.KEEP_LAST="keepLast",r.KEEP_ALL="keepAll",r.PARALLEL="parallel",r.REUSE="reuse",r.ABORT="abort"}(U||(U={}));var er=rr.useSyncExternalStoreWithSelector,nr=N.useSyncExternalStore;function tr(r){var e=i(),n=i(),t=i(),u=i(),o=i(),c=i(),j=i(),w=i(),V=i(),x=i(),D=i(),k=i(!1),P=i(!1),C=i(),G=i([]),K=r.actions,B=r.middlewares,I=void 0===B?[]:B,H=function(r,e){var n={};for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&e.indexOf(t)<0&&(n[t]=r[t]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(t=Object.getOwnPropertySymbols(r);u<t.length;u++)e.indexOf(t[u])<0&&Object.prototype.propertyIsEnumerable.call(r,t[u])&&(n[t[u]]=r[t[u]])}return n}(r,["actions","middlewares"]),N=a(r)?r.getInitialState:function(){return r.initialState},U={},W=0;function z(){C.current=M(M(M(M(M({},t.current),u.current),e.current),n.current.state),{isLoading:Z,abortAction:$,loading:w.current,loadingMap:j.current,loadingParallelMap:c.current,errorMap:rr(),errorParallelMap:V.current}),P.current=!1,Object.keys(U).forEach((function(r){var e;null===(e=U[r])||void 0===e||e.call(U)}))}function F(r){e.current=r,g(e,t,n,H),z()}function J(r){c.current=r,j.current=function(){if(k.current){var r=c.current;return Object.keys(r).reduce((function(e,n){var t=r[n];return e[n]=null!=Object.keys(t).find((function(r){return!!t[r]})),e}),{})}return null}(),w.current=function(){if(k.current){var r=c.current;return null!=Object.keys(r).find((function(e){var n=r[e];return Object.keys(n).find((function(r){return!!n[r]}))}))}return null}()}function Y(c){var i;if(!k.current){t.current=c,k.current=!0,e.current=N(c);var a=new Set(null==H?void 0:H.initialActionsMarkedLoading),l=Object.keys(K).reduce((function(r,e){var n;return r[e]=a.has(e)?((n={})[S]=!0,n):{},r}),{});J(l),V.current=Object.keys(K).reduce((function(r,e){return r[e]={},r}),{}),o.current={},x.current={},D.current={},n.current={state:null,deps:{}},(null==H?void 0:H.derivedState)&&y(H),G.current=[];var f={options:H,get state(){return e.current},actions:K,setState:F};r.logEnabled&&T&&I.push(T());var s=function(r){var e,n=r();G.current.push(n),null===(e=n.init)||void 0===e||e.call(n,f)};null===(i=_.defaultMiddlewares)||void 0===i||i.forEach(s),null==I||I.forEach(s),g(e,t,n,H),E(e,n,t,null,u,H,N,Q,!0,!1),(null==H?void 0:H.onMount)&&H.onMount(L(e,n,t,u)),z()}}function q(r,e){void 0===e&&(e=S);var n=V.current[r];(null==n?void 0:n[e])&&delete n[e],z()}function Q(r,u,o,i,a,l,f,s,d){void 0===f&&(f=!1),void 0===s&&(s=!1),void 0===d&&(d=!1);var v=!1;if(!k.current)return z;var p=u,O=!!a&&b(p||c.current,[o,null==l?void 0:l.promiseId]),_=A(G.current,e.current,r,O,o,i,a,l),E=_.newState,y=_.newLoading;if(null!=E&&(v=!0,e.current=E),O!==y&&(p=m(p,o,(null==l?void 0:l.promiseId)||S,y),0===Object.keys(p).length&&(p=void 0)),null!=p&&(v=!0,J(p)),v)g(e,t,n,H),s||(e.current=E||e.current,d||z());else if(f){!g(e,t,n,H)||s||d||z()}return z}var X=Object.keys(K);function Z(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return b.apply(void 0,function(r,e,n){if(n||2===arguments.length)for(var t,u=0,o=e.length;u<o;u++)!t&&u in e||(t||(t=Array.prototype.slice.call(e,0,u)),t[u]=e[u]);return r.concat(t||Array.prototype.slice.call(e))}([c.current],r,!1))}function $(r,e){var n,t;void 0===e&&(e=S);var u=null===(t=null===(n=x.current[r])||void 0===n?void 0:n[e])||void 0===t?void 0:t.abortController,o=!1;return u&&(u.abort(),o=!0),o}function rr(){return k.current?R(V.current):null}return u.current=X.reduce((function(r,i){var a=K[i];return l(a)?r[i]=f(i,a,e,n,t,k,H,Q):s(a)?r[i]=d({actionName:i,stateRef:e,derivedStateRef:n,propsRef:t,loadingParallelMapRef:c,errorMapRef:V,actionsRef:u,promisesRef:x,conflictActionsRef:D,initializedRef:k,timeoutRef:o,needNotifyListenersRef:P,options:H,setState:Q,clearError:q,action:v(a)}):p(a)&&(r[i]=O(i,a,e,n,t,u,k,o,P,H,Q,q)),r}),{}),{getConfig:function(){return M({},r)},setProps:function(r){var o=t.current;t.current=r,k.current?E(e,n,t,o,u,H,N,Q,!1,!1):Y(r)},addStateListener:function(r){var e="".concat(W++);return U[e]=r,function(){delete U[e]}},init:Y,destroy:function(){var r;if(k.current){var u=Object.keys(K).reduce((function(r,e){var n=K[e];if(s(n)&&n.abortable){var t=c.current[e],u=Object.keys(t).reduce((function(r,n){return t[n]&&($(e,n),r.push(n)),r}),[]);u.length>0&&(r[e]=u)}return r}),{});null===(r=null==H?void 0:H.onUnmount)||void 0===r||r.call(H,h(e,n,t,u)),Object.keys(o.current).forEach((function(r){clearTimeout(o.current[r])})),t.current=null,e.current=null,c.current=null,j.current=null,w.current=null,V.current=null,x.current=null,D.current=null,n.current=null,C.current=null,o.current=null,U={},k.current=!1,G.current.forEach((function(r){var e;null===(e=r.destroy)||void 0===e||e.call(r)})),G.current=[]}},isLoading:Z,abortAction:$,clearError:q,getSnapshot:function(){return C.current},invokeOnMountDeferred:function(){E(e,n,t,null,u,H,N,Q,!0,!0),(null==H?void 0:H.onMountDeferred)&&H.onMountDeferred(L(e,n,t,u))},get actions(){return u.current},get state(){return k.current?M(M({},e.current),n.current.state):null},get loading(){return w.current},get loadingMap(){return j.current},get errorMap(){return rr()},get loadingParallelMap(){return k.current?c.current:null},get errorParallelMap(){return k.current?V.current:null}}}function ur(r,n){var t=e((function(){return n instanceof Function?n:function(r){return lr(r,n)}}),[n]);return er(r.addStateListener,r.getSnapshot,void 0,t,_.comparator)}function or(r){return nr(r.addStateListener,r.getSnapshot),r}function cr(r,e){return ur(n(r),e)}function ir(r,e,n){void 0===n&&(n=!0);var i=t();return null==i.current&&(i.current=tr(r)),u((function(){return i.current.invokeOnMountDeferred(),function(){var r;null===(r=i.current)||void 0===r||r.destroy()}}),[]),function(r,e,n){void 0===e&&(e=null),void 0===n&&(n=!0);var u=c((function(r){return r>100?0:r+1}),0)[1],i=t(r);return r.setProps(e),o((function(){r.init(e)}),[]),o((function(){if(n)return i.current.addStateListener((function(){u()}))}),[n]),r}(i.current,e,n)}function ar(r,e){return r.setProps(e),nr(r.addStateListener,r.getSnapshot),o((function(){r.init(e)}),[]),u((function(){r.invokeOnMountDeferred()}),[r]),r}function lr(r,e){return e.reduce((function(e,n){return r.hasOwnProperty(n)&&(e[n]=r[n]),e}),{})}function fr(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return function(e){return lr(e,r)}}export{U as ConflictPolicy,tr as createStore,ir as default,lr as pick,fr as slice,ar as useBindStore,ir as useLocalStore,or as useStore,cr as useStoreContextSlice,ur as useStoreSlice}; //# sourceMappingURL=index.js.map