refactor(mobile): split playback provider and replace polling with player events #95

Merged
joshuafhiggins merged 4 commits from t3/split-mobile-playback into main 2026-11-16 21:46:56 -05:00
Owner

contexts/playback.tsx was ~1,380 lines that mixed native playlist sync, progress persistence, the queue API and three contexts. It also relied on timers and polling instead of player events: a 150 ms setTimeout to suppress track-change events, a 250 ms poll for Siri donation, and a 100 ms poll in waitForPlaybackReady.

Changes

  • hooks/use-native-playlist-sync.ts keeps the native AudioPlaylist in sync with the queue and reports its status back.

  • hooks/use-progress-persistence.ts restores the saved position and saves it while playing, on pause, and when the app goes to the background.

  • contexts/playback.tsx is now a thin provider (~740 lines) holding the store wiring, the queue API and the contexts.

  • Track-change suppression: each sync records where it left the native player (index, current song, history length). A trackChanged event is ignored when the player's live index still matches that record, or while a queue change is still being applied. This replaces the 150 ms timer. Native currentIndex reads and edits are synchronous on both platforms, so the record matches the player.

  • Readiness: waitForSong keeps a map of pending promises keyed by song ID. playlistStatusUpdate, trackChanged and each sync settle it, and a store subscription rejects it when the queue moves on. Siri donation and waitForPlaybackReady both use it. The 20 s timeout and the error messages are unchanged.

  • The play* / play*Shuffled pairs for album, artist and playlist are built from one table.

  • Fix (also broken on main): a restored queue showed 0:00 until Play, and playback started before the song loaded (Play right after launch, a Siri resume) began at 0:00 and then jumped. The restore seek is now queued as soon as the song becomes current, the saved position shows right away, and play() waits until a pending seek lands. This also covers rebuilding the player after a stream-quality change.

Apart from that fix, behavior is unchanged.

Testing

  • pnpm typecheck passes.
  • iOS (iPhone Duo simulator): restoring the queue on cold start (paused at the saved position, Play continues from there; with temporary logging, the native player's first playing status was at the saved position, never 0:00, including when playback was requested before the song loaded), play/pause, Previous (restart, then back one track), Next, seek, the next track starting when one ends, play album, play from a song in an album, shuffle while playing, and jumping to a song in the queue.
  • One stall seen once and not reproduced: a track stopped 4 s before its end while the UI showed it playing. After a relaunch it resumed and moved on to the next track normally.
  • Not tested: Siri intents and lock-screen controls (can't trigger them on the simulator), and Android (the emulator was in use).

Changes by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, running in T3 Code.

🤖 Generated with Claude Code

`contexts/playback.tsx` was ~1,380 lines that mixed native playlist sync, progress persistence, the queue API and three contexts. It also relied on timers and polling instead of player events: a 150 ms `setTimeout` to suppress track-change events, a 250 ms poll for Siri donation, and a 100 ms poll in `waitForPlaybackReady`. ## Changes - `hooks/use-native-playlist-sync.ts` keeps the native `AudioPlaylist` in sync with the queue and reports its status back. - `hooks/use-progress-persistence.ts` restores the saved position and saves it while playing, on pause, and when the app goes to the background. - `contexts/playback.tsx` is now a thin provider (~740 lines) holding the store wiring, the queue API and the contexts. - Track-change suppression: each sync records where it left the native player (index, current song, history length). A `trackChanged` event is ignored when the player's live index still matches that record, or while a queue change is still being applied. This replaces the 150 ms timer. Native `currentIndex` reads and edits are synchronous on both platforms, so the record matches the player. - Readiness: `waitForSong` keeps a map of pending promises keyed by song ID. `playlistStatusUpdate`, `trackChanged` and each sync settle it, and a store subscription rejects it when the queue moves on. Siri donation and `waitForPlaybackReady` both use it. The 20 s timeout and the error messages are unchanged. - The `play*` / `play*Shuffled` pairs for album, artist and playlist are built from one table. - Fix (also broken on `main`): a restored queue showed 0:00 until Play, and playback started before the song loaded (Play right after launch, a Siri resume) began at 0:00 and then jumped. The restore seek is now queued as soon as the song becomes current, the saved position shows right away, and `play()` waits until a pending seek lands. This also covers rebuilding the player after a stream-quality change. Apart from that fix, behavior is unchanged. ## Testing - `pnpm typecheck` passes. - iOS (iPhone Duo simulator): restoring the queue on cold start (paused at the saved position, Play continues from there; with temporary logging, the native player's first playing status was at the saved position, never 0:00, including when playback was requested before the song loaded), play/pause, Previous (restart, then back one track), Next, seek, the next track starting when one ends, play album, play from a song in an album, shuffle while playing, and jumping to a song in the queue. - One stall seen once and not reproduced: a track stopped 4 s before its end while the UI showed it playing. After a relaunch it resumed and moved on to the next track normally. - Not tested: Siri intents and lock-screen controls (can't trigger them on the simulator), and Android (the emulator was in use). --- Changes by Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, running in T3 Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
joshuafhiggins added 1 commit 2026-11-16 13:54:19 -05:00
Move native playlist sync and progress persistence into their own hooks,
wait on playlist events instead of timers and polling, and build the
album/artist/playlist play actions from one table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshuafhiggins added 1 commit 2026-11-16 20:19:01 -05:00
A paused player sends no status updates after it loads, so a restored
position waited until Play to apply and the slider showed 0:00. Seek
right away when the song is already loaded, and show the position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshuafhiggins added 1 commit 2026-11-16 20:35:20 -05:00
The restore seek was queued only after the player loaded, so playback
requested before then (Play right after launch, a Siri resume) started
at 0:00 and jumped. Queue the seek when the song becomes current, and
hold off starting the player until a pending seek lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshuafhiggins added 1 commit 2026-11-16 21:46:50 -05:00
# Conflicts:
#	apps/mobile/contexts/playback.tsx
joshuafhiggins merged commit 4f09f3eb5f into main 2026-11-16 21:46:56 -05:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: joshuafhiggins/otunes#95