@@ -22,7 +22,7 @@ import com.lasthopesoftware.bluewater.client.playback.nowplaying.storage.ManageN
2222import com.lasthopesoftware.bluewater.client.playback.nowplaying.storage.NowPlaying
2323import com.lasthopesoftware.bluewater.client.playback.playlist.ManagePlaylistPlayback
2424import com.lasthopesoftware.bluewater.shared.lazyLogger
25- import com.lasthopesoftware.policies.ratelimiting.PromisingRateLimiter
25+ import com.lasthopesoftware.bluewater.shared.tryUpdate
2626import com.lasthopesoftware.promises.ContinuingResult
2727import com.lasthopesoftware.promises.extensions.ProgressingPromise
2828import com.lasthopesoftware.promises.extensions.keepPromise
@@ -69,7 +69,7 @@ class PlaybackEngine(
6969
7070 private val activeLibraryId = AtomicReference (markerLibraryId)
7171
72- private val nowPlayingStateSync = PromisingRateLimiter <NowPlaying ?>(1 )
72+ private val promisedNowPlayingState = AtomicReference ( Promise .empty <NowPlaying ?>() )
7373
7474 private val promisedPlayback = AtomicReference <ProgressingPromise <PositionedPlayingFile , Unit >? > (null )
7575
@@ -480,13 +480,9 @@ class PlaybackEngine(
480480 return playlistPlayback.resume()
481481 }
482482
483- private fun serializedPlayerUpdate () = nowPlayingStateSync.limit {
484- playbackBootstrapper.updateFromState(activeLibraryId.get())
485- }
483+ private fun serializedPlayerUpdate () = updateStateSynchronously { playbackBootstrapper.updateFromState(activeLibraryId.get()) }
486484
487- private fun promiseActiveNowPlaying () = nowPlayingStateSync.limit {
488- nowPlayingRepository.promiseNowPlaying(activeLibraryId.get())
489- }
485+ private fun promiseActiveNowPlaying () = updateStateSynchronously { nowPlayingRepository.promiseNowPlaying(activeLibraryId.get()) }
490486
491487 private fun saveState (
492488 libraryId : LibraryId ,
@@ -508,7 +504,7 @@ class PlaybackEngine(
508504 private inline fun saveState (
509505 libraryId : LibraryId ,
510506 crossinline updateFunc : NowPlaying .() -> NowPlaying
511- ): Promise <NowPlaying ?> = nowPlayingStateSync.limit {
507+ ): Promise <NowPlaying ?> = updateStateSynchronously {
512508 nowPlayingRepository.promiseNowPlaying(libraryId).eventually {
513509 it?.let { np ->
514510 val updatedNowPlaying = updateFunc(np)
@@ -519,7 +515,24 @@ class PlaybackEngine(
519515 }
520516
521517 @Suppress(" UNCHECKED_CAST" )
522- private fun saveState (nowPlaying : NowPlaying ): Promise <NowPlaying ?> = nowPlayingStateSync.limit {
518+ private fun saveState (nowPlaying : NowPlaying ): Promise <NowPlaying ?> = updateStateSynchronously {
523519 nowPlayingRepository.updateNowPlaying(nowPlaying) as Promise <NowPlaying ?>
524520 }
521+
522+ /* *
523+ * Updates the `promisedNowPlayingState` with the result of the provided `updateFunc`.
524+ *
525+ * This function ensures that the `promisedNowPlayingState` is updated synchronously.
526+ * Each new update is chained to the previous promise, guaranteeing that state
527+ * updates are applied in the order they are initiated.
528+ *
529+ * @param updateFunc A function that returns a [Promise] for a nullable [NowPlaying] state.
530+ * This function is responsible for fetching or modifying the state.
531+ * @return A [Promise] that resolves with the current [NowPlaying] state after the update
532+ * function has been scheduled. To wait for the update to complete, chain to the returned promise.
533+ */
534+ private inline fun updateStateSynchronously (crossinline updateFunc : () -> Promise <NowPlaying ?>): Promise <NowPlaying ?> {
535+ promisedNowPlayingState.tryUpdate { prev -> prev.eventually { updateFunc() } }
536+ return promisedNowPlayingState.get()
537+ }
525538}
0 commit comments