KDispatcher - easy and convenient eventbus for everyday use.

Hello, dear colleagues, Kotlin lovers

In my first article I want to tell you about one useful library that I wrote, exclusively on Kotlin, and actively support and develop. It is called, - KDispatcher. Designed for sending and subscribing to notifications between your code components. You can use it in any kotlin-project, be it Android, Frontend, Backend or KotlinNative.

Main advantages:

  • Quick and easy event subscription
  • The priority of calls to listeners (callbacks)
  • Using kotlin extension functions
  • Flow safety

So, to use this library in your project, you need to connect it through Gradle:

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

or Maven:

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

After that, you can use two methods:

1) Call the object method:

KDispatcher.subscribe(eventName, ::listenerFunction, priority)

where, eventName is the string name of your event for which you want to subscribe; :: listenerFunction is a function that takes one parameter of Notification type and can be of the form

(Notification<T>)->Unit

contains your data object (data: T) and event name (eventName: String). The last, optional parameter, priority: Int is a numeric variable to sort the callback functions of your callback functions, since you can subscribe several listeners to one event at once. To pass an event you need to call a method:

KDispatcher.call(eventName:String, data:Any)

You can also unsubscribe from listening to events when you no longer need them, using the method:

KDispatcher.unsubscribe(eventName:String, ::listenerFunction)

Everything is very simple.

2) The second way is to embed your classes (the word is certainly not very, but I hope everyone will understand) from the IKDispatcher interface and you can get all the functionality of the event framework, and even more, due to kotlin extension functions. Below I want to give a small example of using this interface:

classMainActivity : AppCompatActivity(), IKDispatcher {
    privateval eventListenerOne = this::eventOneHandler
    //...overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        scopeOperation()
    }
    privatefunscopeOperation() {
        // подписываемся на события используя разные способы
        subscribe(EVENT_CALL_ONE, 3, eventListenerOne)
        subscribe(EVENT_CALL_ONE, 1, ::eventListenerTwo)
        subscribe(EVENT_CALL_ONE, 2, MyClass::eventListenerFour)
        // вызываем событие
        call(EVENT_CALL_ONE, "FIRST CALL FROM KDISPATCHER")
        /**
         * так же вы можете использовать lambda-выражения
         */val eventName = "LAMBDA_EVENT"
        subscribe<String>(eventName) { notification ->
            println("LAMBDA_EVENT HAS FIRED with event name ${notification.eventName} and data ${notification.data}")
            unsubscribe(notification.eventName)
        }
        call(eventName, "FIRST CALL CUSTOM LABDA EVENT")
    }
    funeventOneHandler(notification:Notification<Any>) {
       println("eventOneHandler MY TEST IS COMING event = ${notification.eventName} AND data = ${notification.data}")
    }
}

You can find a complete example of use
at the KDispatcher link on the githabe.

Thank you for reading to the end. I would be very happy feedback and assistance in the development of the project.

Enjoy your coding in the amazing KOTLIN language!

Also popular now: