You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor subtitle initialization to pure event-driven approach
Removed all setTimeout calls with arbitrary delays (300ms, 600ms, 1000ms)
in favor of a reliable event-driven implementation.
## Problems with Previous Approach
1. **Magic Numbers**: Used arbitrary timeouts that might be too short/long
2. **Unreliable**: Depended on guessing when VTT files would load
3. **Complex**: Multiple retry attempts with nested setTimeout calls
4. **Wasteful**: Called activation logic multiple times unnecessarily
## New Pure Event-Driven Approach
### How It Works
```typescript
// Check current state
if (trackElement.readyState === 2 && track.cues?.length > 0) {
// Already loaded - activate immediately
track.mode = 'showing';
} else {
// Not loaded yet - wait for browser events
trackElement.addEventListener('load', activateTrack);
track.addEventListener('cuechange', activateTrack);
}
```
### Key Benefits
1. **No Magic Numbers**: Zero setTimeout calls
2. **Reliable**: Activates exactly when VTT is ready (event-driven)
3. **Simple**: Single activation point, clean logic flow
4. **Efficient**: Only processes once (guard at function start)
### How It Handles Different Scenarios
**Fast Connection (VTT loads immediately):**
- readyState === 2 on first check
- Activates immediately, no waiting
**Slow Connection (VTT loads later):**
- readyState !== 2 on first check
- Event listeners fire when ready
- Activates when browser completes loading
**Already Playing (late initialization):**
- Cues already available from browser
- Activates immediately
### Changes Made
- Removed all setTimeout/retry logic
- Simplified to single activation function
- Added readyState check for immediate activation
- Used load + cuechange events for delayed activation
- Removed unnecessary handlePlayWithSubtitles wrapper
- Added guard to prevent duplicate processing
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
0 commit comments