state-decorator
Version:
React state management library
3 lines (2 loc) • 17.1 kB
JavaScript
var e=function(r,t){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])},e(r,t)};function r(r,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=r}e(r,t),r.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var t=function(){return t=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e},t.apply(this,arguments)};function n(e,r,t,n){return new(t||(t=Promise))((function(o,i){function u(e){try{a(n.next(e))}catch(e){i(e)}}function c(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){var r;e.done?o(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(u,c)}a((n=n.apply(e,r||[])).next())}))}function o(e,r){var t,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(t=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,n=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=r.call(e,u)}catch(e){c=[6,e],n=0}finally{t=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function i(e,r,t){if(t||2===arguments.length)for(var n,o=0,i=r.length;o<i;o++)!n&&o in r||(n||(n=Array.prototype.slice.call(r,0,o)),n[o]=r[o]);return e.concat(n||Array.prototype.slice.call(r))}"function"==typeof SuppressedError&&SuppressedError;var u,c=function(){function e(e){this.keys=e,this.adj={}}return e.prototype.isCyclicUtil=function(e,r,t){var n;if(t.has(e))return!0;if(r.has(e))return!1;r.add(e),t.add(e);for(var o=0,i=null!==(n=this.adj[e])&&void 0!==n?n:[];o<i.length;o++){var u=i[o];if(this.isCyclicUtil(u,r,t))return!0}return t.delete(e),!1},e.prototype.setEdges=function(e,r){this.adj[e]=r},e.prototype.isCyclic=function(){for(var e=new Set,r=new Set,t=0,n=this.keys;t<n.length;t++){var o=n[t];if(this.isCyclicUtil(o,e,r))return!0}return!1},e}();!function(e){e.REJECT="reject",e.IGNORE="ignore",e.KEEP_LAST="keepLast",e.KEEP_ALL="keepAll",e.PARALLEL="parallel",e.REUSE="reuse",e.ABORT="abort"}(u||(u={}));var a=function(e){function t(r,n){var o=e.call(this,r.message)||this;return o.source=r,o.initialError=n,Object.setPrototypeOf(o,t.prototype),o}return r(t,e),t}(Error),l={clone:function(e){return JSON.parse(JSON.stringify(e))},comparator:function(e,r){if(Object.is(e,r))return!0;if("object"!=typeof e||null===e||"object"!=typeof r||null===r)return!1;if(e instanceof Set||r instanceof Set)return!1;var t=Object.keys(e),n=Object.keys(r);if(t.length!==n.length)return!1;for(var o=0;o<t.length;o++)if(!Object.prototype.hasOwnProperty.call(r,t[o])||!Object.is(e[t[o]],r[t[o]]))return!1;return!0},asyncErrorHandler:function(){},retryOnErrorFunction:function(e){return e instanceof TypeError},notifySuccess:void 0,notifyError:void 0,notifyWarning:void 0,getErrorMessage:void 0,defaultMiddlewares:[],autoRetryCount:void 0,autoRetryTimeout:void 0};function f(e){Object.keys(e).forEach((function(r){null!=e[r]&&(l[r]=e[r])}))}function s(e){try{return l.clone(e)}catch(e){var r="StateDecorator: Cannot clone object. Set setCloneFunction on global global with another implementation like lodash/cloneDeep.";throw"development"===process.env.NODE_ENV&&(console.error(r),console.error(e.toString())),new Error(r)}}function d(e){return!(e instanceof Function)&&(e.hasOwnProperty("getPromise")||e.hasOwnProperty("getGetPromise"))}function v(e){return"function"==typeof e}function p(e){return"function"!=typeof e&&!e.hasOwnProperty("getPromise")&&!e.hasOwnProperty("getGetPromise")}function y(e){return"function"!=typeof e&&e.hasOwnProperty("getPromise")&&!e.hasOwnProperty("getGetPromise")}function h(e){return"function"!=typeof e&&!e.hasOwnProperty("getPromise")&&e.hasOwnProperty("getGetPromise")}function g(e){var r,n,o;return e.conflictPolicy===u.ABORT&&(e.abortable=!0),h(e)?t(t({},e),{getPromise:e.getGetPromise,retryDelaySeed:null!==(r=l.autoRetryTimeout)&&void 0!==r?r:1e3,retryCount:null!==(n=l.autoRetryCount)&&void 0!==n?n:3,conflictPolicy:null!==(o=e.conflictPolicy)&&void 0!==o?o:u.REUSE}):e}function E(e,r,n,o){var i,u,c,a;return t(t({},e),o?((i={})[r]=t(t({},e[r]||{}),((u={})[n]=o,u)),i):((c={})[r]=t(t({},e[r]),((a={})[n]=!1,a)),c))}function S(e,r,n,o){var i,u,c,a;return t(t({},e),o?((i={})[r]=t(t({},e[r]||{}),((u={})[n]=o,u)),i):((c={})[r]=t(t({},e[r]),((a={})[n]=void 0,a)),c))}var m=function(e){function t(r){var n=e.call(this,"Error")||this;return n.promiseIds=r,Object.setPrototypeOf(n,t.prototype),n}return r(t,e),t}(Error);function P(e){return Object.keys(e).reduce((function(r,t){var n=e[t];if(null==n[W]);else if(n[W])r[t]=n[W];else{var o=Object.keys(n).reduce((function(e,r){return null!=n[r]&&(e.hasErr=!0,e.map[r]=n[r]),e}),{hasErr:!1,map:{}});o.hasErr&&(r[t]=new m(o.map))}return r}),{})}function w(e,r,t,i,u){return void 0===t&&(t=1),void 0===i&&(i=1e3),void 0===u&&(u=function(){return!0}),1===t?e:function(c){var a=e(c);return null===a?null:function a(l,f){return n(this,void 0,void 0,(function(){return o(this,(function(n){return null===f?[2,null]:[2,f.catch((function(n){return u(n)?l===t?Promise.reject(n):new Promise((function(t,n){r(setTimeout((function(){var r=e(c);a(l+1,r).then(t).catch(n)}),i*l))})):Promise.reject(n)}))]}))}))}(1,a)}}function b(e){return void 0===e&&(e=null),{current:e}}function R(e,r,t){var n,o;return{state:e.current,s:e.current,ds:null===(n=null==r?void 0:r.current)||void 0===n?void 0:n.state,derived:null===(o=null==r?void 0:r.current)||void 0===o?void 0:o.state,props:t.current,p:t.current}}function O(e,r,t,n){return k(R(e,r,t),n)}function A(e,r,n,o){return t(t({},R(e,r,n)),{abortedActions:o})}function j(e,r,t,n,o,i,u){var c=R(e,r,t);return c.indices=n,c.index=o,c.isInit=i,c.getInitialState=u,c}function C(e,r,t,n,o){var i=R(e,r,t);return i.args=n,o&&(i.promiseId=o),i}function L(e,r,t,n,o,i){var u=C(e,r,t,n,i);return u.result=o,u.res=o,u}function T(e,r){var t=e;return t.state=r.current,t.s=r.current,t}function k(e,r){var t=e;return t.actions=r.current,t.a=r.current,t}function _(e,r,t,n,o,i){var u,c=k(e,n);T(e,r);var a=null===(u=null==t?void 0:t.current)||void 0===u?void 0:u.state;return c.derived=a,c.ds=a,c.notifyWarning=o,c.clearError=i,c}function M(e){"development"===process.env.NODE_ENV&&console.warn("[state-decorator] ".concat(e," action was called while store is not initialized or destroyed, this is probably a leak"))}function x(e,r,n){return n?r:null==r?null:t(t({},e.current),r)}function D(e){return e.hasOwnProperty("getInitialState")}function I(e,r,t,n,o,i,u,c){return function(){for(var a=[],l=0;l<arguments.length;l++)a[l]=arguments[l];if(i.current){var f=C(t,n,o,a),s=x(t,r(f),u.fullStateEffects);c(s,null,e,"effects",!1,f,!1)}else M(e)}}function N(e,r,t,n,o,i,u,c,a,l,f,s,d){var v,p,y=L(t,n,o,d,void 0),h=!1;if(null!=r.effects){var g=x(t,r.effects(y),l.fullStateEffects);if(null===g)h=!0;else{var E=null!=r.debounceSideEffectsTimeout||null==r.sideEffects;a.current=!E,p=f(g,void 0,e,"effects",!1,y,!1,!1,!E)}}if(!h)if(r.debounceSideEffectsTimeout>0){var S=u.current[e];S&&clearTimeout(S),u.current[e]=setTimeout((function(){var o;c.current&&(null===(o=r.sideEffects)||void 0===o||o.call(r,_(y,t,n,i,l.notifyWarning,s)),delete u.current[e])}),r.debounceSideEffectsTimeout)}else null===(v=r.sideEffects)||void 0===v||v.call(r,_(y,t,n,i,l.notifyWarning,s)),a.current&&(null==p||p())}function G(e,r,t,n,o,i,u,c,a,l,f,s){return function(){for(var d=[],v=0;v<arguments.length;v++)d[v]=arguments[v];if(u.current)if(r.debounceTimeout){var p=c.current[e];p&&clearTimeout(p),c.current[e]=setTimeout((function(){u.current&&(delete c.current[e],N(e,r,t,n,o,i,c,u,a,l,f,s,d))}),r.debounceTimeout)}else N(e,r,t,n,o,i,c,u,a,l,f,s,d);else M(e)}}function z(e,r,t,n){var o=e.action,i=e.stateRef,u=e.derivedStateRef,c=e.propsRef,f=e.actionsRef,s=e.loadingParallelMapRef,d=e.errorMapRef,v=e.promisesRef,p=e.conflictActionsRef,y=e.actionName,h=e.options,g=e.clearError,m=e.setState;if(e.initializedRef.current){var P=null,w=t,b=function(e,r,t,n,o,i){var u=C(e,r,t,n,i);return u.error=o,u.err=o,u}(i,u,c,n,w,r);if(o.errorEffects)try{P=x(i,o.errorEffects(b),h.fullStateEffects)}catch(e){w=new a(e,t)}d.current=S(d.current,y,r,w),m(P,E(s.current,y,r,!1),y,"errorEffects",!0,b,!1);var R=!1;(null!=P||o.errorSideEffects||o.isErrorManaged||h.isErrorManaged)&&(R=!0);var O=h.notifyError||l.notifyError;if(O){var A=void 0;o.getErrorMessage&&(A=o.getErrorMessage(b)),void 0===A&&!R&&l.getErrorMessage&&(A=l.getErrorMessage(w)),A&&(R=!0,O(A))}return l.asyncErrorHandler(w,R||o.rejectPromiseOnError,i.current,c.current,y,n),delete v.current[y][r],o.errorSideEffects&&o.errorSideEffects(_(b,i,u,f,l.notifyWarning,g)),J(e.initializedRef,y,f.current,p.current),!R||o.rejectPromiseOnError?Promise.reject(w):Promise.resolve()}}function U(e){var r=e.action,n=e.stateRef,o=e.derivedStateRef,c=e.propsRef,f=e.actionsRef,d=e.timeoutRef,v=e.promisesRef,p=e.loadingParallelMapRef,y=e.errorMapRef,h=e.initializedRef,g=e.conflictActionsRef,m=e.actionName,P=e.setState,b=e.options;return function(){for(var R,O,A,j=[],T=0;T<arguments.length;T++)j[T]=arguments[T];if(!h.current)return M(m),null;var D=v.current,I=r.conflictPolicy,N=void 0===I?u.KEEP_ALL:I,G=N===u.PARALLEL,U=null;if(G){if(!r.getPromiseId)throw new Error("If conflict policy is set to ConflictPolicy.PARALLEL, getPromiseId must be set and return a string.");U=r.getPromiseId.apply(r,j)}else U=W;if(!G&&(null===(O=D[m])||void 0===O?void 0:O[U])){var K=function(e,r,t,n,o){var i=n;if(i===u.REUSE){if(F(e[t][W].refArgs,o))return e[t][W].promise;i=u.KEEP_ALL}return new Promise((function(n,c){var a,l={resolve:n,reject:c,args:s(o),timestamp:Date.now()};switch(i){case u.IGNORE:n(void 0);break;case u.REJECT:c(new Error("An asynchronous action ".concat(t," is already ongoing.")));break;case u.KEEP_LAST:r[t]=[l];break;case u.ABORT:var f=null===(a=e[t][W])||void 0===a?void 0:a.abortController;null==f||f.abort("conflict");case u.KEEP_ALL:var d=r[t];d||(r[t]=d=[]),d.push(l);case u.PARALLEL:}}))}(D,g.current,m,N,j);return K}var B=null,H=!1,V=C(n,o,c,j,U);if(r.preEffects){try{B=x(n,r.preEffects(V),b.fullStateEffects)}catch(e){return l.asyncErrorHandler(new a(e),!1,n.current,c.current,m,j),Promise.reject(new a(e))}B&&(H=!0,n.current=B)}var q=r.abortable,Q=r.retryCount,X=r.retryDelaySeed,Y=r.isTriggerRetryError,Z=q&&window.AbortController?new AbortController:null,$=null===(A=w(r.getPromise,(function(e){d.current&&(d.current[m]=e)}),Q?1+Q:1,X,Y||l.retryOnErrorFunction)(function(e,r,t,n,o,i,u){var c=k(C(e,r,t,n,u),o);return c.abortSignal=i,c}(n,o,c,j,f,null==Z?void 0:Z.signal)))||void 0===A?void 0:A.then((function(r){return function(e,r,t,n){var o=e.action,i=e.stateRef,u=e.needNotifyListenersRef,c=e.derivedStateRef,f=e.propsRef,s=e.promisesRef,d=e.actionsRef,v=e.loadingParallelMapRef,p=e.conflictActionsRef,y=e.actionName,h=e.options,g=e.setState,S=e.clearError;if(e.initializedRef.current){var m=null,P=L(i,c,f,n,t,r);if(o.effects)try{m=x(i,o.effects(P),h.fullStateEffects)}catch(e){return Promise.reject(new a(e))}u.current=!0;var w=g(m,E(v.current,y,r,!1),y,"effects",!0,P,!1,!1,!0),b=h.notifySuccess||l.notifySuccess;if(b){var R=void 0;o.getSuccessMessage&&(R=o.getSuccessMessage(P)),R&&b(R)}return delete s.current[y][r],o.sideEffects&&o.sideEffects(_(P,i,c,d,l.notifyWarning,S)),u.current&&w(),J(e.initializedRef,y,d.current,p.current),t}}(e,U,r,j)})).catch((function(r){return z(e,U,r,j)}));return null!=$&&(D[m]=t(t({},D[m]||{}),((R={})[U||W]={abortController:Z,promise:$,refArgs:N===u.REUSE&&j.length>0?i([],j,!0):[]},R))),y.current=S(y.current,m,U,null),P(null!=$||H?n.current:void 0,null==$?void 0:E(p.current,m,U,!0),m,"preEffects",!0,V,!1),$}}function K(e){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];return r.some((function(r){var t,n=null,o=W;Array.isArray(r)?(n=r[0],o=void 0===(t=r[1])?W:t):n=r;var i=e[n];return null!=i&&(null!=i[o]&&i[o])}))}var W="__def__";function F(e,r){return e.length===r.length&&-1===e.findIndex((function(e,t){return r[t]!==e}))}function J(e,r,t,n){if(e.current){var o=n[r];if((null==o?void 0:o.length)>0){var i=o.shift();if(i){var u=t[r].apply(t,i.args);null==u?J(e,r,t,n):u.then((function(o){null===o&&J(e,r,t,n),i.resolve(o)})).catch((function(e){return i.reject(e)}))}}}}function B(e,r,n,o,i){if(void 0===i&&(i=null),null==(null==o?void 0:o.derivedState))return!1;var u=!1,c=l.comparator,a=n.current.deps,f=R(e,null,r),s=Object.keys(o.derivedState);return n.current.state=s.reduce((function(e,r){var l,s,d,v,p=!1;if(null===(l=null==i?void 0:i.current)||void 0===l?void 0:l[r])e[r]=null===(s=i.current)||void 0===s?void 0:s[r];else{var y=o.derivedState[r],h=a[r],g=null!==(v=null===(d=y.getDeps)||void 0===d?void 0:d.call(y,f))&&void 0!==v?v:[];if(y.derivedDeps&&y.derivedDeps.forEach((function(r){var t,o,i;return g.push(null!==(t=e[r])&&void 0!==t?t:null===(i=null===(o=n.current)||void 0===o?void 0:o.state)||void 0===i?void 0:i[r])})),null==h)p=!0;else p=-1!==g.findIndex((function(e,r){return h.length<r||!c(h[r],e)}));u=u||p,a[r]=g,e[r]=p?o.derivedState[r].get(function(e,r){return t(t({},e),{ds:r,derived:r})}(f,e)):n.current.state[r]}return e}),{}),u}function H(e){var r,t=Object.keys(e.derivedState),n=new c(t);if(t.forEach((function(r){n.setEdges(r,e.derivedState[r].derivedDeps)})),n.isCyclic())throw new Error("There are cycles in the derived state dependencies");for(var o=!1,i={},u=0,a=t;u<a.length;u++){var l=a[u],f=null!==(r=e.derivedState[l].derivedDeps)&&void 0!==r?r:[];o=o||f.length>0,i[l]=f.concat()}if(o){t.sort((function(e,r){return i[e].length-i[r].length}));for(var s=[],d=new Set,v=t.length;d.size!==v;){var p=t.pop();if(!d.has(p))i[p]=i[p].filter((function(e){return!d.has(e)})),0===i[p].length?(s.push(p),d.add(p)):t.splice(0,0,p)}for(var y={},h=0,g=s;h<g.length;h++){y[p=g[h]]=e.derivedState[p]}e.derivedState=y}}function V(e,r,n,o,i,u,c,a,f,s){if(null!=e.current){var d=null!=(null==u?void 0:u.derivedState);if(null!=u.onPropsChange){var v;v=Array.isArray(u.onPropsChange)?u.onPropsChange:[u.onPropsChange];var p=!1,y=b(e.current),h=[],g=l.comparator;v.forEach((function(t,i){var l,d=t.getDeps;if(d){var v=!1,E=[];if(f){if(!(!s&&t.onMount||s&&t.onMountDeferred))return;v=!0}else{var S=d(o),m=d(n.current);S.length!==m.length?(console.warn("options.onPropsChange.getDeps returned array must be stable (same length)"),v=!0):S.forEach((function(e,r){g(e,m[r])||(v=!0,E.push(r))}))}if(v){var P=j(y,r,n,E,i,f,c),w=x(e,null===(l=t.effects)||void 0===l?void 0:l.call(t,P),u.fullStateEffects);null!=w&&(p=!0,y.current=w,a(y.current,void 0,"onPropsChange","effects",!1,P,!0,f)),t.sideEffects&&h.push({index:i,indices:E,ctx:P})}}})),!p&&d&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0),h.length>0&&h.forEach((function(e){v[e.index].sideEffects(t(t({},T(k(e.ctx,i),y)),{indices:e.indices}))}))}else null!=(null==u?void 0:u.derivedState)&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0)}}function q(e,r,t,n,o,i,u,c){if("onPropsChange"===o&&void 0===t)return{newState:null,newLoading:!1};if(e&&e.length>0){var a=e.reduce((function(e,r){var t=r.effects({isAsync:u,name:o,type:i,context:c},e.oldState,e.newState,n);return{newState:(null==t?void 0:t.state)||e.newState,oldState:(null==t?void 0:t.state)?e.newState:e.oldState,loading:null==(null==t?void 0:t.loading)?e.loading:t.loading}}),{oldState:r,newState:t,loading:n});return{newLoading:a.loading,newState:a.newState}}return{newState:t,newLoading:n}}export{W as DEFAULT_PROMISE_ID,a as EffectError,m as ParallelActionError,k as addContextActions,T as addStateToContext,F as areSameArgs,P as buildErrorMap,R as buildInvocationContextBase,E as buildLoadingMap,O as buildOnMountInvocationContext,j as buildOnPropChangeEffects,A as buildOnUnMountInvocationContext,g as computeAsyncActionInput,B as computeDerivedValues,b as createRef,U as decorateAsyncAction,I as decorateSimpleSyncAction,G as decorateSyncAction,H as fixDerivedDeps,l as globalConfig,d as isAsyncAction,y as isAsyncGetPromiseAction,h as isAsyncGetPromiseGetAction,D as isFuncConfig,K as isLoadingImpl,v as isSimpleSyncAction,p as isSyncAction,V as onPropChange,w as retryPromiseDecorator,q as runMiddlewares,f as setGlobalConfig,S as updateErrorMap};
//# sourceMappingURL=impl.js.map