Yet another message queue
This library is so similar to an event bus or a message queue. The differences are
To handle the request-result pairs we used Kotlin coroutines’ biggest power, which is making
callbacks similar to ordinary lines of code with an input and output.
Let’s have some RequestCommand
and ResultCommand
like
data class RequestCommand(val request: String) : Command
data class ResultCommand(val result: String) : Command
we want to have a few lines of coroutines code to send the request and receive the result.
suspend fun CommandExecutor.runCommand(request: String): CommandResult<ResultCommand> =
exe(RequestCommand(request))
scope.launch {
when (val result = executor.runCommand("Are you there?")) {
is Available<*> -> assert(result.command == ResultCommand("Yes! Of course!"))
}
}
There are a few notes here.
exe
will be used tosuspend
functions.is Available<*>
branch of when
will not be called.To receive the request and respond to it
val registration = commandRegister.register(RequestCommand::class,
CommandCallbackImpl(commandShooter) {
ResultCommand("Yes! Of course!")
})
whenever is needed we can dispose the registration
.
Download via gradle
implementation "com.github.hadilq:command-ku-api:$libVersion"
implementation "com.github.hadilq:command-ku-impl:$libVersion"
where you can find the libVersion
in the Releases page of this repository.
Snapshots of the development version are available in Sonatype’s snapshots repository.
Create an issue and suggest your idea and raise your hand if you want to implement it. If it’s okay
please create a pull request. Thank you, you’re awesome.