Last active
February 1, 2017 21:22
-
-
Save feresr/f388ab08b5a5d95a2fe1f4c78e8e2b40 to your computer and use it in GitHub Desktop.
RxDataStore implementation
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 class JokesStore extends RxStore<JokeRequest, JokeResponse> { | |
private JokesEndpoint endpoints; | |
@Inject | |
public JokesStore(JokesEndpoint endpoints) { | |
this.endpoints = endpoints; | |
} | |
@Override | |
protected Observable.Transformer<JokeRequest, JokeResponse> getTransformer() { | |
return new Observable.Transformer<JokeRequest, JokeResponse>() { | |
@Override | |
public Observable<JokeResponse> call(Observable<JokeRequest> jokeRequestObservable) { | |
return jokeRequestObservable.onBackpressureDrop() | |
.observeOn(Schedulers.io()) | |
.flatMap(new Func1<JokeRequest, Observable<JokeResponse>>() { | |
@Override | |
public Observable<JokeResponse> call(final JokeRequest jokeRequest) { | |
if (!jokeRequest.wasConsumed()) { | |
jokeRequest.setConsumed(true); | |
return network(); | |
} else { | |
jokeRequest.setConsumed(true); | |
return Observable.concat(memory(), network()).first(); | |
} | |
} | |
}).observeOn(AndroidSchedulers.mainThread()); | |
} | |
}; | |
} | |
private Observable<JokeResponse> network() {} //see implementation on github repository | |
private Observable<JokeResponse> memory() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment