Last active
February 4, 2017 05:32
-
-
Save dokinkon/dfb3fb751716e9be0c6e52d650c436e5 to your computer and use it in GitHub Desktop.
搭配Dagger2的做法
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 RxBusExample { | |
RxBus bus = RxBus.getInstance(); | |
bus.post(); | |
} |
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
@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); | |
} | |
} |
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; | |
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