Skip to content

Instantly share code, notes, and snippets.

@czyrux
Created November 2, 2016 18:55
Show Gist options
  • Save czyrux/d73503f79c223adfbb7da95dd5b469fe to your computer and use it in GitHub Desktop.
Save czyrux/d73503f79c223adfbb7da95dd5b469fe to your computer and use it in GitHub Desktop.
Event bus using RxJava
import com.jakewharton.rxrelay.PublishRelay;
import com.jakewharton.rxrelay.Relay;
import rx.Observable;
public class RxBus {
private static final RxBus INSTANCE = new RxBus();
private final Relay<Object, Object> busSubject = PublishRelay.create().toSerialized();
public static RxBus getInstance() {
return INSTANCE;
}
private RxBus() { }
public <T> Observable<T> register(Class<T> eventClass) {
return busSubject
.filter(event -> event.getClass().equals(eventClass))
.map(obj -> (T) obj);
}
public void post(Object event) {
busSubject.call(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment