Skip to content

a simple implementation of EventBus, only for study propose.

License

Notifications You must be signed in to change notification settings

glowingstone124/SimpleEventBus

Repository files navigation

Simple EventBUS

This is a simple eventbus implementation written in kotlin.

Functions

Support Listener register, unregister and callOnce listeners.

Using Kotlin 2.0.20 and Kotlin-Reflection, Kotlin-Coroutines.

Usage

To create a event, create a KClass and implement AbstractEvent

To create a listener, you can choose two method:

Lambda Callback

val eventBus = EventBus()
val instance1 = eventBus.register(CustomEvent::class) { // This should be called everytime
    println("I will always be here $it")
}

Annotationed Functions

class Test {
    @onEvent(eventType = CustomEvent::class)
    fun newEventHandler(event: CustomEvent) {
        println("112233445566778899 ${event.name}")
    }
}
//Main
val test = Test()
//register Instance
eventBus.register(test)

Call once

eventBus.callOnce(CustomEvent::class) { // This will only print once
        println("I will only be called once!")
}

About

a simple implementation of EventBus, only for study propose.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages