blob: faf576fd8d7412fd1b9bfce5e22fc227f0bcc598 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
export interface ActionDefinitions {
[key: string]: {
[key: string]: any;
};
}
export interface Actions {
[key: string]: {
[key: string]: {
(...args: any[]): void;
listeners: Function[];
notify: (params: any) => void;
listen: (listener: (params: any) => void) => void;
off: (listener: (params: any) => void) => void;
};
};
}
export const createActionsFromDefinitions = <T>(
actionDefinitions: ActionDefinitions,
validate: any,
): T => {
const actions = {};
for (const actionName of Object.keys(actionDefinitions)) {
const action = (params = {}) => {
const schema = actionDefinitions[actionName];
validate(schema, params, actionName);
action.notify(params);
};
actions[actionName] = action;
action.listeners = [];
action.listen = listener => action.listeners.push(listener);
action.off = listener => {
const { listeners } = action;
listeners.splice(listeners.indexOf(listener), 1);
};
action.notify = params => {
for (const listener of action.listeners) {
listener(params);
}
};
}
return actions as T;
};
export default (definitions, validate) => {
const newActions = {};
for (const scopeName of Object.keys(definitions)) {
newActions[scopeName] = createActionsFromDefinitions(
definitions[scopeName],
validate,
);
}
return newActions;
};
|