createSelector2
Selector<
- T1,
- T2
Creates a memoized selector with two input selectors.
Example:
final getVisibleTodos = createSelector2(
getTodos,
getFilter,
(todos, filter) => switch (filter) {
'completed' => todos.where((t) => t.completed).toList(),
'active' => todos.where((t) => !t.completed).toList(),
_ => todos,
},
);
Implementation
Selector<S, R> createSelector2<S, T1, T2, R>(
Selector<S, T1> selector1,
Selector<S, T2> selector2,
R Function(T1, T2) combiner,
) {
T1? lastInput1;
T2? lastInput2;
R? lastResult;
var hasCache = false;
return (S state) {
final input1 = selector1(state);
final input2 = selector2(state);
if (hasCache &&
identical(input1, lastInput1) &&
identical(input2, lastInput2)) {
return lastResult as R;
}
lastInput1 = input1;
lastInput2 = input2;
lastResult = combiner(input1, input2);
hasCache = true;
return lastResult as R;
};
}