Skip to content

Commit 9b96194

Browse files
committed
refactor(audio_service): extract stale transition guard into helper method
1 parent d270b94 commit 9b96194

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

lib/services/audio_service.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,11 @@ class MusifyAudioHandler extends BaseAudioHandler {
12991299
return false;
13001300
}
13011301

1302+
/// Check if the given transitionId is stale (outdated by a newer request).
1303+
bool _isStaleTransition(int? transitionId) {
1304+
return transitionId != null && transitionId != _currentLoadingTransitionId;
1305+
}
1306+
13021307
Future<bool> playSong(Map song, {String? mediaId, int? transitionId}) async {
13031308
try {
13041309
final songData = cloneMap(song);
@@ -1316,7 +1321,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
13161321
// Abort if a newer song was requested while we were fetching the stream URL.
13171322
// This is the primary guard against the race condition where a slow streaming
13181323
// load overrides a song the user already switched to.
1319-
if (transitionId != null && transitionId != _currentLoadingTransitionId) {
1324+
if (_isStaleTransition(transitionId)) {
13201325
logger.log(
13211326
'Song load superseded by newer request, aborting: ${songData['ytid']}',
13221327
);
@@ -1341,7 +1346,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
13411346
);
13421347

13431348
// Check again after building the audio source (SponsorBlock fetch can also be slow).
1344-
if (transitionId != null && transitionId != _currentLoadingTransitionId) {
1349+
if (_isStaleTransition(transitionId)) {
13451350
logger.log(
13461351
'Song load superseded after building audio source, aborting: ${songData['ytid']}',
13471352
);
@@ -1464,7 +1469,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
14641469
try {
14651470
// Final staleness check before we touch the audio player.
14661471
// If another song was requested between the URL fetch and here, abort.
1467-
if (transitionId != null && transitionId != _currentLoadingTransitionId) {
1472+
if (_isStaleTransition(transitionId)) {
14681473
return false;
14691474
}
14701475

@@ -1477,7 +1482,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
14771482
// Check once more after the async setAudioSource: a fast offline song
14781483
// could have loaded and started playing while we were buffering/setting up.
14791484
// If so, stop the source we just loaded and yield to the newer song.
1480-
if (transitionId != null && transitionId != _currentLoadingTransitionId) {
1485+
if (_isStaleTransition(transitionId)) {
14811486
unawaited(audioPlayer.stop());
14821487
return false;
14831488
}

0 commit comments

Comments
 (0)