-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patheventBus.tsx
45 lines (40 loc) · 1.16 KB
/
eventBus.tsx
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
type EventBusHandler = (...args: unknown[]) => void;
/**
* @todo replace with an off-the-shelf EventEmitter
* A helper class for delivering messages/events across the app
*/
export class _EventBus {
handlers: { event: string; handler: EventBusHandler }[] = [];
/**
* Subscribe to a library event.
*
* @param {string} event The event to subscribe to.
* @param {function} handler The handler function which takes in 1 argument.
* @returns The unsubscribe function.
*/
subscribe(event: string, handler: EventBusHandler) {
const eventHandler = { event, handler };
this.handlers.push(eventHandler);
// Return the unsubscribe function
return () => {
const index = this.handlers.indexOf(eventHandler);
if (index > -1) {
this.handlers.splice(index, 1);
}
};
}
/**
* Publish an event.
*
* @param {string} event The event to publish
* @param {any} args An argument.
*/
publish(event: string, args: unknown) {
this.handlers.forEach((eventHandler) => {
if (eventHandler.event === event) {
eventHandler.handler(args);
}
});
}
}
export const EventBus = new _EventBus();