Skip to content

Instantly share code, notes, and snippets.

@feresr
Last active February 2, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feresr/5ce6d6f5a8a67ab8820a9a2ed16c1de0 to your computer and use it in GitHub Desktop.
Save feresr/5ce6d6f5a8a67ab8820a9a2ed16c1de0 to your computer and use it in GitHub Desktop.
RxStore
public abstract class RxStore<Input, Output> {
private BehaviorSubject<Input> subject = BehaviorSubject.create();
private Observable<Output> observable;
public final Subscription register(Subscriber<Output> subscriber) {
if (observable == null) {
observable = subject.compose(getTransformer());
}
return observable.subscribe(subscriber);
}
public final void unregister(Subscription subscription) {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
protected abstract Observable.Transformer<Input, Output> getTransformer();
public final void onNext(Input event) {
subject.onNext(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment