Use new server.kt listener methods

This commit is contained in:
2026-10-05 12:47:17 +01:00
parent 0bf6b0dae0
commit 4602af4e44
@@ -1,5 +1,6 @@
package com.shr4pnel.ferretirc
import com.shr4pnel.ferretirc.irc.IRCChannel
import com.shr4pnel.ferretirc.irc.Server
import com.shr4pnel.ferretirc.irc.User
import com.shr4pnel.ferretirc.net.Connection
@@ -17,13 +18,13 @@ import org.slf4j.helpers.NOPLogger
class IrcClient(
hostname: String,
port: Int,
var enableLogging: Boolean = false,
val enableLogging: Boolean = false,
) {
init {
KotlinLoggingConfiguration.logStartupMessage = false
}
private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
// private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
lateinit var server: Server
@@ -43,14 +44,15 @@ class IrcClient(
}
/**
* Send a message to the IRC server writer
* This is asynchronous and ordered
* Send a message to the IRC server message writer
* @param message The client message to send to the server
*/
suspend fun queueMessage(message: ClientMessage) {
connection.writer.outgoingMessages.send(message)
}
suspend fun join(channel: IRCChannel) = queueMessage(ClientMessage.Join(channel))
/**
* Send a message to the IRC server writer
* This is asynchronous and ordered
@@ -85,7 +87,7 @@ class IrcClient(
* @param predicate A function to filter a message based on its parameters
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
*/
suspend inline fun <reified T : ServerMessage> queueAndWaitForNext(
suspend inline fun <reified T : ServerMessage> queueMessageAndWaitForNext(
message: ClientMessage,
noinline predicate: (T) -> Boolean = { true },
): T = server.waitForNext(T::class, predicate) { queueMessage(message) }
@@ -96,10 +98,10 @@ class IrcClient(
* @param action A function to execute after message buffer flow collection has begun
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
*/
suspend inline fun <reified T : ServerMessage> waitForNextAfterAction(
suspend inline fun <reified T : ServerMessage> waitForNextMessageAfterAction(
noinline predicate: (T) -> Boolean = { true },
noinline action: suspend () -> Unit,
): T = server.waitForNext(T::class, predicate, action)
): T = server.waitForNext(T::class, predicate) { action() }
suspend fun register(
nick: String,
@@ -119,6 +121,6 @@ class IrcClient(
}
add(ClientMessage.Cap.END())
}.toTypedArray()
queueMessages(*messages)
waitForNextMessageAfterAction<ServerMessage.Numeric.RPL_WELCOME> { queueMessages(*messages) }
}
}