UNPKG

react-redux-with-path

Version:

A version of react-redux designed for cases that want to access sub-tree of the state. and take it as state in mapStateToProps and mapDispatchToProps

83 lines (73 loc) 3.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; exports.wrapActionCreatorWithPath = wrapActionCreatorWithPath; exports.wrapActionCreatorsWithPath = wrapActionCreatorsWithPath; exports.bindActionCreator = bindActionCreator; exports.default = bindActionCreators; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } // export only for test function wrapActionCreatorWithPath(actionCreator, path) { return function () { var res = actionCreator.apply(undefined, arguments); res.path = path; return res; }; } function wrapActionCreatorsWithPath(actionCreators, path) { return Object.keys(actionCreators).reduce(function (obj, key) { Object.assign(obj, _defineProperty({}, key, wrapActionCreatorWithPath(actionCreators[key], path))); return obj; }, {}); } function bindActionCreator(actionCreator, dispatch, path) { if (!!path) { return function () { return dispatch(wrapActionCreatorWithPath(actionCreator, path).apply(undefined, arguments)); }; } return function () { return dispatch(actionCreator.apply(undefined, arguments)); }; } /** * Turns an object whose values are action creators, into an object with the * same keys, but with every function wrapped into a `dispatch` call so they * may be invoked directly. This is just a convenience method, as you can call * `store.dispatch(MyActionCreators.doSomething())` yourself just fine. * * For convenience, you can also pass a single function as the first argument, * and get a function in return. * * @param {Function|Object} actionCreators An object whose values are action * creator functions. One handy way to obtain it is to use ES6 `import * as` * syntax. You may also pass a single function. * * @param {Function} dispatch The `dispatch` function available on your Redux * store. * * @returns {Function|Object} The object mimicking the original object, but with * every action creator wrapped into the `dispatch` call. If you passed a * function as `actionCreators`, the return value will also be a single * function. */ function bindActionCreators(actionCreators, dispatch, path) { if (typeof actionCreators === 'function') { return bindActionCreator(actionCreators, dispatch, path); } if ((typeof actionCreators === 'undefined' ? 'undefined' : _typeof(actionCreators)) !== 'object' || actionCreators === null) { throw new Error('bindActionCreators expected an object or a function, instead received ' + (actionCreators === null ? 'null' : typeof actionCreators === 'undefined' ? 'undefined' : _typeof(actionCreators)) + '. ' + 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'); } var keys = Object.keys(actionCreators); var boundActionCreators = {}; for (var i = 0; i < keys.length; i++) { var key = keys[i]; var actionCreator = actionCreators[key]; if (typeof actionCreator === 'function') { boundActionCreators[key] = bindActionCreator(actionCreator, dispatch, path); } } return boundActionCreators; }