UNPKG

redux-loop

Version:

Sequence your effects naturally and purely by returning them from your reducers.

2 lines (1 loc) 11.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).ReduxLoop={})}(this,(function(t){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function n(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function r(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function o(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?r(Object(o),!0).forEach((function(e){n(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):r(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function i(t,e){if(null==t)return{};var n,r,o=function(t,e){if(null==t)return{};var n,r,o={},i=Object.keys(t);for(r=0;r<i.length;r++)n=i[r],e.indexOf(n)>=0||(o[n]=t[n]);return o}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(r=0;r<i.length;r++)n=i[r],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(o[n]=t[n])}return o}function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],r=!0,o=!1,i=void 0;try{for(var u,c=t[Symbol.iterator]();!(r=(u=c.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==c.return||c.return()}finally{if(o)throw i}}return n}(t,e)||a(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(t){return function(t){if(Array.isArray(t))return s(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||a(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,e){if(t){if("string"==typeof t)return s(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(t,e):void 0}}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var l,f=Array.prototype.concat,d=function(t){return f.apply([],t)},p=Symbol("isCmd"),h=Symbol("dispatch"),y=Symbol("getState"),v="RUN",m="ACTION",g="SET_TIMEOUT",b="SET_INTERVAL",O="LIST",A="MAP",w="NONE";function S(t){return!!t&&!!t[p]}function C(t,n){var r,o,i=n.dispatch,u=n.getState,c=n.loopConfig,a=t.successActionCreator||function(){};r=t.failActionCreator?function(e){return c.DONT_LOG_ERRORS_ON_HANDLED_FAILURES||console.error(e),t.failActionCreator(e)}:console.error;try{var s=t.func.apply(void 0,function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1?arguments[1]:void 0,n=arguments.length>2?arguments[2]:void 0;return t.map((function(t){return t===h?e:t===y?n:t}))}(t.args,i,u));if((o=s)&&"object"===e(o)&&"function"==typeof o.then&&!t.forceSync)return s.then(a,r).then((function(t){return t?[t]:[]}));var l=a(s);return l?Promise.resolve([l]):null}catch(e){if(!t.failActionCreator)throw console.error(e),e;var f=r(e);return f?Promise.resolve([f]):null}}function j(t,e,n){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};return I(t,{dispatch:e,wrappedDispatch:e,getState:n,loopConfig:r})}function I(t,e){switch(t.type){case v:return C(t,e);case m:return Promise.resolve([t.actionToDispatch]);case g:case b:return function(t,e){var n,r=function(){var n=I(t.nestedCmd,e);n&&n.then((function(t){t.forEach((function(t){return e.wrappedDispatch(t)}))}))};return n=t.type===b?setInterval(r,t.delayMs):setTimeout(r,t.delayMs),t.scheduledActionCreator?Promise.resolve([t.scheduledActionCreator(n)]):null}(t,e);case O:return t.sequence?function(t,e){var n=t.cmds,r=t.batch,o=void 0!==r&&r,i=n.length?n[0]:null;if(!i)return null;var u=new Promise((function(t){var r=I(i,e);(r=r||Promise.resolve([])).then((function(r){(o?Promise.resolve():Promise.all(r.map((function(t){return e.wrappedDispatch(t)})))).then((function(){var i=I(N(n.slice(1),{batch:o,sequence:!0}),e);i?i.then((function(e){t(r.concat(e))})):t(r)}))}))})).then(d);return o?u:u.then((function(){return[]}))}(t,e):function(t,e){var n=t.cmds,r=t.batch,o=void 0!==r&&r,i=n.map((function(t){var n=I(t,e);return!n||o?n:n.then((function(t){return Promise.all(t.map((function(t){return e.wrappedDispatch(t)})))}))})).filter((function(t){return t}));return 0===i.length?null:Promise.all(i).then(d).then((function(t){return o?t:[]}))}(t,e);case A:var n=I(t.nestedCmd,o(o({},e),{},{wrappedDispatch:function(n){return e.wrappedDispatch(t.tagger.apply(t,c(t.args).concat([n])))}}));return n?n.then((function(e){return e.map((function(e){return t.tagger.apply(t,c(t.args).concat([e]))}))})):null;case w:return null;default:throw new Error("Invalid Cmd type ".concat(t.type))}}function E(t){var e=t.result,n=t.success;return n&&this.successActionCreator?this.successActionCreator(e):!n&&this.failActionCreator?this.failActionCreator(e):null}function P(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(r.testInvariants)throw Error("Redux Loop: Detected usage of Cmd.run's testInvariants option in production code. This should only be used in tests.");r.testInvariants;var u=i(r,["testInvariants"]);return Object.freeze(o((n(e={},p,!0),n(e,"type",v),n(e,"func",t),n(e,"simulate",E),e),u))}function T(){return this.actionToDispatch}function R(t,e,r,o,i){var u;return Object.freeze((n(u={},p,!0),n(u,"type",o),n(u,"nestedCmd",t),n(u,"delayMs",e),n(u,"scheduledActionCreator",r.scheduledActionCreator),n(u,"simulate",D),u))}function D(t,e){var n=this.nestedCmd.simulate(e),r=null;return Array.isArray(n)?r=n:n&&(r=[n]),this.scheduledActionCreator?[this.scheduledActionCreator(t)].concat(r):r}function _(t){return d(this.cmds.map((function(e,n){return e.simulate(t[n])})).filter((function(t){return t})))}function N(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(r.testInvariants)throw Error("Redux Loop: Detected usage of Cmd.list's testInvariants option in production code. This should only be used in tests.");r.testInvariants;var u=i(r,["testInvariants"]);return Object.freeze(o((n(e={},p,!0),n(e,"type",O),n(e,"cmds",t),n(e,"simulate",_),e),u))}function L(t){var e=this,n=this.nestedCmd.simulate(t);return Array.isArray(n)?n.map((function(t){return e.tagger.apply(e,c(e.args).concat([t]))})):n?this.tagger.apply(this,c(this.args).concat([n])):null}var x={run:P,action:function(t){var e;return Object.freeze((n(e={},p,!0),n(e,"type",m),n(e,"actionToDispatch",t),n(e,"simulate",T),e))},setTimeout:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return R(t,e,n,g)},setInterval:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return R(t,e,n,b)},clearTimeout:function(t){return P(clearTimeout,{args:[t]})},clearInterval:function(t){return P(clearInterval,{args:[t]})},list:N,map:function(t,e){for(var r,o=arguments.length,i=new Array(o>2?o-2:0),u=2;u<o;u++)i[u-2]=arguments[u];return Object.freeze((n(r={},p,!0),n(r,"type",A),n(r,"tagger",e),n(r,"nestedCmd",t),n(r,"args",i),n(r,"simulate",L),r))},none:Object.freeze((n(l={},p,!0),n(l,"type",w),n(l,"simulate",(function(){return null})),l)),dispatch:h,getState:y},M=function(t){return Array.isArray(t)&&2===t.length&&S(t[1])&&!S(t[0])},U=function(t){return M(t)?t[1]:null},z=function(t){return M(t)?t[0]:t},k=function(t,e){return[t,e]},G=function(t){return M(t)?t:k(t,x.none)},H=function(t,e){return"\nException thrown when running Cmds from action: ".concat(t,".\n\nThrown exception: \n").concat(e,"\n")},q={DONT_LOG_ERRORS_ON_HANDLED_FAILURES:!1,ENABLE_THUNK_MIGRATION:!1};function B(t){switch(t.length){case 0:return x.none;case 1:return t[0];default:return x.list(t)}}function F(t){return function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments.length>1?arguments[1]:void 0,i=arguments.length,u=new Array(i>2?i-2:0),c=2;c<i;c++)u[c-2]=arguments[c];var a=[],s=!1,l=Object.keys(t).reduce((function(e,i){var c=t[i];if(!c)return s||(e=o({},e),s=!0),delete e[i],e;var l=c.apply(void 0,[e[i],r].concat(u));return M(l)&&(a.push(U(l)),l=z(l)),e[i]!==l&&s?e[i]=l:e[i]!==l&&(e=o(o({},e),{},n({},i,l)),s=!0),e}),e);return k(l,B(a))}}function K(){return console.warning("mergeChildReducers is deprecated. Use reduceReducers or combineReducers instead."),V.apply(void 0,arguments)}function V(t,e,r){for(var i=arguments.length,u=new Array(i>3?i-3:0),c=3;c<i;c++)u[c-3]=arguments[c];var a,s=t;M(s)&&(a=U(s),s=z(s));var l=a?[a]:[],f=!1,d=Object.keys(r).reduce((function(t,i){var c=r[i];if(!c)return f||(t=o({},t),f=!0),delete t[i],t;var a=c.apply(void 0,[t[i],e].concat(u));return M(a)&&(l.push(U(a)),a=z(a)),t[i]!==a&&f?t[i]=a:t[i]!==a&&(t=o(o({},t),{},n({},i,a)),f=!0),t}),s);return k(d,B(l))}var $=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return function(t,n){for(var r=arguments.length,o=new Array(r>2?r-2:0),i=2;i<r;i++)o[i-2]=arguments[i];var u=e.reduce((function(t,e){var r=e.apply(void 0,[t.newState,n].concat(o));return M(r)?{newState:z(r),cmds:[].concat(c(t.cmds),[U(r)])}:{newState:r,cmds:t.cmds}}),{newState:t,cmds:[]}),a=u.newState,s=u.cmds;return k(a,B(s))}};t.Cmd=x,t.DEPRECATED_mergeChildReducers=function(){return V.apply(void 0,arguments)},t.combineReducers=function(){return F.apply(void 0,arguments)},t.getCmd=U,t.getModel=z,t.install=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=Object.assign({},q,t);return function(t){return function(n,r,i){var c=u(G(r),2),a=c[0],s=c[1],l=[],f=function(t){return function(e,n){var r=t(e,n),o=u(G(r),2),i=o[0],c=o[1];return l.push({originalAction:n,cmd:c}),i}},d=t(f(n),a,i);function p(t){var n=t.originalAction,r=j(t.cmd,h,d.getState,e);return r?r.then((function(t){if(t.length)return Promise.all(t.map(h))})).catch((function(t){throw console.error(H(n.type,t)),t})):null}function h(t){if(e.ENABLE_THUNK_MIGRATION&&"function"==typeof t)return t(h,d.getState);var n,r,o=d.dispatch(t),i=l;return l=[],(n=i,r=n.map(p).filter((function(t){return t})),0===r.length?Promise.resolve():1===r.length?r[0]:Promise.all(r).then((function(){}))).then((function(){return o}))}return p({originalAction:{type:"@@ReduxLoop/INIT"},cmd:s}),o(o({},d),{},{dispatch:h,replaceReducer:function(t){return d.replaceReducer(f(t))}})}}},t.isLoop=M,t.liftState=G,t.loop=k,t.mergeChildReducers=function(){return K.apply(void 0,arguments)},t.reduceReducers=function(){return $.apply(void 0,arguments)},Object.defineProperty(t,"__esModule",{value:!0})}));