cqrs-eda
Version:
Lightweight CQRS and Event-Driven Architecture library using TypeScript decorators, handlers and typings. Perfect for scalable event-driven apps.
26 lines (25 loc) • 791 B
JavaScript
// Registry mapping event names to arrays of observer class constructors
const observerRegistry = new Map();
/**
* Decorator to register a class as an Observer for a specific event.
* Multiple observers can listen to the same event.
*
* @param eventName - The name of the event to observe.
* @returns Class decorator function.
*/
export function Observer(eventName) {
return function (target) {
if (!observerRegistry.has(eventName)) {
observerRegistry.set(eventName, []);
}
observerRegistry.get(eventName).push(target);
};
}
/**
* Retrieves the entire observer registry.
*
* @returns Map where keys are event names and values are arrays of observer constructors.
*/
export function getObserverRegistry() {
return observerRegistry;
}