Commit 2236a1a3 by Hugo Häggmark

Minor change to Action interfaces

parent 94ce065f
......@@ -2,23 +2,23 @@ import { Action } from 'redux';
const allActionCreators: string[] = [];
export interface GrafanaAction<Payload> extends Action {
export interface ActionOf<Payload> extends Action {
readonly type: string;
readonly payload: Payload;
}
export interface GrafanaActionCreator<Payload> {
export interface ActionCreator<Payload> {
readonly type: string;
(payload: Payload): GrafanaAction<Payload>;
(payload: Payload): ActionOf<Payload>;
}
export interface ActionCreatorFactory<Payload> {
create: () => GrafanaActionCreator<Payload>;
create: () => ActionCreator<Payload>;
}
export const actionCreatorFactory = <Payload>(type: string): ActionCreatorFactory<Payload> => {
const create = (): GrafanaActionCreator<Payload> => {
return Object.assign((payload: Payload): GrafanaAction<Payload> => ({ type, payload }), { type });
const create = (): ActionCreator<Payload> => {
return Object.assign((payload: Payload): ActionOf<Payload> => ({ type, payload }), { type });
};
if (allActionCreators.some(t => (t && type ? t.toLocaleUpperCase() === type.toLocaleUpperCase() : false))) {
......
import { reducerFactory } from './reducerFactory';
import { actionCreatorFactory, GrafanaAction } from './actionCreatorFactory';
import { actionCreatorFactory, ActionOf } from './actionCreatorFactory';
interface DummyReducerState {
n: number;
......@@ -37,7 +37,7 @@ describe('reducerFactory', () => {
describe('when reducer is called with no state', () => {
describe('and with an action that the handler can not handle', () => {
it('then the resulting state should be intial state', () => {
const result = dummyReducer(undefined as DummyReducerState, {} as GrafanaAction<any>);
const result = dummyReducer(undefined as DummyReducerState, {} as ActionOf<any>);
expect(result).toEqual(dummyReducerIntialState);
});
......@@ -56,7 +56,7 @@ describe('reducerFactory', () => {
describe('when reducer is called with a state', () => {
describe('and with an action that the handler can not handle', () => {
it('then the resulting state should be intial state', () => {
const result = dummyReducer(dummyReducerIntialState, {} as GrafanaAction<any>);
const result = dummyReducer(dummyReducerIntialState, {} as ActionOf<any>);
expect(result).toEqual(dummyReducerIntialState);
});
......
import { GrafanaAction, GrafanaActionCreator } from './actionCreatorFactory';
import { ActionOf, ActionCreator } from './actionCreatorFactory';
import { Reducer } from 'redux';
export interface HandlerConfig<State, Payload> {
filter: GrafanaActionCreator<Payload>;
handler: (state: State, action: GrafanaAction<Payload>) => State;
filter: ActionCreator<Payload>;
handler: (state: State, action: ActionOf<Payload>) => State;
}
export interface AddHandler<State> {
......@@ -11,7 +11,7 @@ export interface AddHandler<State> {
}
export interface CreateReducer<State> extends AddHandler<State> {
create: () => Reducer<State, GrafanaAction<any>>;
create: () => Reducer<State, ActionOf<any>>;
}
export const reducerFactory = <State>(initialState: State): AddHandler<State> => {
......@@ -27,8 +27,8 @@ export const reducerFactory = <State>(initialState: State): AddHandler<State> =>
return instance;
};
const create = (): Reducer<State, GrafanaAction<any>> => {
const reducer: Reducer<State, GrafanaAction<any>> = (state: State = initialState, action: GrafanaAction<any>) => {
const create = (): Reducer<State, ActionOf<any>> => {
const reducer: Reducer<State, ActionOf<any>> = (state: State = initialState, action: ActionOf<any>) => {
const validHandlers = allHandlerConfigs
.filter(config => config.filter.type === action.type)
.map(config => config.handler);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment