Skip to content

Commit 38bce88

Browse files
authored
fix(component-store): memoization not working when passing selectors directly to select (#3356)
1 parent 56aedfd commit 38bce88

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

modules/component-store/spec/component-store.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
delay,
2424
concatMap,
2525
} from 'rxjs/operators';
26+
import { createSelector } from '@ngrx/store';
2627

2728
describe('Component Store', () => {
2829
describe('initialization', () => {
@@ -895,6 +896,23 @@ describe('Component Store', () => {
895896

896897
componentStore.ngOnDestroy();
897898
});
899+
900+
it('supports memoization with createSelector', () => {
901+
const projectorCallback = jest.fn((str: string) => str);
902+
const memoizedSelector = createSelector(
903+
(s: State) => s.value,
904+
projectorCallback
905+
);
906+
const selector = componentStore.select(memoizedSelector);
907+
908+
// first call to memoizedSelector
909+
const subscription = selector.subscribe();
910+
// second call to memoizedSelector with the same value
911+
componentStore.setState(INIT_STATE);
912+
913+
subscription.unsubscribe();
914+
expect(projectorCallback).toHaveBeenCalledTimes(1);
915+
});
898916
});
899917

900918
describe('selector with debounce', () => {

modules/component-store/src/component-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export class ComponentStore<T extends object> implements OnDestroy {
244244
if (observables.length === 0) {
245245
observable$ = this.stateSubject$.pipe(
246246
config.debounce ? debounceSync() : (source$) => source$,
247-
map(projector)
247+
map((state) => projector(state))
248248
);
249249
} else {
250250
// If there are multiple arguments, then we're aggregating selectors, so we need

0 commit comments

Comments
 (0)