updux/src/updux.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-21 21:17:56 +00:00
import fp from 'lodash/fp';
import buildActions from './buildActions';
import buildInitial from './buildInitial';
import buildMutations from './buildMutations';
import buildCreateStore from './buildCreateStore';
import buildMiddleware from './buildMiddleware';
import buildUpreducer from './buildUpreducer';
export class Updux {
2019-10-22 14:55:54 +00:00
constructor(config) {
2019-10-21 21:17:56 +00:00
this.subduxes = fp.mapValues(
2019-10-22 14:55:54 +00:00
value => fp.isPlainObject(value) ? new Updux(value ) : value )(fp.getOr({},'subduxes',config)
);
2019-10-21 21:17:56 +00:00
this.actions = buildActions(
config.mutations,
config.effects,
fp.mergeAll( Object.values( this.subduxes ).map( ({ actions }) =>
actions ) )
)
this.initial = buildInitial(
config.initial, fp.mapValues( ({initial}) => initial )(this.subduxes)
);
this.mutations = buildMutations(
config.mutations, this.subduxes
);
this.upreducer = buildUpreducer(
this.initial, this.mutations
);
this.reducer = (state,action) => {
return this.upreducer(action)(state);
}
this.middleware = buildMiddleware(
config.effects,
this.actions,
config.subduxes,
);
this.createStore = buildCreateStore(this.reducer,this.initial,this.middleware,this.actions);
}
}
export default Updux;