RxBus.kt 704 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package com.yidianling.ydl_pay.pay

import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/04/18
 */
class RxBus private constructor(){

    companion object {
        fun getInstance(): RxBus {
            return Holder.INSTANCE
        }
    }

    private val mBus: Subject<Any>

    init {
        mBus = PublishSubject.create()
    }

    fun <T>toObservable(tClass: Class<T>): Observable<T> {
        return mBus.ofType(tClass)
    }

    fun post(any: Any) {
        mBus.onNext(any)
    }

    private object Holder {
        val INSTANCE = RxBus()
    }
}