+ Search:
dispatch(addTodo(text))
} />
dispatch(completeTodo(index))
} />
@@ -33,7 +64,7 @@ class App extends Component {
}
App.propTypes = {
- visibleTodos: PropTypes.arrayOf(PropTypes.shape({
+ todos: PropTypes.arrayOf(PropTypes.shape({
text: PropTypes.string.isRequired,
completed: PropTypes.bool.isRequired
})),
@@ -44,22 +75,11 @@ App.propTypes = {
]).isRequired
}
-function selectTodos(todos, filter) {
- switch (filter) {
- case VisibilityFilters.SHOW_ALL:
- return todos
- case VisibilityFilters.SHOW_COMPLETED:
- return todos.filter(todo => todo.completed)
- case VisibilityFilters.SHOW_ACTIVE:
- return todos.filter(todo => !todo.completed)
- }
-}
-
// Which props do we want to inject, given the global state?
// Note: use https://github.com/faassen/reselect for better performance.
function select(state) {
return {
- visibleTodos: selectTodos(state.todos, state.visibilityFilter),
+ todos: state.todos,
visibilityFilter: state.visibilityFilter,
currentTheme: state.currentTheme
}
diff --git a/memoize.js b/memoize.js
new file mode 100644
index 0000000..d3121d3
--- /dev/null
+++ b/memoize.js
@@ -0,0 +1,29 @@
+// This memoizes a function by remembering its last args and its last result
+// and returning the last result if the args are the same as the last call.
+export function memoize(func) {
+ var lastArgs = null;
+ var lastResult;
+
+ function argsDifferent(args) {
+ return lastArgs === null ||
+ lastArgs.length != args.length ||
+ args.some((arg, idx) => { return arg !== lastArgs[idx] });
+ }
+
+ return function(...args) {
+ if(argsDifferent(args)) {
+ lastArgs = args;
+ lastResult = func(...args);
+ }
+ return lastResult
+ }
+}
+
+// This memoizes `func` and returns a function that calls
+// the memoized `func` with the arguments returned by `argFunc`
+export function createMemoizedFunction(argFunc, func) {
+ var memoized = memoize(func);
+ return function() {
+ return memoized(...argFunc());
+ }
+}