import { EventEmitter } from "node:events"; const EVENTS = new EventEmitter(); EVENTS.setMaxListeners(100); export type EventPayload = { type: string; data: T; timestamp: string; }; export async function shutdownEventBus(): Promise { EVENTS.removeAllListeners(); } export async function publishEvent( eventType: string, data: unknown, ): Promise { const payload: EventPayload = { type: eventType, data, timestamp: new Date().toISOString(), }; try { EVENTS.emit(eventType, payload); } catch (error) { console.error("Failed to publish event:", error); throw error; } } export async function subscribeToEvent( eventType: string, handler: (data: T) => Promise, ): Promise { try { EVENTS.on(eventType, async (payload: EventPayload) => { try { await handler(payload.data); } catch (error) { console.error(`Error processing event ${eventType}:`, error); } }); } catch (error) { console.error("Failed to subscribe to event:", error); throw error; } } process.on("SIGTERM", () => { shutdownEventBus().catch(console.error); });