Skip to content

Instantly share code, notes, and snippets.

@dokinkon
Last active February 4, 2017 05:32
Show Gist options
  • Save dokinkon/dfb3fb751716e9be0c6e52d650c436e5 to your computer and use it in GitHub Desktop.
Save dokinkon/dfb3fb751716e9be0c6e52d650c436e5 to your computer and use it in GitHub Desktop.
搭配Dagger2的做法
public class RxBusExample {
RxBus bus = RxBus.getInstance();
bus.post();
}
@Singleton
public class RxBus {
@Inject
protected RxBus() {
}
private final Subject<Object, Object> busSubject = new SerializedSubject<>(PublishSubject.create());
@NonNull
public <T> Observable<T> subscribe(final Class<T> eventClass) {
return busSubject.filter(e -> e.getClass().equals(eventClass))
.map(o -> (T)o);
}
public void post(Object event) {
busSubject.onNext(event);
}
}
public class RxBus {
private static RxBus instance;
public RxBus getInstance() {
if (instance == null) {
instance = new RxBus();
}
return instance;
}
protected RxBus() {
}
private final Subject<Object, Object> busSubject = new SerializedSubject<>(PublishSubject.create());
@NonNull
public <T> Observable<T> subscribe(final Class<T> eventClass) {
return busSubject.filter(e -> e.getClass().equals(eventClass))
.map(o -> (T)o);
}
public void post(Object event) {
busSubject.onNext(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment