ChannelEventBus
Multi-keys, multi-producers, single-consumer event bus backed by Channels.
This bus is thread-safe to be used by multiple threads. It is safe to send events from multiple threads without any synchronization.
ChannelEvent.Key will be used to identify a bus for a specific type of events. Each bus has a Channel to send events to and a Flow to receive events from.
The Channel is unbounded Channel.UNLIMITED (default) or conflated Channel.CONFLATED. The Flow is cold and only one collector is allowed at a time. This make sure all events are consumed.
Basic usage:
// Create your event type
data class AwesomeEvent(val payload: Int) : ChannelEvent<AwesomeEvent> {
override val key get() = Key
companion object Key : ChannelEventKey<AwesomeEvent>(AwesomeEvent::class)
}
// Create your bus instance
val bus = ChannelEventBus()
// Send events to the bus
bus.send(AwesomeEvent(1))
bus.send(AwesomeEvent(2))
bus.send(AwesomeEvent(3))
// Receive events from the bus
bus.receiveAsFlow(AwesomeEvent)
.collect { e: AwesomeEvent -> println(e) }
Content copied to clipboard
Functions
Link copied to clipboard
abstract fun closeKey(key: ChannelEventKey<*>, validations: Set<ValidationBeforeClosing> = ValidationBeforeClosing.ALL)
Close the bus identified by key.
Link copied to clipboard
Receive events from the bus identified by ChannelEvent.key.
Link copied to clipboard
abstract fun <E : ChannelEvent<E>> send(event: E, option: OptionWhenSendingToBusDoesNotExist = CREATE_NEW_BUS)
Send event to the bus identified by ChannelEvent.key.