state-decorator 7.2.1 → 7.3.0-optim.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/es/impl.js CHANGED
@@ -1,2 +1,2 @@
1
- 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,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(a){return function(u){if(t)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)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 c.label++,{value:u[1],done:!1};case 5:c.label++,n=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=r.call(e,c)}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:[]};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 h(e){return"function"!=typeof e&&e.hasOwnProperty("getPromise")&&!e.hasOwnProperty("getGetPromise")}function g(e){return"function"!=typeof e&&!e.hasOwnProperty("getPromise")&&e.hasOwnProperty("getGetPromise")}function y(e){return e.conflictPolicy===c.ABORT&&(e.abortable=!0),g(e)?t(t({},e),{getPromise:e.getGetPromise,retryCount:3,conflictPolicy:c.REUSE}):e}function E(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 S(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 w=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 w(o.map))}return r}),{})}function m(e,r,t,i){return void 0===r&&(r=1),void 0===t&&(t=1e3),void 0===i&&(i=function(){return!0}),1===r?e:function(c){var u=e(c);return null===u?null:function u(a,l){return n(this,void 0,void 0,(function(){return o(this,(function(n){return null===l?[2,null]:[2,l.catch((function(n){return i(n)?a===r?Promise.reject(n):new Promise((function(r,n){setTimeout((function(){var t=e(c);u(a+1,t).then(r).catch(n)}),t*a)})):Promise.reject(n)}))]}))}))}(1,u)}}function b(e){return void 0===e&&(e=null),{current:e}}function O(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 R(e,r,t,n){return k(O(e,r,t),n)}function A(e,r,n,o){return t(t({},O(e,r,n)),{abortedActions:o})}function j(e,r,t,n,o,i,c){var u=O(e,r,t);return u.indices=n,u.index=o,u.isInit=i,u.getInitialState=c,u}function C(e,r,t,n,o){var i=O(e,r,t);return i.args=n,o&&(i.promiseId=o),i}function L(e,r,t,n,o,i){var c=C(e,r,t,n,i);return c.result=o,c.res=o,c}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 c,u=k(e,n);T(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 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,c,u){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),c.fullStateEffects);u(s,null,e,"effects",!1,f,!1)}else M(e)}}function N(e,r,t,n,o,i,c,u,a,l,f,s){var d,v=L(t,n,o,s,void 0),p=!1;if(null!=r.effects){var h=x(t,r.effects(v),a.fullStateEffects);null===h?p=!0:l(h,void 0,e,"effects",!1,v,!1)}if(!p)if(r.debounceSideEffectsTimeout>0){var g=c.current[e];g&&clearTimeout(g),c.current[e]=setTimeout((function(){var o;u.current&&(null===(o=r.sideEffects)||void 0===o||o.call(r,_(v,t,n,i,a.notifyWarning,f)),delete c.current[e])}),r.debounceSideEffectsTimeout)}else null===(d=r.sideEffects)||void 0===d||d.call(r,_(v,t,n,i,a.notifyWarning,f))}function G(e,r,t,n,o,i,c,u,a,l,f){return function(){for(var s=[],d=0;d<arguments.length;d++)s[d]=arguments[d];if(c.current)if(r.debounceTimeout){var v=u.current[e];v&&clearTimeout(v),u.current[e]=setTimeout((function(){c.current&&(delete u.current[e],N(e,r,t,n,o,i,u,c,a,l,f,s))}),r.debounceTimeout)}else N(e,r,t,n,o,i,u,c,a,l,f,s);else M(e)}}function U(e,r,t,n){var o=e.action,i=e.stateRef,c=e.derivedStateRef,u=e.propsRef,f=e.actionsRef,s=e.loadingParallelMapRef,d=e.errorMapRef,v=e.promisesRef,p=e.conflictActionsRef,h=e.actionName,g=e.options,y=e.clearError,w=e.setState;if(e.initializedRef.current){var P=null,m=t,b=function(e,r,t,n,o,i){var c=C(e,r,t,n,i);return c.error=o,c.err=o,c}(i,c,u,n,m,r);if(o.errorEffects)try{P=x(i,o.errorEffects(b),g.fullStateEffects)}catch(e){m=new a(e,t)}d.current=S(d.current,h,r,m),w(P,E(s.current,h,r,!1),h,"errorEffects",!0,b,!1);var O=!1;(null!=P||o.errorSideEffects||o.isErrorManaged||g.isErrorManaged)&&(O=!0);var R=g.notifyError||l.notifyError;if(R){var A=void 0;o.getErrorMessage&&(A=o.getErrorMessage(b)),void 0===A&&!O&&l.getErrorMessage&&(A=l.getErrorMessage(m)),A&&(O=!0,R(A))}return l.asyncErrorHandler(m,O||o.rejectPromiseOnError,i.current,u.current,h,n),delete v.current[h][r],o.errorSideEffects&&o.errorSideEffects(_(b,i,c,f,l.notifyWarning,y)),J(e.initializedRef,h,f.current,p.current),!O||o.rejectPromiseOnError?Promise.reject(m):Promise.resolve()}}function z(e){var r=e.action,n=e.stateRef,o=e.derivedStateRef,u=e.propsRef,f=e.actionsRef,d=e.promisesRef,v=e.loadingParallelMapRef,p=e.errorMapRef,h=e.initializedRef,g=e.conflictActionsRef,y=e.actionName,w=e.setState,P=e.options;return function(){for(var b,O,R,A=[],j=0;j<arguments.length;j++)A[j]=arguments[j];if(!h.current)return M(y),null;var T=d.current,D=r.conflictPolicy,I=void 0===D?c.KEEP_ALL:D,N=I===c.PARALLEL,G=null;if(N){if(!r.getPromiseId)throw new Error("If conflict policy is set to ConflictPolicy.PARALLEL, getPromiseId must be set and return a string.");G=r.getPromiseId.apply(r,A)}else G=W;if(!N&&(null===(O=T[y])||void 0===O?void 0:O[G])){var z=function(e,r,t,n,o){var i=n;if(i===c.REUSE){if(F(e[t][W].refArgs,o))return e[t][W].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][W])||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:case c.REUSE:}}))}(T,g.current,y,I,A);return z}var K=null,B=!1,H=C(n,o,u,A,G);if(r.preEffects){try{K=x(n,r.preEffects(H),P.fullStateEffects)}catch(e){return l.asyncErrorHandler(new a(e),!1,n.current,u.current,y,A),Promise.reject(new a(e))}K&&(B=!0,n.current=K)}var V=r.abortable,q=r.retryCount,Q=r.retryDelaySeed,X=r.isTriggerRetryError,Y=V&&window.AbortController?new AbortController:null,Z=null===(R=m(r.getPromise,q?1+q:1,Q,X||l.retryOnErrorFunction)(function(e,r,t,n,o,i,c){var u=k(C(e,r,t,n,c),o);return u.abortSignal=i,u}(n,o,u,A,f,null==Y?void 0:Y.signal)))||void 0===R?void 0:R.then((function(r){return function(e,r,t,n){var o=e.action,i=e.stateRef,c=e.derivedStateRef,u=e.propsRef,f=e.promisesRef,s=e.actionsRef,d=e.loadingParallelMapRef,v=e.conflictActionsRef,p=e.actionName,h=e.options,g=e.setState,y=e.clearError;if(e.initializedRef.current){var S=null,w=L(i,c,u,n,t,r);if(o.effects)try{S=x(i,o.effects(w),h.fullStateEffects)}catch(e){return Promise.reject(new a(e))}g(S,E(d.current,p,r,!1),p,"effects",!0,w,!1);var P=h.notifySuccess||l.notifySuccess;if(P){var m=void 0;o.getSuccessMessage&&(m=o.getSuccessMessage(w)),m&&P(m)}return delete f.current[p][r],o.sideEffects&&o.sideEffects(_(w,i,c,s,l.notifyWarning,y)),J(e.initializedRef,p,s.current,v.current),t}}(e,G,r,A)})).catch((function(r){return U(e,G,r,A)}));return null!=Z&&(T[y]=t(t({},T[y]||{}),((b={})[G||W]={abortController:Y,promise:Z,refArgs:I===c.REUSE&&A.length>0?i([],A,!0):[]},b))),p.current=S(p.current,y,G,null),w(null!=Z||B?n.current:void 0,null==Z?void 0:E(v.current,y,G,!0),y,"preEffects",!0,H,!1),Z}}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 c=t[r].apply(t,i.args);null==c?J(e,r,t,n):c.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){if(null==(null==o?void 0:o.derivedState))return!1;var i=!1,c=l.comparator,u=n.current.deps,a=O(e,null,r),f=Object.keys(o.derivedState);return n.current.state=f.reduce((function(e,r){var l,f,s=!1,d=o.derivedState[r],v=u[r],p=null!==(f=null===(l=d.getDeps)||void 0===l?void 0:l.call(d,a))&&void 0!==f?f:[];(d.derivedDeps&&d.derivedDeps.forEach((function(r){var t,o,i;return p.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==v)?s=!0:s=-1!==p.findIndex((function(e,r){return v.length<r||!c(v[r],e)}));return i=i||s,u[r]=p,e[r]=s?o.derivedState[r].get(function(e,r){return t(t({},e),{ds:r,derived:r})}(a,e)):n.current.state[r],e}),{}),i}function H(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],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 h={},g=0,y=s;g<y.length;g++){h[p=y[g]]=e.derivedState[p]}e.derivedState=h}}function V(e,r,n,o,i,c,u,a,f,s){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,h=b(e.current),g=[],y=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),w=d(n.current);S.length!==w.length?(console.warn("options.onPropsChange.getDeps returned array must be stable (same length)"),v=!0):S.forEach((function(e,r){y(e,w[r])||(v=!0,E.push(r))}))}if(v){var P=j(h,r,n,E,i,f,u),m=x(e,null===(l=t.effects)||void 0===l?void 0:l.call(t,P),c.fullStateEffects);null!=m&&(p=!0,h.current=m,a(h.current,void 0,"onPropsChange","effects",!1,P,!0,f)),t.sideEffects&&g.push({index:i,indices:E,ctx:P})}}})),!p&&d&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0),g.length>0&&g.forEach((function(e){v[e.index].sideEffects(t(t({},T(k(e.ctx,i),h)),{indices:e.indices}))}))}else null!=(null==c?void 0:c.derivedState)&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0)}function q(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}}export{W as DEFAULT_PROMISE_ID,a as EffectError,w as ParallelActionError,k as addContextActions,T as addStateToContext,F as areSameArgs,P as buildErrorMap,O as buildInvocationContextBase,E as buildLoadingMap,R as buildOnMountInvocationContext,j as buildOnPropChangeEffects,A as buildOnUnMountInvocationContext,y as computeAsyncActionInput,B as computeDerivedValues,b as createRef,z as decorateAsyncAction,I as decorateSimpleSyncAction,G as decorateSyncAction,H as fixDerivedDeps,l as globalConfig,d as isAsyncAction,h as isAsyncGetPromiseAction,g as isAsyncGetPromiseGetAction,D as isFuncConfig,K as isLoadingImpl,v as isSimpleSyncAction,p as isSyncAction,V as onPropChange,m as retryPromiseDecorator,q as runMiddlewares,f as setGlobalConfig,S as updateErrorMap};
1
+ 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,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(a){return function(u){if(t)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)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 c.label++,{value:u[1],done:!1};case 5:c.label++,n=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=r.call(e,c)}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 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 h(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 g(e){var r,n,o;return e.conflictPolicy===c.ABORT&&(e.abortable=!0),y(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}function E(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 S(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 m(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 P(o.map))}return r}),{})}function w(e,r,t,i){return void 0===r&&(r=1),void 0===t&&(t=1e3),void 0===i&&(i=function(){return!0}),1===r?e:function(c){var u=e(c);return null===u?null:function u(a,l){return n(this,void 0,void 0,(function(){return o(this,(function(n){return null===l?[2,null]:[2,l.catch((function(n){return i(n)?a===r?Promise.reject(n):new Promise((function(r,n){setTimeout((function(){var t=e(c);u(a+1,t).then(r).catch(n)}),t*a)})):Promise.reject(n)}))]}))}))}(1,u)}}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,c){var u=R(e,r,t);return u.indices=n,u.index=o,u.isInit=i,u.getInitialState=c,u}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 c=C(e,r,t,n,i);return c.result=o,c.res=o,c}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 c,u=k(e,n);T(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 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,c,u){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),c.fullStateEffects);u(s,null,e,"effects",!1,f,!1)}else M(e)}}function N(e,r,t,n,o,i,c,u,a,l,f,s){var d,v=L(t,n,o,s,void 0),p=!1;if(null!=r.effects){var h=x(t,r.effects(v),a.fullStateEffects);null===h?p=!0:l(h,void 0,e,"effects",!1,v,!1)}if(!p)if(r.debounceSideEffectsTimeout>0){var y=c.current[e];y&&clearTimeout(y),c.current[e]=setTimeout((function(){var o;u.current&&(null===(o=r.sideEffects)||void 0===o||o.call(r,_(v,t,n,i,a.notifyWarning,f)),delete c.current[e])}),r.debounceSideEffectsTimeout)}else null===(d=r.sideEffects)||void 0===d||d.call(r,_(v,t,n,i,a.notifyWarning,f))}function G(e,r,t,n,o,i,c,u,a,l,f){return function(){for(var s=[],d=0;d<arguments.length;d++)s[d]=arguments[d];if(c.current)if(r.debounceTimeout){var v=u.current[e];v&&clearTimeout(v),u.current[e]=setTimeout((function(){c.current&&(delete u.current[e],N(e,r,t,n,o,i,u,c,a,l,f,s))}),r.debounceTimeout)}else N(e,r,t,n,o,i,u,c,a,l,f,s);else M(e)}}function z(e,r,t,n){var o=e.action,i=e.stateRef,c=e.derivedStateRef,u=e.propsRef,f=e.actionsRef,s=e.loadingParallelMapRef,d=e.errorMapRef,v=e.promisesRef,p=e.conflictActionsRef,h=e.actionName,y=e.options,g=e.clearError,P=e.setState;if(e.initializedRef.current){var m=null,w=t,b=function(e,r,t,n,o,i){var c=C(e,r,t,n,i);return c.error=o,c.err=o,c}(i,c,u,n,w,r);if(o.errorEffects)try{m=x(i,o.errorEffects(b),y.fullStateEffects)}catch(e){w=new a(e,t)}d.current=S(d.current,h,r,w),P(m,E(s.current,h,r,!1),h,"errorEffects",!0,b,!1);var R=!1;(null!=m||o.errorSideEffects||o.isErrorManaged||y.isErrorManaged)&&(R=!0);var O=y.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,u.current,h,n),delete v.current[h][r],o.errorSideEffects&&o.errorSideEffects(_(b,i,c,f,l.notifyWarning,g)),J(e.initializedRef,h,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,u=e.propsRef,f=e.actionsRef,d=e.promisesRef,v=e.loadingParallelMapRef,p=e.errorMapRef,h=e.initializedRef,y=e.conflictActionsRef,g=e.actionName,P=e.setState,m=e.options;return function(){for(var b,R,O,A=[],j=0;j<arguments.length;j++)A[j]=arguments[j];if(!h.current)return M(g),null;var T=d.current,D=r.conflictPolicy,I=void 0===D?c.KEEP_ALL:D,N=I===c.PARALLEL,G=null;if(N){if(!r.getPromiseId)throw new Error("If conflict policy is set to ConflictPolicy.PARALLEL, getPromiseId must be set and return a string.");G=r.getPromiseId.apply(r,A)}else G=W;if(!N&&(null===(R=T[g])||void 0===R?void 0:R[G])){var U=function(e,r,t,n,o){var i=n;if(i===c.REUSE){if(F(e[t][W].refArgs,o))return e[t][W].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][W])||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:}}))}(T,y.current,g,I,A);return U}var K=null,B=!1,H=C(n,o,u,A,G);if(r.preEffects){try{K=x(n,r.preEffects(H),m.fullStateEffects)}catch(e){return l.asyncErrorHandler(new a(e),!1,n.current,u.current,g,A),Promise.reject(new a(e))}K&&(B=!0,n.current=K)}var V=r.abortable,q=r.retryCount,Q=r.retryDelaySeed,X=r.isTriggerRetryError,Y=V&&window.AbortController?new AbortController:null,Z=null===(O=w(r.getPromise,q?1+q:1,Q,X||l.retryOnErrorFunction)(function(e,r,t,n,o,i,c){var u=k(C(e,r,t,n,c),o);return u.abortSignal=i,u}(n,o,u,A,f,null==Y?void 0:Y.signal)))||void 0===O?void 0:O.then((function(r){return function(e,r,t,n){var o=e.action,i=e.stateRef,c=e.stateFlagRef,u=e.derivedStateRef,f=e.propsRef,s=e.promisesRef,d=e.actionsRef,v=e.loadingParallelMapRef,p=e.conflictActionsRef,h=e.actionName,y=e.options,g=e.setState,S=e.clearError;if(e.initializedRef.current){var P=null,m=L(i,u,f,n,t,r);if(o.effects)try{P=x(i,o.effects(m),y.fullStateEffects)}catch(e){return Promise.reject(new a(e))}c.current=!1;var w=g(P,E(v.current,h,r,!1),h,"effects",!0,m,!1,!1,!0),b=y.notifySuccess||l.notifySuccess;if(b){var R=void 0;o.getSuccessMessage&&(R=o.getSuccessMessage(m)),R&&b(R)}return delete s.current[h][r],o.sideEffects&&o.sideEffects(_(m,i,u,d,l.notifyWarning,S)),c.current||w(),J(e.initializedRef,h,d.current,p.current),t}}(e,G,r,A)})).catch((function(r){return z(e,G,r,A)}));return null!=Z&&(T[g]=t(t({},T[g]||{}),((b={})[G||W]={abortController:Y,promise:Z,refArgs:I===c.REUSE&&A.length>0?i([],A,!0):[]},b))),p.current=S(p.current,g,G,null),P(null!=Z||B?n.current:void 0,null==Z?void 0:E(v.current,g,G,!0),g,"preEffects",!0,H,!1),Z}}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 c=t[r].apply(t,i.args);null==c?J(e,r,t,n):c.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){if(null==(null==o?void 0:o.derivedState))return!1;var i=!1,c=l.comparator,u=n.current.deps,a=R(e,null,r),f=Object.keys(o.derivedState);return n.current.state=f.reduce((function(e,r){var l,f,s=!1,d=o.derivedState[r],v=u[r],p=null!==(f=null===(l=d.getDeps)||void 0===l?void 0:l.call(d,a))&&void 0!==f?f:[];(d.derivedDeps&&d.derivedDeps.forEach((function(r){var t,o,i;return p.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==v)?s=!0:s=-1!==p.findIndex((function(e,r){return v.length<r||!c(v[r],e)}));return i=i||s,u[r]=p,e[r]=s?o.derivedState[r].get(function(e,r){return t(t({},e),{ds:r,derived:r})}(a,e)):n.current.state[r],e}),{}),i}function H(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],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 h={},y=0,g=s;y<g.length;y++){h[p=g[y]]=e.derivedState[p]}e.derivedState=h}}function V(e,r,n,o,i,c,u,a,f,s){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,h=b(e.current),y=[],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),P=d(n.current);S.length!==P.length?(console.warn("options.onPropsChange.getDeps returned array must be stable (same length)"),v=!0):S.forEach((function(e,r){g(e,P[r])||(v=!0,E.push(r))}))}if(v){var m=j(h,r,n,E,i,f,u),w=x(e,null===(l=t.effects)||void 0===l?void 0:l.call(t,m),c.fullStateEffects);null!=w&&(p=!0,h.current=w,a(h.current,void 0,"onPropsChange","effects",!1,m,!0,f)),t.sideEffects&&y.push({index:i,indices:E,ctx:m})}}})),!p&&d&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0),y.length>0&&y.forEach((function(e){v[e.index].sideEffects(t(t({},T(k(e.ctx,i),h)),{indices:e.indices}))}))}else null!=(null==c?void 0:c.derivedState)&&a(void 0,void 0,"onPropsChange","effects",!1,null,!0)}function q(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}}export{W as DEFAULT_PROMISE_ID,a as EffectError,P as ParallelActionError,k as addContextActions,T as addStateToContext,F as areSameArgs,m 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,h as isAsyncGetPromiseAction,y 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};
2
2
  //# sourceMappingURL=impl.js.map
