Created
November 2, 2017 12:07
-
-
Save abdallahalaraby/6e68205c9f50f89e4a2ac1c17aa9ac48 to your computer and use it in GitHub Desktop.
An event bus implementation that uses RxJava2
This file contains hidden or 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 io.reactivex.Observable; | |
import io.reactivex.subjects.PublishSubject; | |
public class RxBus { | |
private static volatile RxBus sRxBus = null; | |
private PublishSubject<Object> mPublishSubject = PublishSubject.create(); | |
private RxBus() { | |
} | |
public static RxBus getInstance() { | |
if (sRxBus == null) { | |
synchronized (RxBus.class) { | |
if (sRxBus == null) { | |
sRxBus = new RxBus(); | |
} | |
} | |
} | |
return sRxBus; | |
} | |
public <T> Observable<T> subscribe(Class<T> cls) { | |
return mPublishSubject | |
.filter(o -> o.getClass().equals(cls)) | |
.map(o -> (T) o); | |
} | |
public void post(Object obj) { | |
mPublishSubject.onNext(obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment