|
|
@@ -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);
|
|
|
};
|
|
|
|