redux-loop
Version:
Sequence your effects naturally and purely by returning them from your reducers.
48 lines (37 loc) • 2.27 kB
JavaScript
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { 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 _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import batchCmds from './batch-cmds';
import { loop, isLoop, getModel, getCmd } from './loop';
export default (function () {
for (var _len = arguments.length, reducers = new Array(_len), _key = 0; _key < _len; _key++) {
reducers[_key] = arguments[_key];
}
return function (prevState, action) {
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
var _reducers$reduce = reducers.reduce(function (prevResult, reducer) {
var result = reducer.apply(void 0, [prevResult.newState, action].concat(args));
if (isLoop(result)) {
return {
newState: getModel(result),
cmds: [].concat(_toConsumableArray(prevResult.cmds), [getCmd(result)])
};
}
return {
newState: result,
cmds: prevResult.cmds
};
}, {
newState: prevState,
cmds: []
}),
newState = _reducers$reduce.newState,
cmds = _reducers$reduce.cmds;
return loop(newState, batchCmds(cmds));
};
});