mirror of
https://github.com/thelounge/thelounge
synced 2024-12-02 00:39:19 +00:00
dd05ee3a65
Co-authored-by: Eric Nemchik <eric@nemchik.com> Co-authored-by: Pavel Djundik <xPaw@users.noreply.github.com>
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
const events = new Map();
|
|
|
|
class EventBus {
|
|
/**
|
|
* Register an event handler for the given type.
|
|
*
|
|
* @param {String} type Type of event to listen for.
|
|
* @param {Function} handler Function to call in response to given event.
|
|
*/
|
|
on(type: string, handler: (...evt: any[]) => void) {
|
|
if (events.has(type)) {
|
|
events.get(type).push(handler);
|
|
} else {
|
|
events.set(type, [handler]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove an event handler for the given type.
|
|
*
|
|
* @param {String} type Type of event to unregister `handler` from.
|
|
* @param {Function} handler Handler function to remove.
|
|
*/
|
|
off(type: string, handler: (...evt: any[]) => void) {
|
|
if (events.has(type)) {
|
|
events.set(
|
|
type,
|
|
events.get(type).filter((item: (...evt: any[]) => void) => item !== handler)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Invoke all handlers for the given type.
|
|
*
|
|
* @param {String} type The event type to invoke.
|
|
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler.
|
|
*/
|
|
emit(type: string, ...evt: any) {
|
|
if (events.has(type)) {
|
|
events
|
|
.get(type)
|
|
.slice()
|
|
.map((handler: (...evts: any[]) => void) => {
|
|
handler(...evt);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new EventBus();
|