Skip to content

Commit

Permalink
Gen observe method with BackpressureStrategy param
Browse files Browse the repository at this point in the history
  • Loading branch information
wakwak3125 committed Jan 3, 2018
1 parent 9cc9089 commit 5e28f4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;

/**
Expand All @@ -29,16 +30,19 @@ enum Kind {
DISPATCH,
GETTER,
OBSERVE,
OBSERVE_WITH_BPS,
UNKNOWN
}

private static final Class OBSERVE_METHOD_CLASS = Flowable.class;
private static final Class BACKPRESSURE_STRATEGY_PARAM_CLASS = BackpressureStrategy.class;

private final ExecutableElement element;
private final StoreModel storeModel;
private final Kind kind;
private final DeclaredType returnType;
private boolean isBindable = false;
private String bpsParamName = "";

public StoreMethodModel(ExecutableElement element, StoreModel storeModel) {
this.element = element;
Expand All @@ -61,7 +65,14 @@ public TypeName apply(StoreImplModel input) {
kind = Kind.DISPATCH;
} else if (MoreTypes.isTypeOf(OBSERVE_METHOD_CLASS, returnType.asElement().asType())
&& states.contains(ClassName.get(returnType.getTypeArguments().get(0)))) {
kind = Kind.OBSERVE;
if (element.getParameters() != null
&& !element.getParameters().isEmpty()
&& MoreTypes.isTypeOf(BACKPRESSURE_STRATEGY_PARAM_CLASS, element.getParameters().get(0).asType())) {
bpsParamName = element.getParameters().get(0).getSimpleName().toString();
kind = Kind.OBSERVE_WITH_BPS;
} else {
kind = Kind.OBSERVE;
}
} else {
kind = Kind.UNKNOWN;
}
Expand Down Expand Up @@ -99,6 +110,17 @@ public boolean apply(StoreImplModel input) {
})
.get(0).getVariableName(),
StoreImplModel.STATE_OBSERVE_METHOD_NAME).build();
case OBSERVE_WITH_BPS:
return CodeBlock.builder().addStatement("return $N.$N(" + bpsParamName + ")",
FluentIterable.from(storeModel.getStoreImplModels())
.filter(new Predicate<StoreImplModel>() {
@Override
public boolean apply(StoreImplModel input) {
return ClassName.get(returnType.getTypeArguments().get(0)).equals(input.getState());
}
})
.get(0).getVariableName(),
StoreImplModel.STATE_OBSERVE_METHOD_NAME).build();
case DISPATCH:
return CodeBlock.builder()
.addStatement("return $N.$N($N)", DispatcherModel.VARIABLE_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import info.izumin.android.droidux.annotation.Store;
import info.izumin.android.droidux.example.todomvc.entity.TodoList;
import info.izumin.android.droidux.example.todomvc.reducer.TodoListReducer;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;

/**
Expand All @@ -13,4 +14,5 @@
public interface RootStore extends BaseStore {
TodoList todoList();
Flowable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList(BackpressureStrategy strategy);
}

0 comments on commit 5e28f4a

Please sign in to comment.