Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash when blocked due to too many requests in playing queue #5058

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/src/main/java/com/github/libretube/extensions/RunCatchingIO.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
67 changes: 29 additions & 38 deletions app/src/main/java/com/github/libretube/util/PlayingQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move
import com.github.libretube.extensions.runCatchingIO
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper
import kotlinx.coroutines.CoroutineScope
Expand All @@ -15,7 +16,6 @@
object PlayingQueue {
private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null
private val scope = CoroutineScope(Dispatchers.IO)

/**
* Listener that gets called when the user selects an item from the queue
Expand Down Expand Up @@ -145,65 +145,56 @@
}
}

private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) {
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?, isMainList: Boolean) = runCatchingIO {

Check failure on line 148 in app/src/main/java/com/github/libretube/util/PlayingQueue.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Exceeded max line length (100) Raw Output: app/src/main/java/com/github/libretube/util/PlayingQueue.kt:148:1: error: Exceeded max line length (100) (standard:max-line-length)

Check failure on line 148 in app/src/main/java/com/github/libretube/util/PlayingQueue.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before expression body Raw Output: app/src/main/java/com/github/libretube/util/PlayingQueue.kt:148:101: error: Newline expected before expression body (standard:function-signature)
var playlistNextPage = nextPage
scope.launch(Dispatchers.IO) {
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage).run {
addToQueueAsync(relatedStreams, isMainList = isMainList)
playlistNextPage = this.nextpage
}
}
}

fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) {
scope.launch(Dispatchers.IO) {
runCatching {
val playlist = PlaylistsHelper.getPlaylist(playlistId)
val isMainList = newCurrentStream != null
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
if (playlist.nextpage == null) return@launch
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
}
}
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem?) = runCatchingIO {
val playlist = PlaylistsHelper.getPlaylist(playlistId)
val isMainList = newCurrentStream != null
addToQueueAsync(playlist.relatedStreams, newCurrentStream, isMainList)
if (playlist.nextpage == null) return@runCatchingIO
fetchMoreFromPlaylist(playlistId, playlist.nextpage, isMainList)
}

private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) = runCatchingIO {
var channelNextPage = nextPage
scope.launch(Dispatchers.IO) {
runCatching {
while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
addToQueueAsync(relatedStreams)
channelNextPage = this.nextpage
}
}
while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).run {
addToQueueAsync(relatedStreams)
channelNextPage = this.nextpage
}
}
}

private fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
scope.launch(Dispatchers.IO) {
runCatching {
val channel = RetrofitInstance.api.getChannel(channelId)
addToQueueAsync(channel.relatedStreams, newCurrentStream)
if (channel.nextpage == null) return@launch
fetchMoreFromChannel(channelId, channel.nextpage)
}
}
private fun insertChannel(channelId: String, newCurrentStream: StreamItem) = runCatchingIO {
val channel = RetrofitInstance.api.getChannel(channelId)
addToQueueAsync(channel.relatedStreams, newCurrentStream)
if (channel.nextpage == null) return@runCatchingIO
fetchMoreFromChannel(channelId, channel.nextpage)
}

fun insertByVideoId(videoId: String) {
scope.launch {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
val streams = RetrofitInstance.api.getStreams(videoId.toID())
add(streams.toStreamItem(videoId))
}
}
}

fun updateQueue(streamItem: StreamItem, playlistId: String?, channelId: String?, relatedStreams: List<StreamItem> = emptyList()) {
fun updateQueue(
streamItem: StreamItem,
playlistId: String?,
channelId: String?,
relatedStreams: List<StreamItem> = emptyList()
) {
if (playlistId != null) {
insertPlaylist(playlistId, streamItem)
} else if (channelId != null) {
Expand Down
Loading