package/es/index.d.ts CHANGED
@@ -1,169 +1,169 @@
1
- import { Context } from 'react';
2
- import { setGlobalConfig, GlobalConfig, ParallelActionError, EffectError } from './impl';
3
- import type { DecoratedActions, StoreActions, AsyncAction, SyncAction, SimpleSyncAction, LoadingMap, ErrorMap, LoadingParallelMap, ErrorParallelMap, StoreOptions, AsyncActionPromise, InvocationContext, GetPromiseInvocationContext, EffectsInvocationContext, InvocationContextActions, WarningNotifyFunc, ErrorEffectsInvocationContext, OnMountInvocationContext, ContextBase, OnPropsChangeEffectsContext, ContextState, Middleware, AbortActionCallback, LoadingProps, ErrorProps, LoadingMapProps, StoreConfig, ClearErrorFunc } from './types';
4
- import { ConflictPolicy } from './types';
5
- export type { DecoratedActions, StoreActions, LoadingMap, ErrorMap, LoadingParallelMap, ErrorParallelMap, StoreOptions, AsyncActionPromise as AsynchActionPromise, InvocationContext, GetPromiseInvocationContext, EffectsInvocationContext, InvocationContextActions, WarningNotifyFunc, ErrorEffectsInvocationContext, OnMountInvocationContext, ContextBase, OnPropsChangeEffectsContext, SyncAction, AsyncAction, SimpleSyncAction, ContextState, GlobalConfig, Middleware, LoadingProps, AbortActionCallback, ErrorProps, LoadingMapProps, StoreConfig, };
6
- export { setGlobalConfig, EffectError, ParallelActionError, ConflictPolicy };
7
- export type IsLoadingFunc<A> = (...props: (keyof A | [keyof A, string])[]) => boolean;
8
- type StateListenerUnregister = () => void;
9
- export type StoreApi<S, A extends DecoratedActions, P, DS = {}> = {
10
- /**
11
- * The current store state (with derived state if any).
12
- */
13
- readonly state: S & DS;
14
- /**
15
- * The store decorator (ie. ready to use) actions.
16
- */
17
- readonly actions: A;
18
- /**
19
- * A boolean that indicates that at least one asynchronous action is loading.
20
- */
21
- readonly loading: boolean;
22
- /**
23
- * A map of loading actions map (computed on property access)
24
- */
25
- readonly loadingMap: LoadingMap<A>;
26
- /**
27
- * A map of loading actions map (computed on property access)
28
- */
29
- readonly errorMap: ErrorMap<A>;
30
- /**
31
- * A map of parallel loading actions map (computed on property access)
32
- */
33
- readonly loadingParallelMap: LoadingParallelMap<A>;
34
- /**
35
- * A map of parallel actions error map (computed on property access)
36
- */
37
- readonly errorParallelMap: ErrorParallelMap<A>;
38
- /**
39
- * Initializes the store with the specified props
40
- */
41
- readonly init: (p: P) => void;
42
- /**
43
- * Update the store with the specified props
44
- */
45
- readonly setProps: (p: P) => void;
46
- /**
47
- * Destroy the store
48
- */
49
- readonly destroy: () => void;
50
- /**
51
- * Abort an asynchronous action marked as abortable.
52
- */
53
- readonly abortAction: (actionName: keyof A, promiseId?: string) => boolean;
54
- /**
55
- * Add a store state change listener
56
- */
57
- readonly addStateListener: (listener: StateListener) => StateListenerUnregister;
58
- /**
59
- * A function that takes a list of action names or a tuple of [action name, promiseId] and returns <code>true</code> if any action is loading.
60
- */
61
- readonly isLoading: IsLoadingFunc<A>;
62
- /**
63
- * A function to clear error on an action marked as <code>isErrorManaged: true</code>.
64
- */
65
- readonly clearError: ClearErrorFunc<A>;
66
- /**
67
- * @returns The store configuration
68
- */
69
- getConfig: () => StoreConfig<S, A, P, DS>;
70
- /**
71
- * Invoke onMountDeferred + onPropChange flagged onMountDeferred
72
- * @internal
73
- */
74
- invokeOnMountDeferred: () => void;
75
- /**
76
- * Returns a snapshot of the current state of the store.
77
- */
78
- getSnapshot(): StateListenerContext<S, DS, A>;
79
- };
80
- export type StateListenerContext<S, DS, A extends DecoratedActions> = S & DS & A & Pick<StoreApi<S, A, any, DS>, 'loading' | 'loadingMap' | 'errorParallelMap' | 'errorMap' | 'isLoading' | 'abortAction'>;
81
- export type StateListener = () => void;
82
- /**
83
- * Create a new store.
84
- * @param config Store configuration
85
- * @returns a store.
86
- */
87
- export declare function createStore<S, A extends DecoratedActions, P, DS = {}>(config: StoreConfig<S, A, P, DS>): StoreApi<S, A, P, DS>;
88
- /**
89
- * A react hook to get a state slice. Will be refresh if, and only if, one property of the slice has changed
90
- * @param store The store to listen to.
91
- * @param slicerFunc A function that returns a state slice
92
- * @returns The state slice.
93
- */
94
- export declare function useStoreSlice<S, A extends DecoratedActions, P, DS, SLICE>(store: StoreApi<S, A, P, DS>, slicerFunc: (ctx: StateListenerContext<S, DS, A>) => SLICE): SLICE;
95
- /**
96
- * A react hook to get a state slice. Will be refresh if, and only if, one property of the slice has changed
97
- * @param store The store to listen to.
98
- * @param properties list of properties to extract from store
99
- * @returns The state slice.
100
- */
101
- export declare function useStoreSlice<S, A extends DecoratedActions, P, DS, K extends keyof StateListenerContext<S, DS, A>, KL extends keyof A>(store: StoreApi<S, A, P, DS>, properties: K[]): Pick<StateListenerContext<S, DS, A>, K> & {
102
- loadingMap: LoadingMap<Pick<A, KL>>;
103
- };
104
- /**
105
- * Binds the store to this component.
106
- * Store will NOT be destroyed when component is unmouted.
107
- * The component will be refreshed for every change in the store.
108
- *
109
- * @param store The store to listen to.
110
- * @returns The store.
111
- */
112
- export declare function useStore<S, A extends DecoratedActions, P, DS>(store: StoreApi<S, A, P, DS>): StoreApi<S, A, P, DS>;
113
- /**
114
- * A react hook to get a state slice from a store context.
115
- * The component be refreshed if, and only if, one property of the slice has changed.
116
- * @param store The store to listen to.
117
- * @param slicerFunc A function that returns a state slice
118
- * @returns The state slice.
119
- */
120
- export declare function useStoreContextSlice<S, A extends DecoratedActions, P, DS, SLICE>(storeContext: Context<StoreApi<S, A, P, DS>>, slicerFunc: (ctx: StateListenerContext<S, DS, A>) => SLICE): SLICE;
121
- /**
122
- * A react hook to get a state slice from a store context.
123
- * The component will be refreshed if, and only if, one property of the slice has changed.
124
- * @param store The store to listen to.
125
- * @param properties list of properties to extract from store
126
- * @returns The state slice.
127
- */
128
- export declare function useStoreContextSlice<S, A extends DecoratedActions, P, DS, K extends keyof StateListenerContext<S, DS, A>, KL extends keyof A>(storeContext: Context<StoreApi<S, A, P, DS>>, properties: K[]): Pick<StateListenerContext<S, DS, A>, K> & {
129
- loadingMap: LoadingMap<Pick<A, KL>>;
130
- };
131
- /**
132
- * Creates and manages a local store.
133
- * The component will be refreshed for every change in the store.
134
- * Store will NOT be destroyed when component is unmouted.
135
- *
136
- * @param getInitialState Function to compute initial state from props.
137
- * @param props Owner component props to update state or react on prop changes
138
- * @param refreshOnUpdate Whether refresh the component if store state changes.
139
- * @returns The Store
140
- */
141
- export declare function useLocalStore<S, A extends DecoratedActions, P, DS = {}>(config: StoreConfig<S, A, P, DS>, props?: P, refreshOnUpdate?: boolean): StoreApi<S, A, P, DS>;
142
- /**
143
- * Binds a store to a component and inject props.
144
- * This hook must be the only one used with this store (or else props will the set several times).
145
- * The component will be refreshed for every change in the store.
146
- * Store will NOT be destroyed when component is unmouted.
147
- *
148
- * @param getInitialState Function to compute initial state from props.
149
- * @param actionImpl Actions implementation.
150
- * @param props Owner component props to update state or react on prop changes
151
- * @returns the Store.
152
- */
153
- export declare function useBindStore<S, A extends DecoratedActions, P, DS = {}>(store: StoreApi<S, A, P, DS>, props?: P): StoreApi<S, A, P, DS>;
154
- export default useLocalStore;
155
- /**
156
- * Creates a new object by picking properties to the object passed a first parameter
157
- * @param obj Object to pick properties from
158
- * @param props Property names
159
- * @returns a new object containing only picked properties
160
- */
161
- export declare function pick<T, K extends keyof T>(obj: T, props: K[]): Pick<T, K>;
162
- /**
163
- * Returns a function that creates a new object by picking properties to the object passed as first parameter.
164
- * To be used in conjunction of <code>useStoreSlice</code> like in example:
165
- * const s = useStoreSlice(myStore, slice('list', 'addItem'));
166
- * @param props Property names
167
- * @returns a function to pick property from an passed object.
168
- */
169
- export declare function slice<T, K extends keyof T>(...props: K[]): (obj: T) => Pick<T, K>;
1
+ import { Context } from 'react';
2
+ import { setGlobalConfig, GlobalConfig, ParallelActionError, EffectError } from './impl';
3
+ import type { DecoratedActions, StoreActions, AsyncAction, SyncAction, SimpleSyncAction, LoadingMap, ErrorMap, LoadingParallelMap, ErrorParallelMap, StoreOptions, AsyncActionPromise, InvocationContext, GetPromiseInvocationContext, EffectsInvocationContext, InvocationContextActions, WarningNotifyFunc, ErrorEffectsInvocationContext, OnMountInvocationContext, ContextBase, OnPropsChangeEffectsContext, ContextState, Middleware, AbortActionCallback, LoadingProps, ErrorProps, LoadingMapProps, StoreConfig, ClearErrorFunc } from './types';
4
+ import { ConflictPolicy } from './types';
5
+ export type { DecoratedActions, StoreActions, LoadingMap, ErrorMap, LoadingParallelMap, ErrorParallelMap, StoreOptions, AsyncActionPromise as AsynchActionPromise, InvocationContext, GetPromiseInvocationContext, EffectsInvocationContext, InvocationContextActions, WarningNotifyFunc, ErrorEffectsInvocationContext, OnMountInvocationContext, ContextBase, OnPropsChangeEffectsContext, SyncAction, AsyncAction, SimpleSyncAction, ContextState, GlobalConfig, Middleware, LoadingProps, AbortActionCallback, ErrorProps, LoadingMapProps, StoreConfig, };
6
+ export { setGlobalConfig, EffectError, ParallelActionError, ConflictPolicy };
7
+ export type IsLoadingFunc<A> = (...props: (keyof A | [keyof A, string])[]) => boolean;
8
+ type StateListenerUnregister = () => void;
9
+ export type StoreApi<S, A extends DecoratedActions, P, DS = {}> = {
10
+ /**
11
+ * The current store state (with derived state if any).
12
+ */
13
+ readonly state: S & DS;
14
+ /**
15
+ * The store decorator (ie. ready to use) actions.
16
+ */
17
+ readonly actions: A;
18
+ /**
19
+ * A boolean that indicates that at least one asynchronous action is loading.
20
+ */
21
+ readonly loading: boolean;
22
+ /**
23
+ * A map of loading actions map (computed on property access)
24
+ */
25
+ readonly loadingMap: LoadingMap<A>;
26
+ /**
27
+ * A map of loading actions map (computed on property access)
28
+ */
29
+ readonly errorMap: ErrorMap<A>;
30
+ /**
31
+ * A map of parallel loading actions map (computed on property access)
32
+ */
33
+ readonly loadingParallelMap: LoadingParallelMap<A>;
34
+ /**
35
+ * A map of parallel actions error map (computed on property access)
36
+ */
37
+ readonly errorParallelMap: ErrorParallelMap<A>;
38
+ /**
39
+ * Initializes the store with the specified props
40
+ */
41
+ readonly init: (p: P) => void;
42
+ /**
43
+ * Update the store with the specified props
44
+ */
45
+ readonly setProps: (p: P) => void;
46
+ /**
47
+ * Destroy the store
48
+ */
49
+ readonly destroy: () => void;
50
+ /**
51
+ * Abort an asynchronous action marked as abortable.
52
+ */
53
+ readonly abortAction: (actionName: keyof A, promiseId?: string) => boolean;
54
+ /**
55
+ * Add a store state change listener
56
+ */
57
+ readonly addStateListener: (listener: StateListener) => StateListenerUnregister;
58
+ /**
59
+ * A function that takes a list of action names or a tuple of [action name, promiseId] and returns <code>true</code> if any action is loading.
60
+ */
61
+ readonly isLoading: IsLoadingFunc<A>;
62
+ /**
63
+ * A function to clear error on an action marked as <code>isErrorManaged: true</code>.
64
+ */
65
+ readonly clearError: ClearErrorFunc<A>;
66
+ /**
67
+ * @returns The store configuration
68
+ */
69
+ getConfig: () => StoreConfig<S, A, P, DS>;
70
+ /**
71
+ * Invoke onMountDeferred + onPropChange flagged onMountDeferred
72
+ * @internal
73
+ */
74
+ invokeOnMountDeferred: () => void;
75
+ /**
76
+ * Returns a snapshot of the current state of the store.
77
+ */
78
+ getSnapshot(): StateListenerContext<S, DS, A, P>;
79
+ };
80
+ export type StateListenerContext<S, DS, A extends DecoratedActions, P> = P & S & DS & A & Pick<StoreApi<S, A, any, DS>, 'loading' | 'loadingMap' | 'errorParallelMap' | 'errorMap' | 'isLoading' | 'abortAction'>;
81
+ export type StateListener = () => void;
82
+ /**
83
+ * Create a new store.
84
+ * @param config Store configuration
85
+ * @returns a store.
86
+ */
87
+ export declare function createStore<S, A extends DecoratedActions, P, DS = {}>(config: StoreConfig<S, A, P, DS>): StoreApi<S, A, P, DS>;
88
+ /**
89
+ * A react hook to get a state slice. Will be refresh if, and only if, one property of the slice has changed
90
+ * @param store The store to listen to.
91
+ * @param slicerFunc A function that returns a state slice
92
+ * @returns The state slice.
93
+ */
94
+ export declare function useStoreSlice<S, A extends DecoratedActions, P, DS, SLICE>(store: StoreApi<S, A, P, DS>, slicerFunc: (ctx: StateListenerContext<S, DS, A, P>) => SLICE): SLICE;
95
+ /**
96
+ * A react hook to get a state slice. Will be refresh if, and only if, one property of the slice has changed
97
+ * @param store The store to listen to.
98
+ * @param properties list of properties to extract from store
99
+ * @returns The state slice.
100
+ */
101
+ export declare function useStoreSlice<S, A extends DecoratedActions, P, DS, K extends keyof StateListenerContext<S, DS, A, P>, KL extends keyof A>(store: StoreApi<S, A, P, DS>, properties: K[]): Pick<StateListenerContext<S, DS, A, P>, K> & {
102
+ loadingMap: LoadingMap<Pick<A, KL>>;
103
+ };
104
+ /**
105
+ * Binds the store to this component.
106
+ * Store will NOT be destroyed when component is unmouted.
107
+ * The component will be refreshed for every change in the store.
108
+ *
109
+ * @param store The store to listen to.
110
+ * @returns The store.
111
+ */
112
+ export declare function useStore<S, A extends DecoratedActions, P, DS>(store: StoreApi<S, A, P, DS>): StoreApi<S, A, P, DS>;
113
+ /**
114
+ * A react hook to get a state slice from a store context.
115
+ * The component be refreshed if, and only if, one property of the slice has changed.
116
+ * @param store The store to listen to.
117
+ * @param slicerFunc A function that returns a state slice
118
+ * @returns The state slice.
119
+ */
120
+ export declare function useStoreContextSlice<S, A extends DecoratedActions, P, DS, SLICE>(storeContext: Context<StoreApi<S, A, P, DS>>, slicerFunc: (ctx: StateListenerContext<S, DS, A, P>) => SLICE): SLICE;
121
+ /**
122
+ * A react hook to get a state slice from a store context.
123
+ * The component will be refreshed if, and only if, one property of the slice has changed.
124
+ * @param store The store to listen to.
125
+ * @param properties list of properties to extract from store
126
+ * @returns The state slice.
127
+ */
128
+ export declare function useStoreContextSlice<S, A extends DecoratedActions, P, DS, K extends keyof StateListenerContext<S, DS, A, P>, KL extends keyof A>(storeContext: Context<StoreApi<S, A, P, DS>>, properties: K[]): Pick<StateListenerContext<S, DS, A, P>, K> & {
129
+ loadingMap: LoadingMap<Pick<A, KL>>;
130
+ };
131
+ /**
132
+ * Creates and manages a local store.
133
+ * The component will be refreshed for every change in the store.
134
+ * Store will NOT be destroyed when component is unmouted.
135
+ *
136
+ * @param getInitialState Function to compute initial state from props.
137
+ * @param props Owner component props to update state or react on prop changes
138
+ * @param refreshOnUpdate Whether refresh the component if store state changes.
139
+ * @returns The Store
140
+ */
141
+ export declare function useLocalStore<S, A extends DecoratedActions, P, DS = {}>(config: StoreConfig<S, A, P, DS>, props?: P, refreshOnUpdate?: boolean): StoreApi<S, A, P, DS>;
142
+ /**
143
+ * Binds a store to a component and inject props.
144
+ * This hook must be the only one used with this store (or else props will the set several times).
145
+ * The component will be refreshed for every change in the store.
146
+ * Store will NOT be destroyed when component is unmouted.
147
+ *
148
+ * @param getInitialState Function to compute initial state from props.
149
+ * @param actionImpl Actions implementation.
150
+ * @param props Owner component props to update state or react on prop changes
151
+ * @returns the Store.
152
+ */
153
+ export declare function useBindStore<S, A extends DecoratedActions, P, DS = {}>(store: StoreApi<S, A, P, DS>, props?: P): StoreApi<S, A, P, DS>;
154
+ export default useLocalStore;
155
+ /**
156
+ * Creates a new object by picking properties to the object passed a first parameter
157
+ * @param obj Object to pick properties from
158
+ * @param props Property names
159
+ * @returns a new object containing only picked properties
160
+ */
161
+ export declare function pick<T, K extends keyof T>(obj: T, props: K[]): Pick<T, K>;
162
+ /**
163
+ * Returns a function that creates a new object by picking properties to the object passed as first parameter.
164
+ * To be used in conjunction of <code>useStoreSlice</code> like in example:
165
+ * const s = useStoreSlice(myStore, slice('list', 'addItem'));
166
+ * @param props Property names
167
+ * @returns a function to pick property from an passed object.
168
+ */
169
+ export declare function slice<T, K extends keyof T>(...props: K[]): (obj: T) => Pick<T, K>;
package/es/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import r,{useMemo as n,useContext as e,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 w=function(){return w=Object.assign||function(r){for(var n,e=1,t=arguments.length;e<t;e++)for(var u in n=arguments[e])Object.prototype.hasOwnProperty.call(n,u)&&(r[u]=n[u]);return r},w.apply(this,arguments)};function M(r){var n={exports:{}};return r(n,n.exports),n.exports}"function"==typeof SuppressedError&&SuppressedError;var D="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},j=r.useState,V=r.useEffect,x=r.useLayoutEffect,k=r.useDebugValue;function C(r){var n=r.getSnapshot;r=r.value;try{var e=n();return!D(r,e)}catch(r){return!0}}var P="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(r,n){return n()}:function(r,n){var e=n(),t=j({inst:{value:e,getSnapshot:n}}),u=t[0].inst,o=t[1];return x((function(){u.value=e,u.getSnapshot=n,C(u)&&o({inst:u})}),[r,e,n]),V((function(){return C(u)&&o({inst:u}),r((function(){C(u)&&o({inst:u})}))}),[r]),k(e),e},I={useSyncExternalStore:void 0!==r.useSyncExternalStore?r.useSyncExternalStore:P},B=M((function(n,e){"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=r,t=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function u(r){for(var n=arguments.length,e=new Array(n>1?n-1:0),u=1;u<n;u++)e[u-1]=arguments[u];!function(r,n,e){var u=t.ReactDebugCurrentFrame.getStackAddendum();""!==u&&(n+="%s",e=e.concat([u]));var o=e.map((function(r){return String(r)}));o.unshift("Warning: "+n),Function.prototype.apply.call(console[r],console,o)}("error",r,e)}var o="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},c=n.useState,i=n.useEffect,a=n.useLayoutEffect,l=n.useDebugValue,f=!1,s=!1;function d(r){var n=r.getSnapshot,e=r.value;try{var t=n();return!o(e,t)}catch(r){return!0}}var v=!!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?function(r,n,e){return n()}:function(r,e,t){f||void 0!==n.startTransition&&(f=!0,u("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 v=e();if(!s){var p=e();o(v,p)||(u("The result of getSnapshot should be cached to avoid an infinite loop"),s=!0)}var O=c({inst:{value:v,getSnapshot:e}}),_=O[0].inst,E=O[1];return a((function(){_.value=v,_.getSnapshot=e,d(_)&&E({inst:_})}),[r,v,e]),i((function(){d(_)&&E({inst:_});return r((function(){d(_)&&E({inst:_})}))}),[r]),l(v),v},p=void 0!==n.useSyncExternalStore?n.useSyncExternalStore:v;e.useSyncExternalStore=p,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()})),G=I,K=B,N=M((function(r){"production"===process.env.NODE_ENV?r.exports=G:r.exports=K})),H=N;var U,W="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},F=N.useSyncExternalStore,Y=r.useRef,z=r.useEffect,J=r.useMemo,q=r.useDebugValue,Q={useSyncExternalStoreWithSelector:function(r,n,e,t,u){var o=Y(null);if(null===o.current){var c={hasValue:!1,value:null};o.current=c}else c=o.current;o=J((function(){function r(r){if(!a){if(a=!0,o=r,r=t(r),void 0!==u&&c.hasValue){var n=c.value;if(u(n,r))return i=n}return i=r}if(n=i,W(o,r))return n;var e=t(r);return void 0!==u&&u(n,e)?n:(o=r,i=e)}var o,i,a=!1,l=void 0===e?null:e;return[function(){return r(n())},null===l?void 0:function(){return r(l())}]}),[n,e,t,u]);var i=F(r,o[0],o[1]);return z((function(){c.hasValue=!0,c.value=i}),[i]),q(i),i}},X=M((function(n,e){"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=r,t=N;var u="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},o=t.useSyncExternalStore,c=n.useRef,i=n.useEffect,a=n.useMemo,l=n.useDebugValue;e.useSyncExternalStoreWithSelector=function(r,n,e,t,f){var s,d=c(null);null===d.current?(s={hasValue:!1,value:null},d.current=s):s=d.current;var v=a((function(){var r,o,c=!1,i=function(n){if(!c){c=!0,r=n;var e=t(n);if(void 0!==f&&s.hasValue){var i=s.value;if(f(i,e))return o=i,i}return o=e,e}var a=o;if(u(r,n))return a;var l=t(n);return void 0!==f&&f(a,l)?a:(r=n,o=l,l)},a=void 0===e?null:e;return[function(){return i(n())},null===a?void 0:function(){return i(a())}]}),[n,e,t,f]),p=v[0],O=v[1],_=o(r,p,O);return i((function(){s.hasValue=!0,s.value=_}),[_]),l(_),_},"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()})),Z=Q,$=X,rr=M((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 nr=rr.useSyncExternalStoreWithSelector,er=H.useSyncExternalStore;function tr(r){var n=i(),e=i(),t=i(),u=i(),o=i(),c=i(),M=i(),D=i(),j=i(),V=i(),x=i(),k=i(!1),C=i(),P=i([]),I=r.actions,B=r.middlewares,G=void 0===B?[]:B,K=function(r,n){var e={};for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&n.indexOf(t)<0&&(e[t]=r[t]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(t=Object.getOwnPropertySymbols(r);u<t.length;u++)n.indexOf(t[u])<0&&Object.prototype.propertyIsEnumerable.call(r,t[u])&&(e[t[u]]=r[t[u]])}return e}(r,["actions","middlewares"]),N=a(r)?r.getInitialState:function(){return r.initialState},H={},U=0;function W(){C.current=w(w(w(w({},u.current),n.current),e.current.state),{isLoading:X,abortAction:Z,loading:D.current,loadingMap:M.current,loadingParallelMap:c.current,errorMap:$(),errorParallelMap:j.current}),Object.keys(H).forEach((function(r){var n;null===(n=H[r])||void 0===n||n.call(H)}))}function F(r){n.current=r,g(n,t,e,K),W()}function Y(r){c.current=r,M.current=function(){if(k.current){var r=c.current;return Object.keys(r).reduce((function(n,e){var t=r[e];return n[e]=null!=Object.keys(t).find((function(r){return!!t[r]})),n}),{})}return null}(),D.current=function(){if(k.current){var r=c.current;return null!=Object.keys(r).find((function(n){var e=r[n];return Object.keys(e).find((function(r){return!!e[r]}))}))}return null}()}function z(c){var i;if(!k.current){t.current=c,k.current=!0,n.current=N(c);var a=new Set(null==K?void 0:K.initialActionsMarkedLoading),l=Object.keys(I).reduce((function(r,n){var e;return r[n]=a.has(n)?((e={})[S]=!0,e):{},r}),{});Y(l),j.current=Object.keys(I).reduce((function(r,n){return r[n]={},r}),{}),o.current={},V.current={},x.current={},e.current={state:null,deps:{}},(null==K?void 0:K.derivedState)&&y(K),P.current=[];var f={options:K,get state(){return n.current},actions:I,setState:F};r.logEnabled&&T&&G.push(T());var s=function(r){var n,e=r();P.current.push(e),null===(n=e.init)||void 0===n||n.call(e,f)};null===(i=_.defaultMiddlewares)||void 0===i||i.forEach(s),null==G||G.forEach(s),g(n,t,e,K),E(n,e,t,null,u,K,N,q,!0,!1),(null==K?void 0:K.onMount)&&K.onMount(L(n,e,t,u)),W()}}function J(r,n){void 0===n&&(n=S);var e=j.current[r];(null==e?void 0:e[n])&&delete e[n],W()}function q(r,u,o,i,a,l,f,s){void 0===f&&(f=!1),void 0===s&&(s=!1);var d=!1;if(k.current){var v=u,p=!!a&&b(v||c.current,[o,null==l?void 0:l.promiseId]),O=A(P.current,n.current,r,p,o,i,a,l),_=O.newState,E=O.newLoading;if(null!=_&&(d=!0,n.current=_),p!==E&&(v=m(v,o,(null==l?void 0:l.promiseId)||S,E),0===Object.keys(v).length&&(v=void 0)),null!=v&&(d=!0,Y(v)),d)g(n,t,e,K),s||(n.current=_||n.current,W());else if(f){g(n,t,e,K)&&!s&&W()}}}var Q=Object.keys(I);function X(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return b.apply(void 0,function(r,n,e){if(e||2===arguments.length)for(var t,u=0,o=n.length;u<o;u++)!t&&u in n||(t||(t=Array.prototype.slice.call(n,0,u)),t[u]=n[u]);return r.concat(t||Array.prototype.slice.call(n))}([c.current],r,!1))}function Z(r,n){var e,t;void 0===n&&(n=S);var u=null===(t=null===(e=V.current[r])||void 0===e?void 0:e[n])||void 0===t?void 0:t.abortController,o=!1;return u&&(u.abort(),o=!0),o}function $(){return k.current?R(j.current):null}return u.current=Q.reduce((function(r,i){var a=I[i];return l(a)?r[i]=f(i,a,n,e,t,k,K,q):s(a)?r[i]=d({actionName:i,stateRef:n,derivedStateRef:e,propsRef:t,loadingParallelMapRef:c,errorMapRef:j,actionsRef:u,promisesRef:V,conflictActionsRef:x,initializedRef:k,options:K,setState:q,clearError:J,action:v(a)}):p(a)&&(r[i]=O(i,a,n,e,t,u,k,o,K,q,J)),r}),{}),{getConfig:function(){return w({},r)},setProps:function(r){var o=t.current;t.current=r,k.current?E(n,e,t,o,u,K,N,q,!1,!1):z(r)},addStateListener:function(r){var n="".concat(U++);return H[n]=r,function(){delete H[n]}},init:z,destroy:function(){var r;if(k.current){var u=Object.keys(I).reduce((function(r,n){var e=I[n];if(s(e)&&e.abortable){var t=c.current[n],u=Object.keys(t).reduce((function(r,e){return t[e]&&(Z(n,e),r.push(e)),r}),[]);u.length>0&&(r[n]=u)}return r}),{});null===(r=null==K?void 0:K.onUnmount)||void 0===r||r.call(K,h(n,e,t,u)),Object.keys(o.current).forEach((function(r){clearTimeout(o.current[r])})),t.current=null,n.current=null,c.current=null,M.current=null,D.current=null,j.current=null,V.current=null,x.current=null,e.current=null,C.current=null,o.current=null,H={},k.current=!1,P.current.forEach((function(r){var n;null===(n=r.destroy)||void 0===n||n.call(r)})),P.current=[]}},isLoading:X,abortAction:Z,clearError:J,getSnapshot:function(){return C.current},invokeOnMountDeferred:function(){E(n,e,t,null,u,K,N,q,!0,!0),(null==K?void 0:K.onMountDeferred)&&K.onMountDeferred(L(n,e,t,u))},get actions(){return u.current},get state(){return k.current?w(w({},n.current),e.current.state):null},get loading(){return D.current},get loadingMap(){return M.current},get errorMap(){return $()},get loadingParallelMap(){return k.current?c.current:null},get errorParallelMap(){return k.current?j.current:null}}}function ur(r,e){var t=n((function(){return e instanceof Function?e:function(r){return lr(r,e)}}),[e]);return nr(r.addStateListener,r.getSnapshot,r.getSnapshot,t,_.comparator)}function or(r){return er(r.addStateListener,r.getSnapshot),r}function cr(r,n){return ur(e(r),n)}function ir(r,n,e){void 0===e&&(e=!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,n,e){void 0===n&&(n=null),void 0===e&&(e=!0);var u=c((function(r){return r>100?0:r+1}),0)[1],i=t(r);return r.setProps(n),o((function(){r.init(n)}),[]),o((function(){if(e)return i.current.addStateListener((function(){u()}))}),[e]),r}(i.current,n,e)}function ar(r,n){return r.setProps(n),er(r.addStateListener,r.getSnapshot),o((function(){r.init(n)}),[]),u((function(){r.invokeOnMountDeferred()}),[r]),r}function lr(r,n){return n.reduce((function(n,e){return r.hasOwnProperty(e)&&(n[e]=r[e]),n}),{})}function fr(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return function(n){return lr(n,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};
1
+ import r,{useMemo as n,useContext as e,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 R,buildErrorMap as m}from"./impl";export{EffectError,ParallelActionError,setGlobalConfig}from"./impl";import{logDetailedEffects as T}from"./development";var w=function(){return w=Object.assign||function(r){for(var n,e=1,t=arguments.length;e<t;e++)for(var u in n=arguments[e])Object.prototype.hasOwnProperty.call(n,u)&&(r[u]=n[u]);return r},w.apply(this,arguments)};function M(r){var n={exports:{}};return r(n,n.exports),n.exports}"function"==typeof SuppressedError&&SuppressedError;var D="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},j=r.useState,V=r.useEffect,x=r.useLayoutEffect,k=r.useDebugValue;function C(r){var n=r.getSnapshot;r=r.value;try{var e=n();return!D(r,e)}catch(r){return!0}}var P="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(r,n){return n()}:function(r,n){var e=n(),t=j({inst:{value:e,getSnapshot:n}}),u=t[0].inst,o=t[1];return x((function(){u.value=e,u.getSnapshot=n,C(u)&&o({inst:u})}),[r,e,n]),V((function(){return C(u)&&o({inst:u}),r((function(){C(u)&&o({inst:u})}))}),[r]),k(e),e},I={useSyncExternalStore:void 0!==r.useSyncExternalStore?r.useSyncExternalStore:P},B=M((function(n,e){"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=r,t=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function u(r){for(var n=arguments.length,e=new Array(n>1?n-1:0),u=1;u<n;u++)e[u-1]=arguments[u];!function(r,n,e){var u=t.ReactDebugCurrentFrame.getStackAddendum();""!==u&&(n+="%s",e=e.concat([u]));var o=e.map((function(r){return String(r)}));o.unshift("Warning: "+n),Function.prototype.apply.call(console[r],console,o)}("error",r,e)}var o="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},c=n.useState,i=n.useEffect,a=n.useLayoutEffect,l=n.useDebugValue,f=!1,s=!1;function d(r){var n=r.getSnapshot,e=r.value;try{var t=n();return!o(e,t)}catch(r){return!0}}var v=!!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?function(r,n,e){return n()}:function(r,e,t){f||void 0!==n.startTransition&&(f=!0,u("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 v=e();if(!s){var p=e();o(v,p)||(u("The result of getSnapshot should be cached to avoid an infinite loop"),s=!0)}var O=c({inst:{value:v,getSnapshot:e}}),_=O[0].inst,E=O[1];return a((function(){_.value=v,_.getSnapshot=e,d(_)&&E({inst:_})}),[r,v,e]),i((function(){d(_)&&E({inst:_});return r((function(){d(_)&&E({inst:_})}))}),[r]),l(v),v},p=void 0!==n.useSyncExternalStore?n.useSyncExternalStore:v;e.useSyncExternalStore=p,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()})),G=I,K=B,N=M((function(r){"production"===process.env.NODE_ENV?r.exports=G:r.exports=K})),H=N;var F,U="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},W=N.useSyncExternalStore,Y=r.useRef,z=r.useEffect,J=r.useMemo,q=r.useDebugValue,Q={useSyncExternalStoreWithSelector:function(r,n,e,t,u){var o=Y(null);if(null===o.current){var c={hasValue:!1,value:null};o.current=c}else c=o.current;o=J((function(){function r(r){if(!a){if(a=!0,o=r,r=t(r),void 0!==u&&c.hasValue){var n=c.value;if(u(n,r))return i=n}return i=r}if(n=i,U(o,r))return n;var e=t(r);return void 0!==u&&u(n,e)?n:(o=r,i=e)}var o,i,a=!1,l=void 0===e?null:e;return[function(){return r(n())},null===l?void 0:function(){return r(l())}]}),[n,e,t,u]);var i=W(r,o[0],o[1]);return z((function(){c.hasValue=!0,c.value=i}),[i]),q(i),i}},X=M((function(n,e){"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=r,t=N;var u="function"==typeof Object.is?Object.is:function(r,n){return r===n&&(0!==r||1/r==1/n)||r!=r&&n!=n},o=t.useSyncExternalStore,c=n.useRef,i=n.useEffect,a=n.useMemo,l=n.useDebugValue;e.useSyncExternalStoreWithSelector=function(r,n,e,t,f){var s,d=c(null);null===d.current?(s={hasValue:!1,value:null},d.current=s):s=d.current;var v=a((function(){var r,o,c=!1,i=function(n){if(!c){c=!0,r=n;var e=t(n);if(void 0!==f&&s.hasValue){var i=s.value;if(f(i,e))return o=i,i}return o=e,e}var a=o;if(u(r,n))return a;var l=t(n);return void 0!==f&&f(a,l)?a:(r=n,o=l,l)},a=void 0===e?null:e;return[function(){return i(n())},null===a?void 0:function(){return i(a())}]}),[n,e,t,f]),p=v[0],O=v[1],_=o(r,p,O);return i((function(){s.hasValue=!0,s.value=_}),[_]),l(_),_},"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()})),Z=Q,$=X,rr=M((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"}(F||(F={}));var nr=rr.useSyncExternalStoreWithSelector,er=H.useSyncExternalStore;function tr(r){var n=i(),e=i(),t=i(),u=i(),o=i(),c=i(),M=i(),D=i(),j=i(),V=i(),x=i(),k=i(!1),C=i(!1),P=i(),I=i([]),B=r.actions,G=r.middlewares,K=void 0===G?[]:G,N=function(r,n){var e={};for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&n.indexOf(t)<0&&(e[t]=r[t]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(t=Object.getOwnPropertySymbols(r);u<t.length;u++)n.indexOf(t[u])<0&&Object.prototype.propertyIsEnumerable.call(r,t[u])&&(e[t[u]]=r[t[u]])}return e}(r,["actions","middlewares"]),H=a(r)?r.getInitialState:function(){return r.initialState},F={},U=0;function W(){P.current=w(w(w(w(w({},t.current),u.current),n.current),e.current.state),{isLoading:Z,abortAction:$,loading:D.current,loadingMap:M.current,loadingParallelMap:c.current,errorMap:rr(),errorParallelMap:j.current}),Object.keys(F).forEach((function(r){var n;null===(n=F[r])||void 0===n||n.call(F)}))}function Y(r){n.current=r,g(n,t,e,N),W()}function z(r){c.current=r,M.current=function(){if(k.current){var r=c.current;return Object.keys(r).reduce((function(n,e){var t=r[e];return n[e]=null!=Object.keys(t).find((function(r){return!!t[r]})),n}),{})}return null}(),D.current=function(){if(k.current){var r=c.current;return null!=Object.keys(r).find((function(n){var e=r[n];return Object.keys(e).find((function(r){return!!e[r]}))}))}return null}()}function J(c){var i;if(!k.current){t.current=c,k.current=!0,n.current=H(c);var a=new Set(null==N?void 0:N.initialActionsMarkedLoading),l=Object.keys(B).reduce((function(r,n){var e;return r[n]=a.has(n)?((e={})[S]=!0,e):{},r}),{});z(l),j.current=Object.keys(B).reduce((function(r,n){return r[n]={},r}),{}),o.current={},V.current={},x.current={},e.current={state:null,deps:{}},(null==N?void 0:N.derivedState)&&y(N),I.current=[];var f={options:N,get state(){return n.current},actions:B,setState:Y};r.logEnabled&&T&&K.push(T());var s=function(r){var n,e=r();I.current.push(e),null===(n=e.init)||void 0===n||n.call(e,f)};null===(i=_.defaultMiddlewares)||void 0===i||i.forEach(s),null==K||K.forEach(s),g(n,t,e,N),E(n,e,t,null,u,N,H,Q,!0,!1),(null==N?void 0:N.onMount)&&N.onMount(L(n,e,t,u)),W()}}function q(r,n){void 0===n&&(n=S);var e=j.current[r];(null==e?void 0:e[n])&&delete e[n],W()}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 W;var p=u,O=!!a&&b(p||c.current,[o,null==l?void 0:l.promiseId]),_=A(I.current,n.current,r,O,o,i,a,l),E=_.newState,y=_.newLoading;if(null!=E&&(v=!0,n.current=E),O!==y&&(p=R(p,o,(null==l?void 0:l.promiseId)||S,y),0===Object.keys(p).length&&(p=void 0)),null!=p&&(v=!0,z(p)),v)g(n,t,e,N),s||(n.current=E||n.current,d||(C.current=!0,W()));else if(f){!g(n,t,e,N)||s||d||(C.current=!0,W())}return W}var X=Object.keys(B);function Z(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return b.apply(void 0,function(r,n,e){if(e||2===arguments.length)for(var t,u=0,o=n.length;u<o;u++)!t&&u in n||(t||(t=Array.prototype.slice.call(n,0,u)),t[u]=n[u]);return r.concat(t||Array.prototype.slice.call(n))}([c.current],r,!1))}function $(r,n){var e,t;void 0===n&&(n=S);var u=null===(t=null===(e=V.current[r])||void 0===e?void 0:e[n])||void 0===t?void 0:t.abortController,o=!1;return u&&(u.abort(),o=!0),o}function rr(){return k.current?m(j.current):null}return u.current=X.reduce((function(r,i){var a=B[i];return l(a)?r[i]=f(i,a,n,e,t,k,N,Q):s(a)?r[i]=d({actionName:i,stateRef:n,derivedStateRef:e,propsRef:t,loadingParallelMapRef:c,errorMapRef:j,actionsRef:u,promisesRef:V,conflictActionsRef:x,initializedRef:k,stateFlagRef:C,options:N,setState:Q,clearError:q,action:v(a)}):p(a)&&(r[i]=O(i,a,n,e,t,u,k,o,N,Q,q)),r}),{}),{getConfig:function(){return w({},r)},setProps:function(r){var o=t.current;t.current=r,k.current?E(n,e,t,o,u,N,H,Q,!1,!1):J(r)},addStateListener:function(r){var n="".concat(U++);return F[n]=r,function(){delete F[n]}},init:J,destroy:function(){var r;if(k.current){var u=Object.keys(B).reduce((function(r,n){var e=B[n];if(s(e)&&e.abortable){var t=c.current[n],u=Object.keys(t).reduce((function(r,e){return t[e]&&($(n,e),r.push(e)),r}),[]);u.length>0&&(r[n]=u)}return r}),{});null===(r=null==N?void 0:N.onUnmount)||void 0===r||r.call(N,h(n,e,t,u)),Object.keys(o.current).forEach((function(r){clearTimeout(o.current[r])})),t.current=null,n.current=null,c.current=null,M.current=null,D.current=null,j.current=null,V.current=null,x.current=null,e.current=null,P.current=null,o.current=null,F={},k.current=!1,I.current.forEach((function(r){var n;null===(n=r.destroy)||void 0===n||n.call(r)})),I.current=[]}},isLoading:Z,abortAction:$,clearError:q,getSnapshot:function(){return P.current},invokeOnMountDeferred:function(){E(n,e,t,null,u,N,H,Q,!0,!0),(null==N?void 0:N.onMountDeferred)&&N.onMountDeferred(L(n,e,t,u))},get actions(){return u.current},get state(){return k.current?w(w({},n.current),e.current.state):null},get loading(){return D.current},get loadingMap(){return M.current},get errorMap(){return rr()},get loadingParallelMap(){return k.current?c.current:null},get errorParallelMap(){return k.current?j.current:null}}}function ur(r,e){var t=n((function(){return e instanceof Function?e:function(r){return lr(r,e)}}),[e]);return nr(r.addStateListener,r.getSnapshot,void 0,t,_.comparator)}function or(r){return er(r.addStateListener,r.getSnapshot),r}function cr(r,n){return ur(e(r),n)}function ir(r,n,e){void 0===e&&(e=!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,n,e){void 0===n&&(n=null),void 0===e&&(e=!0);var u=c((function(r){return r>100?0:r+1}),0)[1],i=t(r);return r.setProps(n),o((function(){r.init(n)}),[]),o((function(){if(e)return i.current.addStateListener((function(){u()}))}),[e]),r}(i.current,n,e)}function ar(r,n){return r.setProps(n),er(r.addStateListener,r.getSnapshot),o((function(){r.init(n)}),[]),u((function(){r.invokeOnMountDeferred()}),[r]),r}function lr(r,n){return n.reduce((function(n,e){return r.hasOwnProperty(e)&&(n[e]=r[e]),n}),{})}function fr(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return function(n){return lr(n,r)}}export{F 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};
2
2
  //# sourceMappingURL=index.js.map