/**
*
* @param {*} actionCreators action创建函数或者对象
* @param {*} dispath store上面的分发方法
*/
export default function bindActionCreators(actionCreators, dispath) {
if (typeof actionCreators === 'function') {
getAutoDispatchAction(actionCreators, dispath);
} else if (typeof actionCreators === 'object') {
const autoActionObject = {};
for (const key in actionCreators) {
if (Object.hasOwnProperty.call(actionCreators, key)) {
const action = actionCreators[key];
autoActionObject[key] = getAutoDispatchAction(action, dispath);
}
}
return autoActionObject;
}
else {
throw TypeError('action must be an object or function meas an action')
}
}
function getAutoDispatchAction(action, dispatch) {
return function (...arg) {
const autoAction = action(arg);
dispatch(autoAction);
}
}