Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new extension methods for kotlin. #16

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>)

/**
* Registers a handler to listen for messages of a specific type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ internal class BrokerCoroutinesImpl(
this.delegate.send(message, *targets)
}

override suspend fun send(message: Any, vararg targets: Pair<String, String>) {
this.delegate.send(message, targets.map { Target.of(it.first, it.second) })
}

override suspend fun <T : Any> listen(handler: HandlerCoroutines<T>): AutoCloseable =
this.delegate.listen(handler.type.java) { scope.launch { handler(it) } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>) {
this.send(message, targets.map { Target.of(it.first, it.second) })
}

/**
* Registers a handler to listen for messages of a specific type.
*
Expand All @@ -10,7 +21,7 @@ import java.util.concurrent.CompletableFuture
* @return an [AutoCloseable] that can be used to unregister the handler.
*/
inline fun <reified T : Any> 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.
Expand All @@ -21,7 +32,7 @@ inline fun <reified T : Any> Broker.listen(noinline handler: (T) -> Unit): AutoC
* @return a [CompletableFuture] representing the response to the message.
*/
inline fun <reified R : Any> Broker.request(message: Any, vararg targets: Target): CompletableFuture<R> =
request(message, R::class.java, *targets)
this.request(message, R::class.java, *targets)

/**
* Registers a function to respond to messages of a specific type.
Expand All @@ -32,4 +43,4 @@ inline fun <reified R : Any> Broker.request(message: Any, vararg targets: Target
* @return an [AutoCloseable] that can be used to unregister the responder.
*/
inline fun <reified T : Any, R : Any> Broker.respond(noinline handler: (T) -> R?): AutoCloseable =
respond(T::class.java, handler)
this.respond(T::class.java, handler)