sac.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.composeReducers = exports.subReducer = undefined;
  6. var _cowValueModel = require("./cow-value-model");
  7. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  8. var subReducer = exports.subReducer = function subReducer(key, reducer) {
  9. for (var _len = arguments.length, otherKeys = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  10. otherKeys[_key - 2] = arguments[_key];
  11. }
  12. var value = (0, _cowValueModel.cowValueModel)(key),
  13. otherParts = otherKeys.map(function (each) {
  14. return (0, _cowValueModel.cowValueModel)(each);
  15. });
  16. return function (state, action) {
  17. var newSubState = reducer.apply(undefined, [value(state), action].concat(_toConsumableArray(otherParts.map(function (value) {
  18. return value(state);
  19. }))));
  20. if (typeof newSubState === "undefined") {
  21. throw new Error("The '" + key + "' reducer must not return undefined.");
  22. }
  23. return value(state, newSubState);
  24. };
  25. };
  26. var composeReducers = exports.composeReducers = function composeReducers() {
  27. for (var _len2 = arguments.length, reducers = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  28. reducers[_key2] = arguments[_key2];
  29. }
  30. return function (state, action) {
  31. return reducers.reduce(function (x, r) {
  32. return r(x, action);
  33. }, state);
  34. };
  35. };