Simplified inteface for reducerFactory

This commit is contained in:
Hugo Häggmark 2019-01-30 11:59:00 +01:00
parent 65fb77ce73
commit 94ce065f74
2 changed files with 19 additions and 26 deletions

View File

@ -27,10 +27,8 @@ const dummyActionCreator = actionCreatorFactory<DummyReducerState>('dummy').crea
const dummyReducer = reducerFactory(dummyReducerIntialState)
.addHandler({
creator: dummyActionCreator,
handler: ({ state, action }) => {
return { ...state, ...action.payload };
},
filter: dummyActionCreator,
handler: (state, action) => ({ ...state, ...action.payload }),
})
.create();
@ -79,16 +77,16 @@ describe('reducerFactory', () => {
describe('when a handler with the same creator is added', () => {
it('then is should throw', () => {
const faultyReducer = reducerFactory(dummyReducerIntialState).addHandler({
creator: dummyActionCreator,
handler: ({ state, action }) => {
filter: dummyActionCreator,
handler: (state, action) => {
return { ...state, ...action.payload };
},
});
expect(() => {
faultyReducer.addHandler({
creator: dummyActionCreator,
handler: ({ state }) => {
filter: dummyActionCreator,
handler: state => {
return state;
},
});

View File

@ -1,30 +1,25 @@
import { GrafanaAction, GrafanaActionCreator } from './actionCreatorFactory';
import { Reducer } from 'redux';
export interface ActionHandler<State, Payload> {
state: State;
action: GrafanaAction<Payload>;
export interface HandlerConfig<State, Payload> {
filter: GrafanaActionCreator<Payload>;
handler: (state: State, action: GrafanaAction<Payload>) => State;
}
export interface ActionHandlerConfig<State, Payload> {
creator: GrafanaActionCreator<Payload>;
handler: (handler: ActionHandler<State, Payload>) => State;
export interface AddHandler<State> {
addHandler: <Payload>(config: HandlerConfig<State, Payload>) => CreateReducer<State>;
}
export interface AddActionHandler<State> {
addHandler: <Payload>(config: ActionHandlerConfig<State, Payload>) => CreateReducer<State>;
}
export interface CreateReducer<State> extends AddActionHandler<State> {
export interface CreateReducer<State> extends AddHandler<State> {
create: () => Reducer<State, GrafanaAction<any>>;
}
export const reducerFactory = <State>(initialState: State): AddActionHandler<State> => {
const allHandlerConfigs: Array<ActionHandlerConfig<State, any>> = [];
export const reducerFactory = <State>(initialState: State): AddHandler<State> => {
const allHandlerConfigs: Array<HandlerConfig<State, any>> = [];
const addHandler = <Payload>(config: ActionHandlerConfig<State, Payload>): CreateReducer<State> => {
if (allHandlerConfigs.some(c => c.creator.type === config.creator.type)) {
throw new Error(`There is already a handlers defined with the type ${config.creator.type}`);
const addHandler = <Payload>(config: HandlerConfig<State, Payload>): CreateReducer<State> => {
if (allHandlerConfigs.some(c => c.filter.type === config.filter.type)) {
throw new Error(`There is already a handlers defined with the type ${config.filter.type}`);
}
allHandlerConfigs.push(config);
@ -35,11 +30,11 @@ export const reducerFactory = <State>(initialState: State): AddActionHandler<Sta
const create = (): Reducer<State, GrafanaAction<any>> => {
const reducer: Reducer<State, GrafanaAction<any>> = (state: State = initialState, action: GrafanaAction<any>) => {
const validHandlers = allHandlerConfigs
.filter(config => config.creator.type === action.type)
.filter(config => config.filter.type === action.type)
.map(config => config.handler);
return validHandlers.reduce((currentState, handler) => {
return handler({ state: currentState, action });
return handler(currentState, action);
}, state || initialState);
};