Skip to content

Instantly share code, notes, and snippets.

Last active Jan 19, 2018
What would you like to do?
Managing RxState Starting point
* Many things are omitted to focus on the relevant data.
public class Activity extends AppCompatActivity {
Binder binder;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
private void bind() {
public Observable<Intent> intents() {
return Observable.merge(loadFirstPageIntent(), loadNextPageIntent());
/** on creation */
public Observable<Intent.LoadFirstPageIntent> loadFirstPageIntent() {}
/** on bottom of list */
public Observable<Intent.LoadNextPageIntent> loadNextPageIntent() {}
public void render(ViewState state) {
// Update the UI
* Many things are omitted to focus on the relevant data.
* See Jake's talk for the flow explanation:
class Binder {
private Service service;
Binder(Service service) {
this.service = service;
Observable<ViewState> compose(Observable<Intent> intents) {
return intents
.scan(ViewState.idle(), reducer);
// Emits loading, success and failure events.
private ObservableTransformer<Action.LoadFirstPageAction, Result.LoadFirstPageResult>
// Emits loading, success and failure events.
private ObservableTransformer<Action.LoadNextPageAction, Result.LoadNextPageResult>
private ObservableTransformer<Action, Result> actionToResultTransformer =
actions -> actions.publish(shared -> Observable.merge(
private Action actionFromIntent(Intent intent) {
// Create an Action from an Intent.
private static BiFunction<ViewState, Result, ViewState> reducer =
(previousState, result) -> {
// Create new state from previousState
// Update the new state accordingly to the result
// Return it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment