Skip to content

Commit 7cc9702

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(store): memoize selector arguments (#1393)
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
1 parent 009c3bc commit 7cc9702

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

modules/store/spec/selector.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe('Selectors', () => {
9090
selector(firstState);
9191
selector(firstState);
9292
selector(secondState);
93+
selector(secondState);
9394

9495
expect(incrementOne).toHaveBeenCalledTimes(2);
9596
expect(incrementTwo).toHaveBeenCalledTimes(2);
@@ -201,6 +202,7 @@ describe('Selectors', () => {
201202
selector(firstState, props);
202203
selector(firstState, props);
203204
selector(secondState, props);
205+
selector(secondState, props);
204206

205207
expect(counter).toBe(2);
206208
expect(projectFn).toHaveBeenCalledTimes(2);
@@ -322,6 +324,7 @@ describe('Selectors', () => {
322324
selector(firstState);
323325
selector(firstState);
324326
selector(secondState);
327+
selector(secondState);
325328

326329
expect(incrementOne).toHaveBeenCalledTimes(2);
327330
expect(incrementTwo).toHaveBeenCalledTimes(2);
@@ -433,6 +436,7 @@ describe('Selectors', () => {
433436
selector(firstState, props);
434437
selector(firstState, props);
435438
selector(secondState, props);
439+
selector(secondState, props);
436440

437441
expect(counter).toBe(2);
438442
expect(projectFn).toHaveBeenCalledTimes(2);

modules/store/src/selector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ export function defaultMemoize(
7070
return lastResult;
7171
}
7272

73+
lastArguments = arguments;
74+
7375
const newResult = projectionFn.apply(null, arguments);
7476
if (isResultEqual(lastResult, newResult)) {
7577
return lastResult;
7678
}
7779

7880
lastResult = newResult;
79-
lastArguments = arguments;
8081

8182
return newResult;
8283
}

0 commit comments

Comments
 (0)