Last active
February 2, 2017 16:58
-
-
Save feresr/5ce6d6f5a8a67ab8820a9a2ed16c1de0 to your computer and use it in GitHub Desktop.
RxStore
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
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