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>
This commit is contained in:
@@ -2,6 +2,7 @@ import { users, userTimers } from "@db/schema";
|
||||
import { eq, sql, and } from "drizzle-orm";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import { config } from "@shared/lib/config";
|
||||
import { systemEvents, EVENTS } from "@shared/lib/events";
|
||||
import type { Transaction } from "@shared/lib/types";
|
||||
import { TimerKey, TimerType } from "@shared/lib/constants";
|
||||
import { UserError } from "@shared/lib/errors";
|
||||
@@ -70,8 +71,7 @@ export const levelingService = {
|
||||
.returning();
|
||||
|
||||
// Trigger Quest Event
|
||||
const { questService } = await import("@shared/modules/quest/quest.service");
|
||||
await questService.handleEvent(id, 'XP_GAIN', Number(amount), txFn);
|
||||
await systemEvents.emitAsync(EVENTS.DOMAIN.XP_GAINED, { userId: id, amount: Number(amount), tx: txFn });
|
||||
|
||||
return { user: updatedUser, levelUp, currentLevel: newLevel };
|
||||
}, tx);
|
||||
|
||||
Reference in New Issue
Block a user