Implements dependency injection with Brandi

Introduces Brandi DI container for managing application dependencies.

This change replaces the existing factory pattern with a proper
dependency injection mechanism, improving code modularity,
testability, and maintainability. It configures all managers and core
services within the container, ensuring proper dependency resolution.
This commit is contained in:
Janus C. H. Knudsen 2025-10-14 22:53:28 +02:00
parent 020d5c3efc
commit f7c67e6253
5 changed files with 170 additions and 24 deletions

View file

@ -2,24 +2,25 @@
import { eventBus } from './core/EventBus';
import { calendarConfig } from './core/CalendarConfig';
import { CalendarTypeFactory } from './factories/CalendarTypeFactory';
import { ManagerFactory } from './factories/ManagerFactory';
import { createContainer } from './di/container';
import { TOKENS } from './di/tokens';
import { URLManager } from './utils/URLManager';
import { CalendarManagers } from './types/ManagerTypes';
import { EventManager } from './managers/EventManager';
/**
* Handle deep linking functionality after managers are initialized
*/
async function handleDeepLinking(managers: CalendarManagers): Promise<void> {
async function handleDeepLinking(eventManager: EventManager): Promise<void> {
try {
const urlManager = new URLManager(eventBus);
const eventId = urlManager.parseEventIdFromURL();
if (eventId) {
console.log(`Deep linking to event ID: ${eventId}`);
// Wait a bit for managers to be fully ready
setTimeout(() => {
const success = managers.eventManager.navigateToEvent(eventId);
const success = eventManager.navigateToEvent(eventId);
if (!success) {
console.warn(`Deep linking failed: Event with ID ${eventId} not found`);
}
@ -31,40 +32,49 @@ async function handleDeepLinking(managers: CalendarManagers): Promise<void> {
}
/**
* Initialize the calendar application with simple direct calls
* Initialize the calendar application using Brandi DI
*/
async function initializeCalendar(): Promise<void> {
try {
// Use the singleton calendar configuration
const config = calendarConfig;
// Initialize the CalendarTypeFactory before creating managers
CalendarTypeFactory.initialize();
// Create managers using factory pattern
const managerFactory = ManagerFactory.getInstance();
const managers = managerFactory.createManagers(eventBus);
// Create Brandi DI container (all managers instantiated here)
const container = createContainer();
// Enable debug mode for development
eventBus.setDebug(true);
// Initialize all managers
await managerFactory.initializeManagers(managers);
// Get managers from container
const calendarManager = container.get(TOKENS.calendarManager);
const eventManager = container.get(TOKENS.eventManager);
const resizeHandleManager = container.get(TOKENS.resizeHandleManager);
// Initialize managers
await calendarManager.initialize?.();
await resizeHandleManager.initialize?.();
// Handle deep linking after managers are initialized
await handleDeepLinking(managers);
await handleDeepLinking(eventManager);
// Expose to window for debugging (with proper typing)
(window as Window & {
(window as Window & {
calendarDebug?: {
eventBus: typeof eventBus;
} & CalendarManagers;
container: typeof container;
calendarManager: typeof calendarManager;
eventManager: typeof eventManager;
};
}).calendarDebug = {
eventBus,
...managers
container,
calendarManager,
eventManager,
};
} catch (error) {
throw error;
}