Skip to content

Commit

Permalink
fix(store): memoize selector arguments (#1393)
Browse files Browse the repository at this point in the history
The arguments weren't being memoized when the result of the selector was equal to its previous result. This fix moves the memoization of the arguments before the check if the results are equal.

Closes #1389
  • Loading branch information
timdeschryver authored and brandonroberts committed Nov 1, 2018
1 parent 009c3bc commit 7cc9702
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions modules/store/spec/selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('Selectors', () => {
selector(firstState);
selector(firstState);
selector(secondState);
selector(secondState);

expect(incrementOne).toHaveBeenCalledTimes(2);
expect(incrementTwo).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -201,6 +202,7 @@ describe('Selectors', () => {
selector(firstState, props);
selector(firstState, props);
selector(secondState, props);
selector(secondState, props);

expect(counter).toBe(2);
expect(projectFn).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -322,6 +324,7 @@ describe('Selectors', () => {
selector(firstState);
selector(firstState);
selector(secondState);
selector(secondState);

expect(incrementOne).toHaveBeenCalledTimes(2);
expect(incrementTwo).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -433,6 +436,7 @@ describe('Selectors', () => {
selector(firstState, props);
selector(firstState, props);
selector(secondState, props);
selector(secondState, props);

expect(counter).toBe(2);
expect(projectFn).toHaveBeenCalledTimes(2);
Expand Down
3 changes: 2 additions & 1 deletion modules/store/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ export function defaultMemoize(
return lastResult;
}

lastArguments = arguments;

const newResult = projectionFn.apply(null, arguments);
if (isResultEqual(lastResult, newResult)) {
return lastResult;
}

lastResult = newResult;
lastArguments = arguments;

return newResult;
}
Expand Down

0 comments on commit 7cc9702

Please sign in to comment.