Add clear error message when timing out expecting a message
This commit is contained in:
@@ -6,6 +6,7 @@ import com.shr4pnel.ferretirc.net.messages.ServerMessage.Numeric
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.github.oshai.kotlinlogging.slf4j.logger
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
@@ -114,20 +115,20 @@ class Server(
|
||||
* @param T The type of ServerMessage to fetch
|
||||
* @return A set of messages of type T in the buffer that arrive before untilMessage appears
|
||||
*/
|
||||
suspend inline fun <reified T : ServerMessage, reified U : ServerMessage> fetchMessagesOfType(
|
||||
timeoutMillis: Long = 5000,
|
||||
noinline subscriptionMessage: suspend () -> Unit = {},
|
||||
): List<T> {
|
||||
val buffer = mutableListOf<T>()
|
||||
withTimeoutOrNull(timeoutMillis) {
|
||||
msgBuffer
|
||||
.onSubscription { subscriptionMessage() }
|
||||
.takeWhile { it !is U }
|
||||
.filterIsInstance<T>()
|
||||
.collect { buffer.add(it) }
|
||||
} ?: logger.error { "Timed out while waiting to receive ${U::class}" }
|
||||
return buffer.toList()
|
||||
}
|
||||
// suspend inline fun <reified T : ServerMessage, reified U : ServerMessage> fetchMessagesOfType(
|
||||
// timeoutMillis: Long = 5000,
|
||||
// noinline subscriptionMessage: suspend () -> Unit = {},
|
||||
// ): List<T> {
|
||||
// val buffer = mutableListOf<T>()
|
||||
// withTimeoutOrNull(timeoutMillis) {
|
||||
// msgBuffer
|
||||
// .onSubscription { subscriptionMessage() }
|
||||
// .takeWhile { it !is U }
|
||||
// .filterIsInstance<T>()
|
||||
// .collect { buffer.add(it) }
|
||||
// } ?: logger.error { "Timed out while waiting to receive ${U::class}" }
|
||||
// return buffer.toList()
|
||||
// }
|
||||
|
||||
fun fetchFeatures() {
|
||||
}
|
||||
@@ -137,12 +138,18 @@ class Server(
|
||||
predicate: (T) -> Boolean = { true },
|
||||
timeoutMillis: Long = 5000,
|
||||
onSubscribedLambda: suspend () -> Unit = {},
|
||||
): T =
|
||||
withTimeout(timeoutMillis) {
|
||||
msgBuffer
|
||||
.onSubscription { onSubscribedLambda() }
|
||||
.filter { kClass.isInstance(it) }
|
||||
.map { kClass.cast(it) }
|
||||
.first(predicate)
|
||||
): T {
|
||||
try {
|
||||
return withTimeout(timeoutMillis) {
|
||||
msgBuffer
|
||||
.onSubscription { onSubscribedLambda() }
|
||||
.filter { kClass.isInstance(it) }
|
||||
.map { kClass.cast(it) }
|
||||
.first(predicate)
|
||||
}
|
||||
} catch (e: TimeoutCancellationException) {
|
||||
logger.error {"Timed out while waiting to receive ${kClass.simpleName} message"}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user