Skip to content

Instantly share code, notes, and snippets.

@PierceZ
Last active December 6, 2017 15:47
Show Gist options
  • Save PierceZ/bced6af7bb6549e1fdeefeb34698144e to your computer and use it in GitHub Desktop.
Save PierceZ/bced6af7bb6549e1fdeefeb34698144e to your computer and use it in GitHub Desktop.
An event bus made with RxJava and RxAndroid
/**
* Used for subscribing to and publishing to subjects. Allowing you to send data between activities, fragments, etc.
* <p>
* Created by Pierce Zaifman on 2017-01-02.
*/
public final class RxBus {
private static SparseArray<PublishSubject<Object>> sSubjectMap = new SparseArray<>();
private static Map<Object, CompositeDisposable> sSubscriptionsMap = new HashMap<>();
public static final int SUBJECT_MY_SUBJECT = 0;
public static final int SUBJECT_ANOTHER_SUBJECT = 1;
@Retention(SOURCE)
@IntDef({SUBJECT_MY_SUBJECT, SUBJECT_ANOTHER_SUBJECT})
@interface Subject {
}
private RxBus() {
// hidden constructor
}
/**
* Get the subject or create it if it's not already in memory.
*/
@NonNull
private static PublishSubject<Object> getSubject(@Subject int subjectCode) {
PublishSubject<Object> subject = sSubjectMap.get(subjectCode);
if (subject == null) {
subject = PublishSubject.create();
subject.subscribeOn(AndroidSchedulers.mainThread());
sSubjectMap.put(subjectCode, subject);
}
return subject;
}
/**
* Get the CompositeDisposable or create it if it's not already in memory.
*/
@NonNull
private static CompositeDisposable getCompositeDisposable(@NonNull Object object) {
CompositeDisposable compositeDisposable = sSubscriptionsMap.get(object);
if (compositeDisposable == null) {
compositeDisposable = new CompositeDisposable();
sSubscriptionsMap.put(object, compositeDisposable);
}
return compositeDisposable;
}
/**
* Subscribe to the specified subject and listen for updates on that subject. Pass in an object to associate
* your registration with, so that you can unsubscribe later.
* <br/><br/>
* <b>Note:</b> Make sure to call {@link RxBus#unregister(Object)} to avoid memory leaks.
*/
public static void subscribe(@Subject int subject, @NonNull Object lifecycle, @NonNull Consumer<Object> action) {
Disposable disposable = getSubject(subject).subscribe(action);
getCompositeDisposable(lifecycle).add(disposable);
}
/**
* Unregisters this object from the bus, removing all subscriptions.
* This should be called when the object is going to go out of memory.
*/
public static void unregister(@NonNull Object lifecycle) {
//We have to remove the composition from the map, because once you dispose it can't be used anymore
CompositeDisposable compositeDisposable = sSubscriptionsMap.remove(lifecycle);
if (compositeDisposable != null) {
compositeDisposable.dispose();
}
}
/**
* Publish an object to the specified subject for all subscribers of that subject.
*/
public static void publish(@Subject int subject, @NonNull Object message) {
getSubject(subject).onNext(message);
}
}
@pishguy
Copy link

pishguy commented Apr 15, 2017

could you add imports on source? i cant use that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment