Extension for EventSys to support Kotlin Coroutines ReceiveChannel as Listeners
val listenerRegistry = //...
val manager = //...
val factory = EventsChannelFactoryImpl(listenerRegistry)
factory.channel<ConnectEvent>()
.consumeAsFlow()
.map { it.user }
.filter { user -> user.age >= 16 }
.collect { user ->
// ...
}
Works in the same way as EventSys Factory, but generates a stub interface that return ReceiveChannel<T>
event handlers (this means that returned channel receives data from a listener which handles the annotated event). The value
of @ReceiveChannelEvent cannot be the same as of the @Factory (obvious reasons).
To generate implementation of generated stub observable event handler interface, you can use ReceiveChannelEventsInterfaceGeneratorImpl
(default implementation of ReceiveChannelEventsInterfaceGenerator
).
Example:
@ReceiveChannelEvent("com.github.koresframework.eventsys.coroutine.example.ExampleEvents")
interface BuyEvent : Event {
val user: User
val amount: Int
}
class ReceiveBuyEventExample {
suspend fun example() {
val factory = ...
val generator = ReceiveChannelEventsInterfaceGeneratorImpl(factory)
val exampleEvents = generator.create(ExampleEvents::class.java) // ExampleEvents = Generated stub interface
exampleEvents.buyEvent()
.consumeAsFlow()
.map { it.user }
.filter { it.age >= 18 }
.collect { ... }
}
}