Merge pull request #5058 from Bnyro/master

fix: crash when blocked due to too many requests in playing queue
This commit is contained in:
Bnyro 2023-10-29 15:39:21 +01:00 committed by GitHub
commit 299bb7cd9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 37 deletions

View File

@ -0,0 +1,13 @@
package com.github.libretube.extensions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
fun runCatchingIO(block: suspend () -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
block.invoke()
}
}
}

View File

@ -6,6 +6,7 @@ import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move import com.github.libretube.extensions.move
import com.github.libretube.extensions.runCatchingIO
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -15,7 +16,6 @@ import kotlinx.coroutines.launch
object PlayingQueue { object PlayingQueue {
private val queue = mutableListOf<StreamItem>() private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null private var currentStream: StreamItem? = null
private val scope = CoroutineScope(Dispatchers.IO)
/** /**
* Listener that gets called when the user selects an item from the queue * Listener that gets called when the user selects an item from the queue
@ -145,57 +145,43 @@ object PlayingQueue {
} }
} }
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) { private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) = runCatchingIO {
var playlistNextPage = nextPage var playlistNextPage = nextPage
scope.launch(Dispatchers.IO) { while (playlistNextPage != null) {
while (playlistNextPage != null) { RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!).run { addToQueueAsync(relatedStreams, isMainList = isMainList)
addToQueueAsync(relatedStreams, isMainList = isMainList) playlistNextPage = this.nextpage
playlistNextPage = this.nextpage
}
} }
} }
} }
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) { fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) = runCatchingIO {
scope.launch(Dispatchers.IO) { val playlist = PlaylistsHelper.getPlaylist(playlistId)
runCatching { val isMainList = newCurrentStream != null
val playlist = PlaylistsHelper.getPlaylist(playlistId) addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
val isMainList = newCurrentStream != null if (playlist.nextpage == null) return@runCatchingIO
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList) fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
if (playlist.nextpage == null) return@launch
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
}
}
} }
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) { private fun fetchMoreFromChannel(channelId: String, nextPage: String?) = runCatchingIO {
var channelNextPage = nextPage var channelNextPage = nextPage
scope.launch(Dispatchers.IO) { while (channelNextPage != null) {
runCatching { RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
while (channelNextPage != null) { addToQueueAsync(relatedStreams)
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run { channelNextPage = this.nextpage
addToQueueAsync(relatedStreams)
channelNextPage = this.nextpage
}
}
} }
} }
} }
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) { private fun insertChannel(channelId: String, newCurrentStream: StreamItem) = runCatchingIO {
scope.launch(Dispatchers.IO) { val channel = RetrofitInstance.api.getChannel(channelId)
runCatching { addToQueueAsync(channel.relatedStreams, newCurrentStream)
val channel = RetrofitInstance.api.getChannel(channelId) if (channel.nextpage == null) return@runCatchingIO
addToQueueAsync(channel.relatedStreams, newCurrentStream) fetchMoreFromChannel(channelId, channel.nextpage)
if (channel.nextpage == null) return@launch
fetchMoreFromChannel(channelId, channel.nextpage)
}
}
} }
fun insertByVideoId(videoId: String) { fun insertByVideoId(videoId: String) {
scope.launch { CoroutineScope(Dispatchers.IO).launch {
runCatching { runCatching {
val streams = RetrofitInstance.api.getStreams(videoId.toID()) val streams = RetrofitInstance.api.getStreams(videoId.toID())
add(streams.toStreamItem(videoId)) add(streams.toStreamItem(videoId))