ironbar-persea/frontend/app/ducks.js

46 lines
921 B
JavaScript
Raw Permalink Normal View History

2019-03-05 23:20:26 -06:00
import {combineReducers} from 'redux';
import {all, call} from 'redux-saga/effects';
export function createMapDispatchToProps(ducks) {
return (dispatch) => {
const map = {};
for (const i in ducks) {
map[i] = {};
for (const j in ducks[i].actions.creators) {
map[i][j] = (...args) => {
return dispatch(ducks[i].actions.creators[j](...args));
}
}
}
return map;
};
}
export function createReducer(ducks) {
const map = {};
for (const i in ducks) {
map[i] = ducks[i].reducer;
}
return combineReducers(map);
}
export function createSaga(ducks) {
const sagas = [];
for (const i in ducks) {
if (!ducks[i].saga) {
continue;
}
sagas.push(call(ducks[i].saga));
}
return function* () {
yield all(sagas);
}
}
export function compose(ducks) {
return {
reducer: createReducer(ducks),
saga: createSaga(ducks),
}
}