Debt: Simplifies actionCreatorFactory (#19433)

- Use sets to keep track of previously defined actionCreators
- Remove noPayloadActionCreatorFactory
This commit is contained in:
kay delaney
2019-09-26 11:41:37 +01:00
committed by GitHub
parent 651b59b81b
commit 0991032fef
8 changed files with 33 additions and 74 deletions

View File

@@ -1,3 +1,3 @@
import { noPayloadActionCreatorFactory } from 'app/core/redux';
import { actionCreatorFactory } from 'app/core/redux';
export const toggleLogActions = noPayloadActionCreatorFactory('TOGGLE_LOG_ACTIONS').create();
export const toggleLogActions = actionCreatorFactory('TOGGLE_LOG_ACTIONS').create();

View File

@@ -1,8 +1,4 @@
import {
actionCreatorFactory,
resetAllActionCreatorTypes,
noPayloadActionCreatorFactory,
} from './actionCreatorFactory';
import { actionCreatorFactory, resetAllActionCreatorTypes } from './actionCreatorFactory';
interface Dummy {
n: number;
@@ -18,7 +14,7 @@ interface Dummy {
const setup = (payload?: Dummy) => {
resetAllActionCreatorTypes();
const actionCreator = actionCreatorFactory<Dummy>('dummy').create();
const noPayloadactionCreator = noPayloadActionCreatorFactory('NoPayload').create();
const noPayloadactionCreator = actionCreatorFactory('NoPayload').create();
const result = actionCreator(payload);
const noPayloadResult = noPayloadactionCreator();
@@ -49,7 +45,7 @@ describe('actionCreatorFactory', () => {
setup(payload);
expect(() => {
noPayloadActionCreatorFactory('DuMmY').create();
actionCreatorFactory('DuMmY').create();
}).toThrow();
});
});

View File

@@ -1,6 +1,6 @@
import { Action } from 'redux';
const allActionCreators: string[] = [];
const allActionCreators = new Set<string>();
export interface ActionOf<Payload> extends Action {
readonly type: string;
@@ -25,33 +25,21 @@ export interface NoPayloadActionCreatorFactory {
create: () => NoPayloadActionCreator;
}
export const actionCreatorFactory = <Payload>(type: string): ActionCreatorFactory<Payload> => {
export function actionCreatorFactory<Payload extends undefined>(type: string): NoPayloadActionCreatorFactory;
export function actionCreatorFactory<Payload>(type: string): ActionCreatorFactory<Payload>;
export function actionCreatorFactory<Payload>(type: string): ActionCreatorFactory<Payload> {
const upperCaseType = type.toLocaleUpperCase();
if (allActionCreators.has(upperCaseType)) {
throw new Error(`An actionCreator with type '${type}' has already been defined.`);
}
allActionCreators.add(upperCaseType);
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))) {
throw new Error(`There is already an actionCreator defined with the type ${type}`);
}
allActionCreators.push(type);
return { create };
};
export const noPayloadActionCreatorFactory = (type: string): NoPayloadActionCreatorFactory => {
const create = (): NoPayloadActionCreator => {
return Object.assign((): ActionOf<undefined> => ({ type, payload: undefined }), { type });
};
if (allActionCreators.some(t => (t && type ? t.toLocaleUpperCase() === type.toLocaleUpperCase() : false))) {
throw new Error(`There is already an actionCreator defined with the type ${type}`);
}
allActionCreators.push(type);
return { create };
};
}
export interface NoPayloadActionCreatorMock extends NoPayloadActionCreator {
calls: number;
@@ -73,4 +61,4 @@ export const mockActionCreator = (creator: ActionCreator<any>) => {
};
// Should only be used by tests
export const resetAllActionCreatorTypes = () => (allActionCreators.length = 0);
export const resetAllActionCreatorTypes = () => allActionCreators.clear();