Skip to content

Commit

Permalink
example optimization : reselect alibaba#492
Browse files Browse the repository at this point in the history
  • Loading branch information
zjuwjf committed Oct 12, 2019
1 parent 9f358b5 commit 815fa75
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
28 changes: 19 additions & 9 deletions example/lib/todo_list_page/list_adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ class ToDoListAdapter extends DynamicFlowAdapter<PageState> {
);
}

class _ToDoListConnector extends ConnOp<PageState, List<ItemBean>> {
class _ToDoListConnector extends ConnOp<PageState, List<ItemBean>>

/// [https://github.com/alibaba/fish-redux/issues/482] #482
with
ReselectMixin<PageState, List<ItemBean>> {
@override
void set(PageState state, List<ItemBean> toDos) {
if (toDos?.isNotEmpty == true) {
state.toDos = List<ToDoState>.from(
toDos.map<ToDoState>((ItemBean bean) => bean.data).toList());
} else {
state.toDos = <ToDoState>[];
}
}

@override
List<ItemBean> get(PageState state) {
List<ItemBean> computed(PageState state) {
if (state.toDos?.isNotEmpty == true) {
return state.toDos
.map<ItemBean>((ToDoState data) => ItemBean('toDo', data))
Expand All @@ -27,13 +41,9 @@ class _ToDoListConnector extends ConnOp<PageState, List<ItemBean>> {
}
}

/// watched factors
@override
void set(PageState state, List<ItemBean> toDos) {
if (toDos?.isNotEmpty == true) {
state.toDos = List<ToDoState>.from(
toDos.map<ToDoState>((ItemBean bean) => bean.data).toList());
} else {
state.toDos = <ToDoState>[];
}
List<dynamic> factors(PageState state) {
return <dynamic>[state.toDos];
}
}
10 changes: 4 additions & 6 deletions example/lib/todo_list_page/list_adapter/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ Reducer<PageState> buildReducer() {

PageState _add(PageState state, Action action) {
final ToDoState toDo = action.payload;
final PageState newState = state.clone();
newState.toDos.add(toDo);
return newState;
return state.clone()..toDos = (state.toDos.toList()..add(toDo));
}

PageState _remove(PageState state, Action action) {
final String unique = action.payload;
final PageState newState = state.clone();
newState.toDos.removeWhere((ToDoState state) => state.uniqueId == unique);
return newState;
return state.clone()
..toDos = (state.toDos.toList()
..removeWhere((ToDoState state) => state.uniqueId == unique));
}

0 comments on commit 815fa75

Please sign in to comment.