net.dankito.utils.events.RxEventBus.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-utils Show documentation
Show all versions of java-utils Show documentation
Some basic utils needed in many projects
package net.dankito.utils.events
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.subjects.PublishSubject
class RxEventBus : IEventBus, IRxEventBus {
private val bus = PublishSubject.create()
override fun post(event: Any) {
bus.onNext(event)
}
override fun subscribe(eventClass: Class, listener: (T) -> Unit): ISubscribedEvent {
val subscribedFlowable = subscribe(eventClass)
val disposable = subscribedFlowable.subscribe { listener(it) }
return SubscribedRxEvent(disposable)
}
override fun subscribe(eventClass: Class): Flowable {
return bus
.toFlowable(BackpressureStrategy.BUFFER)
.filter { event -> event.javaClass == eventClass }
.map { event -> event as T }
}
}