probably I’m trying to reinvent the wheel, but I’m...
# codereview
a
probably I’m trying to reinvent the wheel, but I’m writing simple implementation of RxBus. here what I have:
Copy code
class RxBus {

    private val subjects: MutableMap<Class<Any>, PublishSubject<Any>> = mutableMapOf()

    fun publish(event: Any) {
        val subject = getSubject(event.javaClass)
        subject.onNext(event)
    }

    fun <T : Any> observe(clazz: Class<T>): Observable<T> {
        val subject = getSubject(clazz as Class<Any>)
        return subject.asObservable() as Observable<T>
    }

    private fun getSubject(clazz: Class<Any>): PublishSubject<Any> {
        var subject = subjects[clazz]
        if (subject == null) {
            subject = PublishSubject.create()!!
            subjects.put(clazz, subject)
        }
        return subject
    }
}
any suggestions to improve this code? especially I want to avoid unchecked casts inside the observe method and have PublishSubject with the same type as a key for this PublishSubject inside the
subjects
map