Skip to content

Instantly share code, notes, and snippets.

@philsong
Forked from tatocaster/RxBus.java
Created October 26, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philsong/e0f2d27b8b19e53cb272df6bc013420e to your computer and use it in GitHub Desktop.
Save philsong/e0f2d27b8b19e53cb272df6bc013420e to your computer and use it in GitHub Desktop.
RxBus singleton
public class RxBus {
private static RxBus instance = null;
private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());
private static RxBus getInstance() {
if (instance == null) {
instance = new RxBus();
}
return instance;
}
public static void broadCast(BaseEvent baseEvent) {
getInstance().send(baseEvent);
}
public static void subscribe(final Action1 onNext) {
getInstance().toObserverable().subscribe(onNext);
}
private void send(BaseEvent baseEvent) {
_bus.onNext(baseEvent);
}
private Observable<Object> toObserverable() {
return _bus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment