svenjacobs
07/30/2018, 11:38 AMConflatedBroadcastChannel
which keeps the last sent item and delivers it to new subscribers in openSubscription()
. I'm looking for an event bus implementation that does not cache items. So if an event is send but there are no active subscriptions the bus should just drop the event. Unfortunately there doesn't seem to be a default implementation of BroadcastChannel
with these characteristics. Has anyone an idea how to implement a simple non-caching event bus?RendezvousBroadcastChannel
.import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.experimental.DefaultDispatcher
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.channels.RendezvousChannel
import kotlinx.coroutines.experimental.channels.filter
import kotlinx.coroutines.experimental.channels.map
import kotlinx.coroutines.experimental.launch
import kotlin.coroutines.experimental.CoroutineContext
/**
* Coroutine-based event bus.
*/
class EventBus {
private val subscribers = atomic<Iterable<Subscriber>>(emptyList())
fun send(event: Any, context: CoroutineContext = DefaultDispatcher) {
launch(context) {
subscribers.value.forEach { subscriber ->
subscriber.send(event)
}
}
}
fun subscribe(): ReceiveChannel<Any> =
Subscriber(subscribers).also { subscriber ->
subscribers.value = subscribers.value + subscriber
}
inline fun <reified T> subscribeToEvent() =
subscribe().filter { it is T }.map { it as T }
private class Subscriber(private val subscribers: AtomicRef<Iterable<Subscriber>>) : RendezvousChannel<Any>() {
override fun cancel(cause: Throwable?): Boolean =
close(cause).also {
subscribers.value = subscribers.value - this
}
}
}
louiscad
07/30/2018, 11:10 PMBroadcastChannel(capacity = 1)
is likely to be what you're looking forsvenjacobs
07/31/2018, 4:58 AMwhen (capacity) {
0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
louiscad
07/31/2018, 9:17 AMBroadcastChannel
, and a capacity of 1 behaves similarly to a RendezVousChannel
, I suggest you give it a trysvenjacobs
08/02/2018, 5:49 AMlouiscad
08/03/2018, 11:46 PM