-
-
Save philsong/e0f2d27b8b19e53cb272df6bc013420e to your computer and use it in GitHub Desktop.
RxBus singleton
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
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