playback-state-duplication.md
1 # Playback State Duplication Issue 2 3 ## Problem 4 5 The current codebase has a design issue where video playback state is managed in multiple places: 6 7 1. The VideoController maintains a `playbackState` object 8 2. The UIManager needs to know about playback state to update the UI 9 3. The InteractionHandler needs to know about playback state to handle user interactions 10 4. The VideoSwitcher needs to know about playback state to handle video switching 11 12 This duplication leads to synchronization issues and makes the code harder to maintain. 13 14 ## Impact 15 16 - Playback state may become inconsistent across components 17 - UI may not accurately reflect the actual playback state 18 - User interactions may be handled incorrectly based on playback state 19 - Difficult to debug due to state being spread across multiple components 20 21 ## Current Implementation 22 23 Currently, we're using events to synchronize playback state across components: 24 25 1. VideoController publishes events like PLAYBACK_STARTED, PLAYBACK_PAUSED, PLAYBACK_ENDED 26 2. Other components subscribe to these events to update their internal state 27 3. Components query their local state to make decisions 28 29 This approach works but is fragile and error-prone. 30 31 ## Recommended Solution 32 33 For future refactoring: 34 35 1. **Single Source of Truth**: Keep playback state only in the VideoController 36 2. **Dependency Injection**: Inject the VideoController into components that need it 37 3. **Observer Pattern**: Components observe the VideoController for state changes 38 4. **No Local Copies**: Components should not maintain their own copies of the state 39 40 ## Implementation Notes 41 42 - The VideoController already has a `playbackState` object that could be exposed through getter methods 43 - Components should call methods like `isPlaying()`, `getCurrentTime()`, etc. directly on the VideoController 44 - Events should still be emitted for UI updates, but state should be queried from the VideoController 45 - This would simplify the code and reduce the risk of state inconsistencies