mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
22 lines
553 B
Kotlin
22 lines
553 B
Kotlin
package com.github.libretube.extensions
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.withContext
|
|
|
|
suspend fun <T> runSafely(
|
|
onSuccess: (List<T>) -> Unit = { },
|
|
ioBlock: suspend () -> List<T>
|
|
) {
|
|
withContext(Dispatchers.IO) {
|
|
val result = runCatching { ioBlock.invoke() }
|
|
.getOrNull()
|
|
?.takeIf { it.isNotEmpty() } ?: return@withContext
|
|
|
|
withContext(Dispatchers.Main) {
|
|
if (result.isNotEmpty()) {
|
|
onSuccess.invoke(result)
|
|
}
|
|
}
|
|
}
|
|
}
|