sac.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.subEffex = exports.subMiddleware = exports.composeReducers = exports.subReducer = undefined;
  6. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  7. var _cowValueModel = require("./cow-value-model");
  8. 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); } }
  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 value = (0, _cowValueModel.cowValueModel)(key),
  14. otherParts = otherKeys.map(function (each) {
  15. return (0, _cowValueModel.cowValueModel)(each);
  16. });
  17. return function (state, action) {
  18. var newSubState = reducer.apply(undefined, [value(state), action].concat(_toConsumableArray(otherParts.map(function (value) {
  19. return value(state);
  20. }))));
  21. if (typeof newSubState === "undefined") {
  22. throw new Error("The '" + key + "' reducer must not return undefined.");
  23. }
  24. return value(state, newSubState);
  25. };
  26. };
  27. var composeReducers = exports.composeReducers = function composeReducers() {
  28. for (var _len2 = arguments.length, reducers = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  29. reducers[_key2] = arguments[_key2];
  30. }
  31. return function (state, action) {
  32. return reducers.reduce(function (x, r) {
  33. return r(x, action);
  34. }, state);
  35. };
  36. };
  37. var subMiddleware = exports.subMiddleware = function subMiddleware(keyOrSelectorFn, middleware) {
  38. var cow = typeof keyOrSelectorFn === "function" ? keyOrSelectorFn : (0, _cowValueModel.cowValueModel)(keyOrSelectorFn);
  39. return function (store) {
  40. for (var _len3 = arguments.length, rest = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
  41. rest[_key3 - 1] = arguments[_key3];
  42. }
  43. var _getState = store.getState;
  44. return middleware.apply(undefined, [_extends({}, store, { getState: function getState() {
  45. return cow(_getState());
  46. } })].concat(rest));
  47. };
  48. };
  49. var subEffex = exports.subEffex = function subEffex(keyOrSelectorFn, effects) {
  50. return effects.map(function (each) {
  51. return _extends({}, each, { effect: subMiddleware(keyOrSelectorFn, each.effect) });
  52. });
  53. };