From 521dc00757f597d08d04a9fea41934e17ed987fe Mon Sep 17 00:00:00 2001 From: observer Date: Tue, 15 Sep 2026 03:08:34 +0100 Subject: [PATCH] Add waitForNextAfterAction, queueAndWaitForNext --- .../com/shr4pnel/ferretirc/IrcClient.kt | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/shr4pnel/ferretirc/IrcClient.kt b/src/main/kotlin/com/shr4pnel/ferretirc/IrcClient.kt index d1e7c6e..73cd32a 100644 --- a/src/main/kotlin/com/shr4pnel/ferretirc/IrcClient.kt +++ b/src/main/kotlin/com/shr4pnel/ferretirc/IrcClient.kt @@ -1,6 +1,7 @@ package com.shr4pnel.ferretirc import com.shr4pnel.ferretirc.irc.Server +import com.shr4pnel.ferretirc.irc.User import com.shr4pnel.ferretirc.net.Connection import com.shr4pnel.ferretirc.net.messages.ClientMessage import com.shr4pnel.ferretirc.net.messages.ServerMessage @@ -24,7 +25,12 @@ class IrcClient( private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER) val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + lateinit var server: Server + private set + + lateinit var currentUser: User + private set @PublishedApi internal val connection = Connection(hostname, port, scope, enableLogging) @@ -64,17 +70,43 @@ class IrcClient( } /** - * Return the next Server Message meeting condition + * Return the next Server Message meeting given condition "predicate" * @param predicate A function to filter a message based on its parameters + * @param T A ServerMessage inheritor + * @return An instance of ServerMessage + * @see com.shr4pnel.ferretirc.irc.Server.waitForNext + * @see com.shr4pnel.ferretirc.net.messages.ServerMessage */ suspend inline fun waitForNext(noinline predicate: (T) -> Boolean = { true }): T = - connection.reader.waitForNext(T::class, predicate) + server.waitForNext(T::class, predicate) + + /** + * Queue a client message, and wait for the next server message meeting condition (predicate) + * @param predicate A function to filter a message based on its parameters + * @see com.shr4pnel.ferretirc.IrcClient.waitForNext + */ + suspend inline fun queueAndWaitForNext( + message: ClientMessage, + noinline predicate: (T) -> Boolean = { true }, + ): T = server.waitForNext(T::class, predicate) { queueMessage(message) } + + /** + * Create a subscription to the server message buffer, after performing an action + * @param predicate A function to filter the message buffer flow + * @param action A function to execute after message buffer flow collection has begun + * @see com.shr4pnel.ferretirc.IrcClient.waitForNext + */ + suspend inline fun waitForNextAfterAction( + noinline predicate: (T) -> Boolean = { true }, + noinline action: suspend () -> Unit, + ): T = server.waitForNext(T::class, predicate, action) suspend fun register( nick: String, realName: String? = null, password: String? = null, ) { + currentUser = User(nick, "") // TODO TEMP USER ASSIGNMENT val messages = buildList { if (!password.isNullOrEmpty()) add(ClientMessage.Pass(password)) @@ -86,7 +118,7 @@ class IrcClient( add(ClientMessage.User(nick, nick)) } add(ClientMessage.Cap.END()) - } - queueMessages(*messages.toTypedArray()) + }.toTypedArray() + queueMessages(*messages) } }