Refactors and optimizes core calendar managers

Streamlines several core managers by removing unnecessary complexity, caching, and redundant methods

Key improvements:
- Simplified event and view management logic
- Removed unnecessary caching mechanisms
- Reduced method complexity in managers
- Improved code readability and performance
This commit is contained in:
Janus C. H. Knudsen 2025-11-01 21:07:07 +01:00
parent 1ae4f00f2b
commit b6ab1ff50e
6 changed files with 193 additions and 327 deletions

View file

@ -15,15 +15,13 @@ interface RawEventData {
}
/**
* EventManager - Optimized event lifecycle and CRUD operations
* Handles data loading with improved performance and caching
* EventManager - Event lifecycle and CRUD operations
* Handles data loading and event management
*/
export class EventManager {
private events: CalendarEvent[] = [];
private rawData: RawEventData[] | null = null;
private eventCache = new Map<string, CalendarEvent[]>(); // Cache for period queries
private lastCacheKey: string = '';
private dateService: DateService;
private config: CalendarConfig;
@ -37,12 +35,11 @@ export class EventManager {
}
/**
* Optimized data loading with better error handling
* Load event data from JSON file
*/
public async loadData(): Promise<void> {
try {
await this.loadMockData();
this.clearCache(); // Clear cache when new data is loaded
} catch (error) {
console.error('Failed to load event data:', error);
this.events = [];
@ -69,7 +66,7 @@ export class EventManager {
}
/**
* Optimized data processing with better type safety
* Process raw event data and convert to CalendarEvent objects
*/
private processCalendarData(data: RawEventData[]): CalendarEvent[] {
return data.map((event): CalendarEvent => ({
@ -82,14 +79,6 @@ export class EventManager {
}));
}
/**
* Clear event cache when data changes
*/
private clearCache(): void {
this.eventCache.clear();
this.lastCacheKey = '';
}
/**
* Get events with optional copying for performance
*/
@ -162,42 +151,26 @@ export class EventManager {
}
/**
* Optimized events for period with caching and DateService
* Get events that overlap with a given time period
*/
public getEventsForPeriod(startDate: Date, endDate: Date): CalendarEvent[] {
// Create cache key using DateService for consistent formatting
const cacheKey = `${this.dateService.formatISODate(startDate)}_${this.dateService.formatISODate(endDate)}`;
// Return cached result if available
if (this.lastCacheKey === cacheKey && this.eventCache.has(cacheKey)) {
return this.eventCache.get(cacheKey)!;
}
// Filter events using optimized date operations
const filteredEvents = this.events.filter(event => {
// Event overlaps period if it starts before period ends AND ends after period starts
// Event overlaps period if it starts before period ends AND ends after period starts
return this.events.filter(event => {
return event.start <= endDate && event.end >= startDate;
});
// Cache the result
this.eventCache.set(cacheKey, filteredEvents);
this.lastCacheKey = cacheKey;
return filteredEvents;
}
/**
* Optimized event creation with better ID generation
* Create a new event and add it to the calendar
*/
public addEvent(event: Omit<CalendarEvent, 'id'>): CalendarEvent {
const newEvent: CalendarEvent = {
...event,
id: `event_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
};
this.events.push(newEvent);
this.clearCache(); // Clear cache when data changes
this.eventBus.emit(CoreEvents.EVENT_CREATED, {
event: newEvent
});
@ -206,7 +179,7 @@ export class EventManager {
}
/**
* Optimized event update with validation
* Update an existing event
*/
public updateEvent(id: string, updates: Partial<CalendarEvent>): CalendarEvent | null {
const eventIndex = this.events.findIndex(event => event.id === id);
@ -214,39 +187,11 @@ export class EventManager {
const updatedEvent = { ...this.events[eventIndex], ...updates };
this.events[eventIndex] = updatedEvent;
this.clearCache(); // Clear cache when data changes
this.eventBus.emit(CoreEvents.EVENT_UPDATED, {
event: updatedEvent
});
return updatedEvent;
}
/**
* Optimized event deletion with better error handling
*/
public deleteEvent(id: string): boolean {
const eventIndex = this.events.findIndex(event => event.id === id);
if (eventIndex === -1) return false;
const deletedEvent = this.events[eventIndex];
this.events.splice(eventIndex, 1);
this.clearCache(); // Clear cache when data changes
this.eventBus.emit(CoreEvents.EVENT_DELETED, {
event: deletedEvent
});
return true;
}
/**
* Refresh data by reloading from source
*/
public async refresh(): Promise<void> {
await this.loadData();
}
}
}