From b6a92a3d4a857942b1a1dfaa744c9c3c90b92d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 14 Jun 2024 18:53:33 +0300 Subject: [PATCH] new extension methods for kotlin. (#16) --- .../net/infumia/pubsub/BrokerCoroutines.kt | 8 ++++++++ .../net/infumia/pubsub/BrokerCoroutinesImpl.kt | 4 ++++ .../net/infumia/pubsub/BrokerExtension.kt | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt index 26ac0bc..743a5a3 100644 --- a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt +++ b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt @@ -28,6 +28,14 @@ interface BrokerCoroutines : AutoCloseable { */ suspend fun send(message: Any, vararg targets: Target) + /** + * Sends a message to the specified targets. + * + * @param message the message to send. + * @param targets the targets to send the message to. + */ + suspend fun send(message: Any, vararg targets: Pair) + /** * Registers a handler to listen for messages of a specific type. * diff --git a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt index 07a2a94..7e5cb72 100644 --- a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt +++ b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt @@ -23,6 +23,10 @@ internal class BrokerCoroutinesImpl( this.delegate.send(message, *targets) } + override suspend fun send(message: Any, vararg targets: Pair) { + this.delegate.send(message, targets.map { Target.of(it.first, it.second) }) + } + override suspend fun listen(handler: HandlerCoroutines): AutoCloseable = this.delegate.listen(handler.type.java) { scope.launch { handler(it) } } diff --git a/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt b/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt index 4a45f57..b6b6fd4 100644 --- a/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt +++ b/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt @@ -2,6 +2,17 @@ package net.infumia.pubsub import java.util.concurrent.CompletableFuture + +/** + * Sends a message to the specified targets. + * + * @param message the message to send. + * @param targets the targets to send the message to. + */ +fun Broker.send(message: Any, vararg targets: Pair) { + this.send(message, targets.map { Target.of(it.first, it.second) }) +} + /** * Registers a handler to listen for messages of a specific type. * @@ -10,7 +21,7 @@ import java.util.concurrent.CompletableFuture * @return an [AutoCloseable] that can be used to unregister the handler. */ inline fun Broker.listen(noinline handler: (T) -> Unit): AutoCloseable = - listen(T::class.java, handler) + this.listen(T::class.java, handler) /** * Sends a message and expects a response of a specific type. @@ -21,7 +32,7 @@ inline fun Broker.listen(noinline handler: (T) -> Unit): AutoC * @return a [CompletableFuture] representing the response to the message. */ inline fun Broker.request(message: Any, vararg targets: Target): CompletableFuture = - request(message, R::class.java, *targets) + this.request(message, R::class.java, *targets) /** * Registers a function to respond to messages of a specific type. @@ -32,4 +43,4 @@ inline fun Broker.request(message: Any, vararg targets: Target * @return an [AutoCloseable] that can be used to unregister the responder. */ inline fun Broker.respond(noinline handler: (T) -> R?): AutoCloseable = - respond(T::class.java, handler) + this.respond(T::class.java, handler)