All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dankito.utils.events.RxEventBus.kt Maven / Gradle / Ivy

There is a newer version: 1.0.20
Show newest version
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 }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy