diff options
Diffstat (limited to 'src/actions/lib/actions.ts')
-rw-r--r-- | src/actions/lib/actions.ts | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/actions/lib/actions.ts b/src/actions/lib/actions.ts index da27c2e2b..faf576fd8 100644 --- a/src/actions/lib/actions.ts +++ b/src/actions/lib/actions.ts | |||
@@ -1,8 +1,6 @@ | |||
1 | import PropTypes from 'prop-types'; | ||
2 | |||
3 | export interface ActionDefinitions { | 1 | export interface ActionDefinitions { |
4 | [key: string]: { | 2 | [key: string]: { |
5 | [key: string]: PropTypes.InferType<any>; | 3 | [key: string]: any; |
6 | }; | 4 | }; |
7 | } | 5 | } |
8 | 6 | ||
@@ -18,18 +16,19 @@ export interface Actions { | |||
18 | }; | 16 | }; |
19 | } | 17 | } |
20 | 18 | ||
21 | export const createActionsFromDefinitions = <T extends {}>( | 19 | export const createActionsFromDefinitions = <T>( |
22 | actionDefinitions: ActionDefinitions, | 20 | actionDefinitions: ActionDefinitions, |
23 | validate: any, | 21 | validate: any, |
24 | ): T => { | 22 | ): T => { |
25 | const actions = {}; | 23 | const actions = {}; |
26 | // eslint-disable-next-line unicorn/no-array-for-each | 24 | |
27 | Object.keys(actionDefinitions).forEach(actionName => { | 25 | for (const actionName of Object.keys(actionDefinitions)) { |
28 | const action = (params = {}) => { | 26 | const action = (params = {}) => { |
29 | const schema = actionDefinitions[actionName]; | 27 | const schema = actionDefinitions[actionName]; |
30 | validate(schema, params, actionName); | 28 | validate(schema, params, actionName); |
31 | action.notify(params); | 29 | action.notify(params); |
32 | }; | 30 | }; |
31 | |||
33 | actions[actionName] = action; | 32 | actions[actionName] = action; |
34 | action.listeners = []; | 33 | action.listeners = []; |
35 | action.listen = listener => action.listeners.push(listener); | 34 | action.listen = listener => action.listeners.push(listener); |
@@ -37,21 +36,24 @@ export const createActionsFromDefinitions = <T extends {}>( | |||
37 | const { listeners } = action; | 36 | const { listeners } = action; |
38 | listeners.splice(listeners.indexOf(listener), 1); | 37 | listeners.splice(listeners.indexOf(listener), 1); |
39 | }; | 38 | }; |
40 | action.notify = params => | 39 | action.notify = params => { |
41 | // eslint-disable-next-line unicorn/no-array-for-each | 40 | for (const listener of action.listeners) { |
42 | action.listeners.forEach(listener => listener(params)); | 41 | listener(params); |
43 | }); | 42 | } |
43 | }; | ||
44 | } | ||
45 | |||
44 | return actions as T; | 46 | return actions as T; |
45 | }; | 47 | }; |
46 | 48 | ||
47 | export default (definitions, validate) => { | 49 | export default (definitions, validate) => { |
48 | const newActions = {}; | 50 | const newActions = {}; |
49 | // eslint-disable-next-line unicorn/no-array-for-each | 51 | for (const scopeName of Object.keys(definitions)) { |
50 | Object.keys(definitions).forEach(scopeName => { | ||
51 | newActions[scopeName] = createActionsFromDefinitions( | 52 | newActions[scopeName] = createActionsFromDefinitions( |
52 | definitions[scopeName], | 53 | definitions[scopeName], |
53 | validate, | 54 | validate, |
54 | ); | 55 | ); |
55 | }); | 56 | } |
57 | |||
56 | return newActions; | 58 | return newActions; |
57 | }; | 59 | }; |