Using first version of RxJava and RxAndroid I had following class as EventBus:
public class RxBus {
private static RxBus instance;
private PublishSubject<Object> subject = PublishSubject.create();
public static RxBus instanceOf() {
    if (instance == null) {
        instance = new RxBus();
    }
    return instance;
}
public void setMessage(Object object) {
    subject.onNext(object);
}
public Observable<Object> getEvents() {
    return subject;
}
}
Getting instance via  instanceOf in any class I used setMessage method to emit messages and following code to get emitted messages:
  bus.getEvents().subscribe(new Action1<Object>() {
        @Override
        public void call(Object o) {
            if (o instanceof String) {
                //TODO
            }
        }
    });
Action1 was from rx.functions package. Trying to migrate use RxJava 2 I cannot import it. 
Tell me please, what is the shortest way to use RxJava 2 as EventBus
An event bus is a pipeline that receives events. Rules associated with the event bus evaluate events as they arrive. Each rule checks whether an event matches the rule's criteria. You associate a rule with a specific event bus, so the rule only applies to events received by that event bus.
RxBus - An event bus by ReactiveX/RxJava/ReactiveX/RxAndroid. This is an event bus designed to allowing your application to communicate efficiently.
In RxJava2, the Action1 has been renamed to Consumer.
The remaining action interfaces were named according to the Java 8 functional types. The no argument
Action0is replaced by theio.reactivex.functions.Actionfor the operators andjava.lang.Runnablefor theSchedulermethods.Action1has been renamed toConsumerandAction2is calledBiConsumer.ActionNis replaced by theConsumer<Object[]>type declaration.
See What's different in 2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With