import kotlinx.coroutines.experimental.channels.BroadcastChannel import kotlinx.coroutines.experimental.channels.ConflatedBroadcastChannel import kotlinx.coroutines.experimental.channels.ReceiveChannel import kotlinx.coroutines.experimental.channels.filter import kotlinx.coroutines.experimental.channels.map import kotlinx.coroutines.experimental.launch import javax.inject.Inject import javax.inject.Singleton /** * You can use like this. * val channel = EventBus().asChannel() * launch (UI){ * for(action in channel){ * // You can use item * action.item * } * } */ @Singleton class EventBus @Inject constructor() { val bus: BroadcastChannel = ConflatedBroadcastChannel() fun send(o: Any) { launch { bus.send(o) } } inline fun asChannel(): ReceiveChannel { return bus.openSubscription().filter { it is T }.map { it as T } } }