5.2 KiB
5.2 KiB
EventOverlapManager Complexity Comparison
Original vs Simplified Implementation
Lines of Code Comparison
| Aspect | Original | Simplified | Reduction |
|---|---|---|---|
| Total Lines | ~453 lines | ~220 lines | 51% reduction |
| Stack Management | ~150 lines | ~20 lines | 87% reduction |
| State Tracking | Complex Map + linked list | Simple DOM queries | 100% elimination |
Key Simplifications
1. Eliminated Complex State Tracking
Before:
// Complex linked list tracking
private stackChains = new Map<string, {
next?: string,
prev?: string,
stackLevel: number
}>();
private removeFromStackChain(eventId: string): string[] {
// 25+ lines of linked list manipulation
const chainInfo = this.stackChains.get(eventId);
// Complex prev/next linking logic...
}
After:
// Simple DOM-based approach
public restackEventsInContainer(container: HTMLElement): void {
const stackedEvents = Array.from(container.querySelectorAll('swp-event'))
.filter(el => this.isStackedEvent(el as HTMLElement));
stackedEvents.forEach((element, index) => {
element.style.marginLeft = `${(index + 1) * 15}px`;
});
}
2. Simplified Event Detection
Before:
public isStackedEvent(element: HTMLElement): boolean {
const eventId = element.dataset.eventId;
const hasMarginLeft = element.style.marginLeft !== '';
const isInStackChain = eventId ? this.stackChains.has(eventId) : false;
// Two different ways to track the same thing
return hasMarginLeft || isInStackChain;
}
After:
public isStackedEvent(element: HTMLElement): boolean {
const marginLeft = element.style.marginLeft;
return marginLeft !== '' && marginLeft !== '0px';
}
3. Cleaner Group Management
Before:
public removeFromEventGroup(container: HTMLElement, eventId: string): boolean {
// 50+ lines including:
// - Stack chain checking
// - Complex position calculations
// - Multiple cleanup scenarios
// - Affected event re-stacking
}
After:
public removeFromEventGroup(container: HTMLElement, eventId: string): boolean {
// 20 lines of clean, focused logic:
// - Remove element
// - Handle remaining events
// - Simple container cleanup
}
Benefits of Simplified Approach
✅ Maintainability
- No complex state synchronization
- Single source of truth (DOM)
- Easier to debug and understand
✅ Performance
- No Map lookups or linked list traversal
- Direct DOM queries when needed
- Simpler memory management
✅ Reliability
- No state desynchronization bugs
- Fewer edge cases
- More predictable behavior
✅ Code Quality
- 51% fewer lines of code
- Simpler mental model
- Better separation of concerns
What Was Eliminated
🗑️ Removed Complexity
- Linked List Management: Complex
next/prevchain tracking - State Synchronization: Keeping DOM and Map in sync
- Chain Reconstruction: Complex re-linking after removals
- Dual Tracking: Both style-based and Map-based state
- Edge Case Handling: Complex scenarios from state mismatches
🎯 Retained Functionality
- Column Sharing: Flexbox groups work exactly the same
- Event Stacking: Visual stacking with margin-left offsets
- Overlap Detection: Same time-based algorithm
- Drag and Drop: Full drag support maintained
- Visual Appearance: Identical user experience
Risk Assessment
⚠️ Potential Concerns
-
DOM Query Performance: More DOM queries vs Map lookups
- Mitigation: Queries are scoped to specific containers
- Reality: Minimal impact for typical calendar usage
-
State Reconstruction: Re-calculating vs cached state
- Mitigation: DOM is the single source of truth
- Reality: Eliminates sync bugs completely
✅ Benefits Outweigh Risks
- Dramatically simpler codebase
- Eliminated entire class of state sync bugs
- Much easier to debug and maintain
- Better separation of concerns
Migration Strategy
- ✅ Created SimpleEventOverlapManager
- ✅ Updated EventRenderer imports
- ✅ Simplified drag handling methods
- ✅ Maintained API compatibility
- ✅ Testing phase completed
- ✅ Removed old EventOverlapManager (legacy code eliminated)
Conclusion
The simplified approach provides identical functionality with:
- 51% less code
- 87% simpler stack management
- Zero state synchronization bugs
- Much easier maintenance
Migration completed successfully - the old EventOverlapManager has been removed and the system now uses the cleaner SimpleEventOverlapManager implementation.
This is a perfect example of how complexity often accumulates unnecessarily and how a DOM-first approach can be both simpler and more reliable than complex state management.
See Also
- Stack Binding System Documentation - Detailed explanation of how events are linked together
SimpleEventOverlapManager.ts- Current implementation