Tiny and lightweight event handling system for Java/Kotlin. It's an annotation-based implementation of the observer pattern that takes away all the boilerplate that usually comes with it, making it super easy to use.
Have all events implementing the Event
interface.
class PersonOnlineEvent(val name: String) : Event
Every class that wants to subscribe to one or more Event
must implement the Subscriber
interface.
class Bob : Subscriber
class Joe : Subscriber
Every method that is listening to events should be marked with the @Subscription
annotation.
Those methods should only have one parameter, and that parameter should be an implementation of an Event
.
class Bob : Subscriber {
@Subscription
fun greet(event: PersonOnlineEvent) = println("Bob says: Hi, ${event.name}!")
}
class Joe : Subscriber {
@Subscription
fun greet(event: PersonOnlineEvent) = println("Joe says: Hey ${event.name}.")
}
val bob: Bob = Bob()
val joe: Joe = Joe()
Reporter.registerAll(bob, joe)
Reporter.report(PersonOnlineEvent(name = "Jon"))
Bob says: Hi, Jon!
Joe says: Hey Jon.