UNPKG

state-decorator

Version:
3 lines (2 loc) 17.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});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 c(e){try{a(n.next(e))}catch(e){i(e)}}function u(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(c,u)}a((n=n.apply(e,r||[])).next())}))}function o(e,r){var t,n,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(a){return function(u){if(t)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(i=0)),i;)try{if(t=1,n&&(o=2&u[0]?n.return:u[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,u[1])).done)return o;switch(n=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,n=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=r.call(e,i)}catch(e){u=[6,e],n=0}finally{t=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,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 c,u=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 c=i[o];if(this.isCyclicUtil(c,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"}(c||(c={}));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 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 f(e){return"function"!=typeof e&&!e.hasOwnProperty("getPromise")&&e.hasOwnProperty("getGetPromise")}function d(e,r,n,o){var i,c,u,a;return t(t({},e),o?((i={})[r]=t(t({},e[r]||{}),((c={})[n]=o,c)),i):((u={})[r]=t(t({},e[r]),((a={})[n]=!1,a)),u))}function v(e,r,n,o){var i,c,u,a;return t(t({},e),o?((i={})[r]=t(t({},e[r]||{}),((c={})[n]=o,c)),i):((u={})[r]=t(t({},e[r]),((a={})[n]=void 0,a)),u))}var p=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 y(e,r,t,i,c){return void 0===t&&(t=1),void 0===i&&(i=1e3),void 0===c&&(c=function(){return!0}),1===t?e:function(u){var a=e(u);return null===a?null:function a(l,s){return n(this,void 0,void 0,(function(){return o(this,(function(n){return null===s?[2,null]:[2,s.catch((function(n){return c(n)?l===t?Promise.reject(n):new Promise((function(t,n){r(setTimeout((function(){var r=e(u);a(l+1,r).then(t).catch(n)}),i*l))})):Promise.reject(n)}))]}))}))}(1,a)}}function g(e){return void 0===e&&(e=null),{current:e}}function h(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 E(e,r,t,n,o,i,c){var u=h(e,r,t);return u.indices=n,u.index=o,u.isInit=i,u.getInitialState=c,u}function S(e,r,t,n,o){var i=h(e,r,t);return i.args=n,o&&(i.promiseId=o),i}function m(e,r,t,n,o,i){var c=S(e,r,t,n,i);return c.result=o,c.res=o,c}function P(e,r){var t=e;return t.state=r.current,t.s=r.current,t}function b(e,r){var t=e;return t.actions=r.current,t.a=r.current,t}function w(e,r,t,n,o,i){var c,u=b(e,n);P(e,r);var a=null===(c=null==t?void 0:t.current)||void 0===c?void 0:c.state;return u.derived=a,u.ds=a,u.notifyWarning=o,u.clearError=i,u}function x(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 A(e,r,n){return n?r:null==r?null:t(t({},e.current),r)}function O(e,r,t,n,o,i,c,u,a,l,s,f,d){var v,p,y=m(t,n,o,d,void 0),g=!1;if(null!=r.effects){var h=A(t,r.effects(y),l.fullStateEffects);if(null===h)g=!0;else{var E=null!=r.debounceSideEffectsTimeout||null==r.sideEffects;a.current=!E,p=s(h,void 0,e,"effects",!1,y,!1,!1,!E)}}if(!g)if(r.debounceSideEffectsTimeout>0){var S=c.current[e];S&&clearTimeout(S),c.current[e]=setTimeout((function(){var o;u.current&&(null===(o=r.sideEffects)||void 0===o||o.call(r,w(y,t,n,i,l.notifyWarning,f)),delete c.current[e])}),r.debounceSideEffectsTimeout)}else null===(v=r.sideEffects)||void 0===v||v.call(r,w(y,t,n,i,l.notifyWarning,f)),a.current&&(null==p||p())}function R(e,r,t,n){var o=e.action,i=e.stateRef,c=e.derivedStateRef,u=e.propsRef,s=e.actionsRef,f=e.loadingParallelMapRef,p=e.errorMapRef,y=e.promisesRef,g=e.conflictActionsRef,h=e.actionName,E=e.options,m=e.clearError,P=e.setState;if(e.initializedRef.current){var b=null,x=t,O=function(e,r,t,n,o,i){var c=S(e,r,t,n,i);return c.error=o,c.err=o,c}(i,c,u,n,x,r);if(o.errorEffects)try{b=A(i,o.errorEffects(O),E.fullStateEffects)}catch(e){x=new a(e,t)}p.current=v(p.current,h,r,x),P(b,d(f.current,h,r,!1),h,"errorEffects",!0,O,!1);var R=!1;(null!=b||o.errorSideEffects||o.isErrorManaged||E.isErrorManaged)&&(R=!0);var C=E.notifyError||l.notifyError;if(C){var j=void 0;o.getErrorMessage&&(j=o.getErrorMessage(O)),void 0===j&&!R&&l.getErrorMessage&&(j=l.getErrorMessage(x)),j&&(R=!0,C(j))}return l.asyncErrorHandler(x,R||o.rejectPromiseOnError,i.current,u.current,h,n),delete y.current[h][r],o.errorSideEffects&&o.errorSideEffects(w(O,i,c,s,l.notifyWarning,m)),L(e.initializedRef,h,s.current,g.current),!R||o.rejectPromiseOnError?Promise.reject(x):Promise.resolve()}}var C="__def__";function j(e,r){return e.length===r.length&&-1===e.findIndex((function(e,t){return r[t]!==e}))}function L(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 c=t[r].apply(t,i.args);null==c?L(e,r,t,n):c.then((function(o){null===o&&L(e,r,t,n),i.resolve(o)})).catch((function(e){return i.reject(e)}))}}}}exports.DEFAULT_PROMISE_ID=C,exports.EffectError=a,exports.ParallelActionError=p,exports.addContextActions=b,exports.addStateToContext=P,exports.areSameArgs=j,exports.buildErrorMap=function(e){return Object.keys(e).reduce((function(r,t){var n=e[t];if(null==n[C]);else if(n[C])r[t]=n[C];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 p(o.map))}return r}),{})},exports.buildInvocationContextBase=h,exports.buildLoadingMap=d,exports.buildOnMountInvocationContext=function(e,r,t,n){return b(h(e,r,t),n)},exports.buildOnPropChangeEffects=E,exports.buildOnUnMountInvocationContext=function(e,r,n,o){return t(t({},h(e,r,n)),{abortedActions:o})},exports.computeAsyncActionInput=function(e){var r,n,o;return e.conflictPolicy===c.ABORT&&(e.abortable=!0),f(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:c.REUSE}):e},exports.computeDerivedValues=function(e,r,n,o,i){if(void 0===i&&(i=null),null==(null==o?void 0:o.derivedState))return!1;var c=!1,u=l.comparator,a=n.current.deps,s=h(e,null,r),f=Object.keys(o.derivedState);return n.current.state=f.reduce((function(e,r){var l,f,d,v,p=!1;if(null===(l=null==i?void 0:i.current)||void 0===l?void 0:l[r])e[r]=null===(f=i.current)||void 0===f?void 0:f[r];else{var y=o.derivedState[r],g=a[r],h=null!==(v=null===(d=y.getDeps)||void 0===d?void 0:d.call(y,s))&&void 0!==v?v:[];if(y.derivedDeps&&y.derivedDeps.forEach((function(r){var t,o,i;return h.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==g)p=!0;else p=-1!==h.findIndex((function(e,r){return g.length<r||!u(g[r],e)}));c=c||p,a[r]=h,e[r]=p?o.derivedState[r].get(function(e,r){return t(t({},e),{ds:r,derived:r})}(s,e)):n.current.state[r]}return e}),{}),c},exports.createRef=g,exports.decorateAsyncAction=function(e){var r=e.action,n=e.stateRef,o=e.derivedStateRef,u=e.propsRef,f=e.actionsRef,p=e.timeoutRef,g=e.promisesRef,h=e.loadingParallelMapRef,E=e.errorMapRef,P=e.initializedRef,O=e.conflictActionsRef,T=e.actionName,M=e.setState,_=e.options;return function(){for(var I,D,k,G=[],N=0;N<arguments.length;N++)G[N]=arguments[N];if(!P.current)return x(T),null;var U=g.current,z=r.conflictPolicy,F=void 0===z?c.KEEP_ALL:z,K=F===c.PARALLEL,W=null;if(K){if(!r.getPromiseId)throw new Error("If conflict policy is set to ConflictPolicy.PARALLEL, getPromiseId must be set and return a string.");W=r.getPromiseId.apply(r,G)}else W=C;if(!K&&(null===(D=U[T])||void 0===D?void 0:D[W])){var B=function(e,r,t,n,o){var i=n;if(i===c.REUSE){if(j(e[t][C].refArgs,o))return e[t][C].promise;i=c.KEEP_ALL}return new Promise((function(n,u){var a,l={resolve:n,reject:u,args:s(o),timestamp:Date.now()};switch(i){case c.IGNORE:n(void 0);break;case c.REJECT:u(new Error("An asynchronous action ".concat(t," is already ongoing.")));break;case c.KEEP_LAST:r[t]=[l];break;case c.ABORT:var f=null===(a=e[t][C])||void 0===a?void 0:a.abortController;null==f||f.abort("conflict");case c.KEEP_ALL:var d=r[t];d||(r[t]=d=[]),d.push(l);case c.PARALLEL:}}))}(U,O.current,T,F,G);return B}var J=null,H=!1,V=S(n,o,u,G,W);if(r.preEffects){try{J=A(n,r.preEffects(V),_.fullStateEffects)}catch(e){return l.asyncErrorHandler(new a(e),!1,n.current,u.current,T,G),Promise.reject(new a(e))}J&&(H=!0,n.current=J)}var q=r.abortable,Q=r.retryCount,X=r.retryDelaySeed,Y=r.isTriggerRetryError,Z=q&&window.AbortController?new AbortController:null,$=null===(k=y(r.getPromise,(function(e){p.current&&(p.current[T]=e)}),Q?1+Q:1,X,Y||l.retryOnErrorFunction)(function(e,r,t,n,o,i,c){var u=b(S(e,r,t,n,c),o);return u.abortSignal=i,u}(n,o,u,G,f,null==Z?void 0:Z.signal)))||void 0===k?void 0:k.then((function(r){return function(e,r,t,n){var o=e.action,i=e.stateRef,c=e.needNotifyListenersRef,u=e.derivedStateRef,s=e.propsRef,f=e.promisesRef,v=e.actionsRef,p=e.loadingParallelMapRef,y=e.conflictActionsRef,g=e.actionName,h=e.options,E=e.setState,S=e.clearError;if(e.initializedRef.current){var P=null,b=m(i,u,s,n,t,r);if(o.effects)try{P=A(i,o.effects(b),h.fullStateEffects)}catch(e){return Promise.reject(new a(e))}c.current=!0;var x=E(P,d(p.current,g,r,!1),g,"effects",!0,b,!1,!1,!0),O=h.notifySuccess||l.notifySuccess;if(O){var R=void 0;o.getSuccessMessage&&(R=o.getSuccessMessage(b)),R&&O(R)}return delete f.current[g][r],o.sideEffects&&o.sideEffects(w(b,i,u,v,l.notifyWarning,S)),c.current&&x(),L(e.initializedRef,g,v.current,y.current),t}}(e,W,r,G)})).catch((function(r){return R(e,W,r,G)}));return null!=$&&(U[T]=t(t({},U[T]||{}),((I={})[W||C]={abortController:Z,promise:$,refArgs:F===c.REUSE&&G.length>0?i([],G,!0):[]},I))),E.current=v(E.current,T,W,null),M(null!=$||H?n.current:void 0,null==$?void 0:d(h.current,T,W,!0),T,"preEffects",!0,V,!1),$}},exports.decorateSimpleSyncAction=function(e,r,t,n,o,i,c,u){return function(){for(var a=[],l=0;l<arguments.length;l++)a[l]=arguments[l];if(i.current){var s=S(t,n,o,a),f=A(t,r(s),c.fullStateEffects);u(f,null,e,"effects",!1,s,!1)}else x(e)}},exports.decorateSyncAction=function(e,r,t,n,o,i,c,u,a,l,s,f){return function(){for(var d=[],v=0;v<arguments.length;v++)d[v]=arguments[v];if(c.current)if(r.debounceTimeout){var p=u.current[e];p&&clearTimeout(p),u.current[e]=setTimeout((function(){c.current&&(delete u.current[e],O(e,r,t,n,o,i,u,c,a,l,s,f,d))}),r.debounceTimeout)}else O(e,r,t,n,o,i,u,c,a,l,s,f,d);else x(e)}},exports.fixDerivedDeps=function(e){var r,t=Object.keys(e.derivedState),n=new u(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={},c=0,a=t;c<a.length;c++){var l=a[c],s=null!==(r=e.derivedState[l].derivedDeps)&&void 0!==r?r:[];o=o||s.length>0,i[l]=s.concat()}if(o){t.sort((function(e,r){return i[e].length-i[r].length}));for(var f=[],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?(f.push(p),d.add(p)):t.splice(0,0,p)}for(var y={},g=0,h=f;g<h.length;g++){y[p=h[g]]=e.derivedState[p]}e.derivedState=y}},exports.globalConfig=l,exports.isAsyncAction=function(e){return!(e instanceof Function)&&(e.hasOwnProperty("getPromise")||e.hasOwnProperty("getGetPromise"))},exports.isAsyncGetPromiseAction=function(e){return"function"!=typeof e&&e.hasOwnProperty("getPromise")&&!e.hasOwnProperty("getGetPromise")},exports.isAsyncGetPromiseGetAction=f,exports.isFuncConfig=function(e){return e.hasOwnProperty("getInitialState")},exports.isLoadingImpl=function(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=C;Array.isArray(r)?(n=r[0],o=void 0===(t=r[1])?C:t):n=r;var i=e[n];return null!=i&&(null!=i[o]&&i[o])}))},exports.isSimpleSyncAction=function(e){return"function"==typeof e},exports.isSyncAction=function(e){return"function"!=typeof e&&!e.hasOwnProperty("getPromise")&&!e.hasOwnProperty("getGetPromise")},exports.onPropChange=function(e,r,n,o,i,c,u,a,s,f){if(null!=e.current){var d=null!=(null==c?void 0:c.derivedState);if(null!=c.onPropsChange){var v;v=Array.isArray(c.onPropsChange)?c.onPropsChange:[c.onPropsChange];var p=!1,y=g(e.current),h=[],S=l.comparator;v.forEach((function(t,i){var l,d=t.getDeps;if(d){var v=!1,g=[];if(s){if(!(!f&&t.onMount||f&&t.onMountDeferred))return;v=!0}else{var m=d(o),P=d(n.current);m.length!==P.length?(console.warn("options.onPropsChange.getDeps returned array must be stable (same length)"),v=!0):m.forEach((function(e,r){S(e,P[r])||(v=!0,g.push(r))}))}if(v){var b=E(y,r,n,g,i,s,u),w=A(e,null===(l=t.effects)||void 0===l?void 0:l.call(t,b),c.fullStateEffects);null!=w&&(p=!0,y.current=w,a(y.current,void 0,"onPropsChange","effects",!1,b,!0,s)),t.sideEffects&&h.push({index:i,indices:g,ctx:b})}}})),!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({},P(b(e.ctx,i),y)),{indices:e.indices}))}))}else null!=(null==c?void 0:c.derivedState)&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0)}},exports.retryPromiseDecorator=y,exports.runMiddlewares=function(e,r,t,n,o,i,c,u){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:c,name:o,type:i,context:u},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}},exports.setGlobalConfig=function(e){Object.keys(e).forEach((function(r){null!=e[r]&&(l[r]=e[r])}))},exports.updateErrorMap=v; //# sourceMappingURL=impl.js.map