diff --git a/src/main/kotlin/com/shr4pnel/ferretirc/irc/Server.kt b/src/main/kotlin/com/shr4pnel/ferretirc/irc/Server.kt index 7c0b271..2dea9a5 100644 --- a/src/main/kotlin/com/shr4pnel/ferretirc/irc/Server.kt +++ b/src/main/kotlin/com/shr4pnel/ferretirc/irc/Server.kt @@ -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 fetchMessagesOfType( - timeoutMillis: Long = 5000, - noinline subscriptionMessage: suspend () -> Unit = {}, - ): List { - val buffer = mutableListOf() - withTimeoutOrNull(timeoutMillis) { - msgBuffer - .onSubscription { subscriptionMessage() } - .takeWhile { it !is U } - .filterIsInstance() - .collect { buffer.add(it) } - } ?: logger.error { "Timed out while waiting to receive ${U::class}" } - return buffer.toList() - } +// suspend inline fun fetchMessagesOfType( +// timeoutMillis: Long = 5000, +// noinline subscriptionMessage: suspend () -> Unit = {}, +// ): List { +// val buffer = mutableListOf() +// withTimeoutOrNull(timeoutMillis) { +// msgBuffer +// .onSubscription { subscriptionMessage() } +// .takeWhile { it !is U } +// .filterIsInstance() +// .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 } + } }