Refactors calendar initialization with factory

Implements a factory pattern for manager creation and
initialization, improving dependency management and
extensibility.

This change replaces direct manager instantiation with a
`ManagerFactory` that handles dependency injection. This
enhances code organization and testability. It also includes
an initialization sequence diagram for better understanding
of the calendar's architecture and data flow.
This commit is contained in:
Janus Knudsen 2025-08-17 23:44:30 +02:00
parent 32ee35eb02
commit 26f0cb8aaa
11 changed files with 333 additions and 150 deletions

View file

@ -0,0 +1,88 @@
import { IEventBus } from '../types/CalendarTypes';
import { CalendarConfig } from '../core/CalendarConfig';
import { EventManager } from '../managers/EventManager';
import { EventRenderingService } from '../renderers/EventRendererManager';
import { GridManager } from '../managers/GridManager';
import { ScrollManager } from '../managers/ScrollManager';
import { NavigationManager } from '../managers/NavigationManager';
import { ViewManager } from '../managers/ViewManager';
import { CalendarManager } from '../managers/CalendarManager';
/**
* Factory for creating and managing calendar managers with proper dependency injection
*/
export class ManagerFactory {
private static instance: ManagerFactory;
private constructor() {}
public static getInstance(): ManagerFactory {
if (!ManagerFactory.instance) {
ManagerFactory.instance = new ManagerFactory();
}
return ManagerFactory.instance;
}
/**
* Create all managers with proper dependency injection
*/
public createManagers(eventBus: IEventBus, config: CalendarConfig): {
eventManager: EventManager;
eventRenderer: EventRenderingService;
gridManager: GridManager;
scrollManager: ScrollManager;
navigationManager: NavigationManager;
viewManager: ViewManager;
calendarManager: CalendarManager;
} {
console.log('🏭 ManagerFactory: Creating managers with proper DI...');
// Create managers in dependency order
const eventManager = new EventManager(eventBus);
const eventRenderer = new EventRenderingService(eventBus, eventManager);
const gridManager = new GridManager();
const scrollManager = new ScrollManager();
const navigationManager = new NavigationManager(eventBus);
const viewManager = new ViewManager(eventBus);
// CalendarManager depends on all other managers
const calendarManager = new CalendarManager(
eventBus,
config,
eventManager,
gridManager,
eventRenderer,
scrollManager
);
console.log('✅ ManagerFactory: All managers created successfully');
return {
eventManager,
eventRenderer,
gridManager,
scrollManager,
navigationManager,
viewManager,
calendarManager
};
}
/**
* Initialize all managers in the correct order
*/
public async initializeManagers(managers: {
calendarManager: CalendarManager;
[key: string]: any;
}): Promise<void> {
console.log('🚀 ManagerFactory: Initializing managers...');
try {
await managers.calendarManager.initialize();
console.log('✅ ManagerFactory: All managers initialized successfully');
} catch (error) {
console.error('❌ ManagerFactory: Manager initialization failed:', error);
throw error;
}
}
}