Once you added the library into your project, you'll need to create a new EventBus instance. If you there's only a single event bus in your project I'd recommend using an object, otherwise store it into a variable for example:
object MyEventBus: EventBus()
Now, you need to declare subscriptions
First, implement the Receiver
interface:
class MyClass: Receiver() {
}
The inline function subscription
allows us to create a
subscription with Kotlin:
private val onMessage = subscription<String>(priority = 0, filters = emptyArray()) { message ->
// Now you can use message!
println("Received $message !")
}
Obviously you don't have to specify a priority of 0 and empty filters but this example just shows you they are available.
Now we just need to register our class instance to the EventManager using
MyEventBus.register(classInstance)
and call our message using
MyEventBus.dispatch("Hewlo GitHub")
Which results into:
Received Hewlo Github !
Kall is a smart library, it'll automatically adapt depending of the situation to provide high performance. Here are a few examples:
- A
Subscription
that has noFilter
will result into aNonFilteredSubscription
: An implementation of Subscription that has no operations associated with filters. - A
Dispatcher
that has noSubscription
will result into a no-opEmptyDispatcher
- A
Dispatcher
that has oneSubscription
will result into aSingletonDispatcher
which is made for a singleSubscription
- N3xuz for teaching me a lot of little tricks to optimize the library