1
1
import { Action } from 'redux' ;
2
2
3
- const allActionCreators : string [ ] = [ ] ;
3
+ const allActionCreators = new Set < string > ( ) ;
4
4
5
5
export interface ActionOf < Payload > extends Action {
6
6
readonly type : string ;
@@ -25,33 +25,21 @@ export interface NoPayloadActionCreatorFactory {
25
25
create : ( ) => NoPayloadActionCreator ;
26
26
}
27
27
28
- export const actionCreatorFactory = < Payload > ( type : string ) : ActionCreatorFactory < Payload > => {
29
- const create = ( ) : ActionCreator < Payload > => {
30
- return Object . assign ( ( payload : Payload ) : ActionOf < Payload > => ( { type, payload } ) , { type } ) ;
31
- } ;
32
-
33
- if ( allActionCreators . some ( t => ( t && type ? t . toLocaleUpperCase ( ) === type . toLocaleUpperCase ( ) : false ) ) ) {
34
- throw new Error ( `There is already an actionCreator defined with the type ${ type } ` ) ;
28
+ export function actionCreatorFactory < Payload extends undefined > ( type : string ) : NoPayloadActionCreatorFactory ;
29
+ export function actionCreatorFactory < Payload > ( type : string ) : ActionCreatorFactory < Payload > ;
30
+ export function actionCreatorFactory < Payload > ( type : string ) : ActionCreatorFactory < Payload > {
31
+ const upperCaseType = type . toLocaleUpperCase ( ) ;
32
+ if ( allActionCreators . has ( upperCaseType ) ) {
33
+ throw new Error ( `An actionCreator with type '${ type } ' has already been defined.` ) ;
35
34
}
36
35
37
- allActionCreators . push ( type ) ;
36
+ allActionCreators . add ( upperCaseType ) ;
38
37
39
- return { create } ;
40
- } ;
41
-
42
- export const noPayloadActionCreatorFactory = ( type : string ) : NoPayloadActionCreatorFactory => {
43
- const create = ( ) : NoPayloadActionCreator => {
44
- return Object . assign ( ( ) : ActionOf < undefined > => ( { type, payload : undefined } ) , { type } ) ;
38
+ const create = ( ) : ActionCreator < Payload > => {
39
+ return Object . assign ( ( payload : Payload ) : ActionOf < Payload > => ( { type, payload } ) , { type } ) ;
45
40
} ;
46
-
47
- if ( allActionCreators . some ( t => ( t && type ? t . toLocaleUpperCase ( ) === type . toLocaleUpperCase ( ) : false ) ) ) {
48
- throw new Error ( `There is already an actionCreator defined with the type ${ type } ` ) ;
49
- }
50
-
51
- allActionCreators . push ( type ) ;
52
-
53
41
return { create } ;
54
- } ;
42
+ }
55
43
56
44
export interface NoPayloadActionCreatorMock extends NoPayloadActionCreator {
57
45
calls : number ;
@@ -73,4 +61,4 @@ export const mockActionCreator = (creator: ActionCreator<any>) => {
73
61
} ;
74
62
75
63
// Should only be used by tests
76
- export const resetAllActionCreatorTypes = ( ) => ( allActionCreators . length = 0 ) ;
64
+ export const resetAllActionCreatorTypes = ( ) => allActionCreators . clear ( ) ;
0 commit comments