package cn.tee3.manager.util;

import android.util.Log;

import io.reactivex.Flowable;
import io.reactivex.processors.FlowableProcessor;
import io.reactivex.processors.PublishProcessor;

public class RxBus {
    private final FlowableProcessor<Object> mBus;

    private RxBus() {
        // toSerialized method made bus thread safe
        mBus = PublishProcessor.create().toSerialized();
    }

    public static RxBus getInstance() {
        return Holder.BUS;
    }

    public void post(Object obj) {
        if (obj == null) {
            Log.w("RxBus", "RxBus.post(obj) param is null!");
            return;
        }
        mBus.onNext(obj);
    }

    public <T> Flowable<T> toObservable(Class<T> tClass) {
        return mBus.ofType(tClass);
    }

    public Flowable<Object> toObservable() {
        return mBus;
    }

    public boolean hasSubscribers() {
        return mBus.hasSubscribers();
    }

    private static class Holder {
        private static final RxBus BUS = new RxBus();
    }
}
