diff --git a/public/app/core/redux/reducerFactory.test.ts b/public/app/core/redux/reducerFactory.test.ts index 85b3939efad..7e1d5e350e0 100644 --- a/public/app/core/redux/reducerFactory.test.ts +++ b/public/app/core/redux/reducerFactory.test.ts @@ -27,10 +27,8 @@ const dummyActionCreator = actionCreatorFactory('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; }, }); diff --git a/public/app/core/redux/reducerFactory.ts b/public/app/core/redux/reducerFactory.ts index 0ce9ac95edd..9fcc52649e3 100644 --- a/public/app/core/redux/reducerFactory.ts +++ b/public/app/core/redux/reducerFactory.ts @@ -1,30 +1,25 @@ import { GrafanaAction, GrafanaActionCreator } from './actionCreatorFactory'; import { Reducer } from 'redux'; -export interface ActionHandler { - state: State; - action: GrafanaAction; +export interface HandlerConfig { + filter: GrafanaActionCreator; + handler: (state: State, action: GrafanaAction) => State; } -export interface ActionHandlerConfig { - creator: GrafanaActionCreator; - handler: (handler: ActionHandler) => State; +export interface AddHandler { + addHandler: (config: HandlerConfig) => CreateReducer; } -export interface AddActionHandler { - addHandler: (config: ActionHandlerConfig) => CreateReducer; -} - -export interface CreateReducer extends AddActionHandler { +export interface CreateReducer extends AddHandler { create: () => Reducer>; } -export const reducerFactory = (initialState: State): AddActionHandler => { - const allHandlerConfigs: Array> = []; +export const reducerFactory = (initialState: State): AddHandler => { + const allHandlerConfigs: Array> = []; - const addHandler = (config: ActionHandlerConfig): CreateReducer => { - 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 = (config: HandlerConfig): CreateReducer => { + 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 = (initialState: State): AddActionHandler> => { const reducer: Reducer> = (state: State = initialState, action: GrafanaAction) => { 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); };