Hey hey :) After using this library a bit more and then comparing it to built_redux, one feature that I really liked from built_redux were the ReducerBuilder and MiddlewareBuilder classes. They allow you to bind actions of a given Type to a specific Reducer or Middleware. I realized: We can totally achieve the same result!
While this can result in slightly more code at times, it can be easier to quickly scan to see how each action is being handled and ensure the functions are type-safe.
Let me know whatcha think!
This is a "Before" and "After" of what these additional utilities provide.
Reducer Before
List<Todo> handRolledTodosReducer(List<Todo> todos, action) {
if (action is AddTodoAction) {
return new List.from(todos)
..add(action.todo);
} else if (action is DeleteTodoAction) {
return todos.where((todo) => todo.id != action.id).toList();
} else if (action is UpdateTodoAction) {
return todos
.map((todo) => todo.id == action.id ? action.updatedTodo : todo)
.toList();
} else if (action is ClearCompletedAction) {
return todos.where((todo) => !todo.complete).toList();
} else if (action is ToggleAllAction) {
final allComplete = allCompleteSelector(todos);
return todos.map((todo) => todo.copyWith(complete: !allComplete)).toList();
} else if (action is TodosLoadedAction) {
return action.todos;
} else if (action is TodosNotLoadedAction) {
return [];
} else {
return todos;
}
}
Hey hey :) After using this library a bit more and then comparing it to
built_redux
, one feature that I really liked frombuilt_redux
were theReducerBuilder
andMiddlewareBuilder
classes. They allow you to bind actions of a givenType
to a specificReducer
orMiddleware
. I realized: We can totally achieve the same result!While this can result in slightly more code at times, it can be easier to quickly scan to see how each action is being handled and ensure the functions are type-safe.
Let me know whatcha think!
This is a "Before" and "After" of what these additional utilities provide.
Reducer Before
Reducer After
Middleware Before
Middleware After