-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionregistry-eventbus-listener.ts
43 lines (40 loc) · 1.1 KB
/
actionregistry-eventbus-listener.ts
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
import { IAction } from '../action/types';
import { IEventbusListener } from './types';
/**
* The `IEventbusListener` first registers a number of actions that are then
* executed when the specified event is broadcast.
*/
export class ActionRegistryEventbusListener implements IEventbusListener {
private _actionRegistry = new Map<string, IAction[]>();
registerAction(
action: IAction,
eventName: string,
eventTopic?: string
): void {
if (eventTopic && eventTopic.length) {
eventName = `${eventName}:${eventTopic}`;
}
if (!this._actionRegistry.has(eventName)) {
this._actionRegistry.set(eventName, []);
}
this._actionRegistry.get(eventName)?.push(action);
}
handleEvent(
eventName: string,
eventTopic: string | undefined,
args: any[]
): void {
if (eventTopic) {
eventName = `${eventName}:${eventTopic}`;
}
const actions = this._actionRegistry.get(eventName);
if (actions) {
const operationData = {
eventArgs: args,
};
actions.forEach((action) => {
action.start(operationData);
});
}
}
}