KDispatcher simple and light-weight event bus for Kotlin

Alexandr Minkin
3 min readJul 20, 2018
by JetBrains

Hi everyone. This is my first story and i want to tell you about the great and useful android library that i was writen about one year ago. It’s called KDispatcher.

Now you can sequence many aspect of your Kotlin development with each other. It’s write on clear Kotlin with some of inline function and extension functions of that powerful language. First of all i want to say thanks to JetBrains (http://jetbrains.ru) and all http://kotlinlang.org/ team.

The main goals of it’s event dispatcher is:
1) Platform independent (you can use it in android app or backend or k/n apps)
2) It’s use an extensions function so it’s simple to integrate in your existing projects
3) KDispatcher has priority to sort your event handling function for execute one by one with given aspects
4) You can transfer any kind of data in you events notifications and you can listen another type of event in a single handler (see example project)
5) And for the final KDispatcher is thread-safe cause it use a kotlin singleton object to manipulate your events notifications

So i will start with some small example of how you can use it this library in you project. First of all you need to declare library implementation
Gradle:

implementation 'com.rasalexman.kdispatcher:kdispatcher:x.y.z'

Maven:

<dependency>
<groupId>com.rasalexman.kdispatcher</groupId>
<artifactId>kdispatcher</artifactId>
<version>x.y.z</version>
<type>pom</type>
</dependency>

Then you have two choises how to use it:
1) you can simple use Singleton Object:

val EVENT_CALL_ONE = "simple_event_name"
val eventListener = ::eventHandler
val priority = 1
KDispatcher.subscribe(EVENT_CALL_ONE, eventListener, priority)

where:

  • EVENT_CALL_ONE — simple eventName :String
  • eventListener — function listener for event
  • priority — the priority to sort calling functions
/**
* notif:Notification<T:Any> - event holder object that store
* data:T? = null - can be any type of data
* eventName:String? = null - current event type
* cause u may have more then one EVENT_TYPE for current event listener
*/
fun eventHandler(notif:Notification<Any>){
when(notif.eventName){
EVENT_CALL_ONE -> println("FIRST EVENT with data ${notif.data}")
}
}

2) You can implement an IKDispatcher interface and has a control to manipulate your event inside you calling and subscribing objects like this

class MyAnyClassCaller : IKDispatcher {
// some work function
fun doSomeWorkAndNotifyOthers() {
//.... do some work
val anyData:Any = "String for data"
call("event_name_for_handling", anyData)
}
}
class MyAnyClassListener : IKDispatcher {
init {
val priority = 1
subscribe("event_name_for_handling", priority, ::eventHandler)
// or
subscribe("event_name_for_handling", 2) { notif ->
// do anything you want with notif.data and notif.eventName
}
// or without priority (default 0)
subscribe("event_name_for_handling") {
// do anything you want with it.data and it.eventName
}
}
fun eventHandler(notif:Notification<Any>) {
// do anything you want with notif.data and notif.eventName
}
}

And don’t forget to unsubscribe you event handlers when you don’t need it anymore with just one call unsubscribe(eventName, eventHandler?)

If you like this library, please share and support it on github:
https://github.com/Rasalexman/KDispatcher

Also i need a feedback for future improvement and more core complex realisations. Thanks for reading and have a good coding)

--

--

Alexandr Minkin

Lead Software Developer. Kotlin enthusiast, Android developer, Swift lover, Clean Architecture Supporter