Last active
March 16, 2023 10:44
-
-
Save jaredsburrows/e9706bd8c7d587ea0c1114a0d7651d13 to your computer and use it in GitHub Desktop.
RxBus for RxJava 1 and RxJava 2
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 rx.Observable; | |
import rx.subjects.PublishSubject; | |
import rx.subjects.SerializedSubject; | |
import rx.subjects.Subject; | |
/** | |
* @author <a href="mailto:[email protected]">Jared Burrows</a> | |
*/ | |
public final class RxBus { | |
private final Subject<Object, Object> bus = new SerializedSubject<>(PublishSubject.create()); | |
public void send(final Object event) { | |
bus.onNext(event); | |
} | |
public Observable<Object> toObservable() { | |
return bus; | |
} | |
public boolean hasObservers() { | |
return bus.hasObservers(); | |
} | |
} |
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; | |
/** | |
* @author <a href="mailto:[email protected]">Jared Burrows</a> | |
*/ | |
public final class RxBus { | |
private final Relay<Object, Object> bus = PublishRelay.create().toSerialized(); | |
public void send(final Object event) { | |
bus.call(event); | |
} | |
public Observable<Object> toObservable() { | |
return bus; | |
} | |
public boolean hasObservers() { | |
return bus.hasObservers(); | |
} | |
} |
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 io.reactivex.Observable; | |
import io.reactivex.subjects.PublishSubject; | |
/** | |
* @author <a href="mailto:[email protected]">Jared Burrows</a> | |
*/ | |
public final class RxBus { | |
private final PublishSubject<Object> bus = PublishSubject.create(); | |
public void send(final Object event) { | |
bus.onNext(event); | |
} | |
public Observable<Object> toObservable() { | |
return bus; | |
} | |
public boolean hasObservers() { | |
return bus.hasObservers(); | |
} | |
} |
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.rxrelay2.PublishRelay; | |
import com.jakewharton.rxrelay2.Relay; | |
import io.reactivex.Observable; | |
/** | |
* @author <a href="mailto:[email protected]">Jared Burrows</a> | |
*/ | |
public final class RxBus { | |
private final Relay<Object> bus = PublishRelay.create().toSerialized(); | |
public void send(Object event) { | |
bus.accept(event); | |
} | |
public Observable<Object> toObservable() { | |
return bus; | |
} | |
public boolean hasObservers() { | |
return bus.hasObservers(); | |
} | |
} |
@PaulWoitaschek That is great for filtering events to listen for.
@igoticecream Yeah. I'll update the gists with https://github.com/JakeWharton/RxRelay.
A activity send msg , B activity can receive msg?
@xwc520 You can do that with the RxBus.
How do you unsubscribe?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think is better to use RxRelay from Jake Wharton... because if onError occurs, the bus is dead.