node.js 1.7 KB

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