Files
aurorabot/shared/lib/events.ts
syntaxbullet 5863418ae9 refactor: replace dynamic imports with event bus pattern
Replace 12 dynamic `await import()` calls with domain events emitted
through the existing systemEvents bus, breaking circular dependencies
between services (economy/inventory/leveling -> quest, * -> dashboard).

- Add `emitAsync` to SystemEventEmitter for sequential listener awaiting,
  preserving DB transaction atomicity for quest progress tracking
- Add DOMAIN event constants (BALANCE_CHANGED, XP_GAINED, ITEM_COLLECTED,
  ITEM_USED, TRANSFER_COMPLETED, DAILY_CLAIMED, TRIVIA_*, EXAM_PASSED)
- Create shared/lib/eventWiring.ts to register all domain event listeners
- Convert quest event calls to `await systemEvents.emitAsync()` (5 calls)
- Convert dashboard event calls to `systemEvents.emit()` fire-and-forget (5 calls)
- Convert exam.service.ts userService import to static import (1 call)
- Convert dashboard.service.ts events import to static import (1 call)
- Leave inventory.service.ts validateAndExecuteEffect import unchanged (Task 3)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 12:59:15 +01:00

49 lines
1.6 KiB
TypeScript

import { EventEmitter } from "node:events";
/**
* Global system event bus for cross-module communication.
* Used for real-time dashboard updates and domain event decoupling.
*/
class SystemEventEmitter extends EventEmitter {
/**
* Emit an event and await all listeners sequentially.
* Used for domain events that must preserve transaction atomicity
* (e.g., quest progress tracking within the caller's DB transaction).
*/
async emitAsync(event: string, ...args: any[]): Promise<boolean> {
const listeners = this.listeners(event);
for (const listener of listeners) {
await (listener as Function)(...args);
}
return listeners.length > 0;
}
}
export const systemEvents = new SystemEventEmitter();
export const EVENTS = {
DASHBOARD: {
STATS_UPDATE: "dashboard:stats_update",
NEW_EVENT: "dashboard:new_event",
},
ACTIONS: {
RELOAD_COMMANDS: "actions:reload_commands",
CLEAR_CACHE: "actions:clear_cache",
MAINTENANCE_MODE: "actions:maintenance_mode",
},
QUEST: {
COMPLETED: "quest:completed",
},
DOMAIN: {
BALANCE_CHANGED: "domain:balance_changed",
XP_GAINED: "domain:xp_gained",
ITEM_COLLECTED: "domain:item_collected",
ITEM_USED: "domain:item_used",
TRANSFER_COMPLETED: "domain:transfer_completed",
DAILY_CLAIMED: "domain:daily_claimed",
TRIVIA_STARTED: "domain:trivia_started",
TRIVIA_WON: "domain:trivia_won",
EXAM_PASSED: "domain:exam_passed",
},
} as const;