Created
November 2, 2016 18:55
-
-
Save czyrux/d73503f79c223adfbb7da95dd5b469fe to your computer and use it in GitHub Desktop.
Event bus using RxJava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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