Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component-store): fix memoization not working when passing selectors directly to select #3356

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
delay,
concatMap,
} from 'rxjs/operators';
import { createSelector } from '@ngrx/store';

describe('Component Store', () => {
describe('initialization', () => {
Expand Down Expand Up @@ -895,6 +896,23 @@ describe('Component Store', () => {

componentStore.ngOnDestroy();
});

it('supports memoization with createSelector', () => {
const projectorCallback = jest.fn((str: string) => str);
const memoizedSelector = createSelector(
(s: State) => s.value,
projectorCallback
);
const selector = componentStore.select(memoizedSelector);

// first call to memoizedSelector
const subscription = selector.subscribe();
// second call to memoizedSelector with the same value
componentStore.setState(INIT_STATE);

subscription.unsubscribe();
expect(projectorCallback).toHaveBeenCalledTimes(1);
});
});

describe('selector with debounce', () => {
Expand Down
2 changes: 1 addition & 1 deletion modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class ComponentStore<T extends object> implements OnDestroy {
if (observables.length === 0) {
observable$ = this.stateSubject$.pipe(
config.debounce ? debounceSync() : (source$) => source$,
map(projector)
map((state) => projector(state))
);
} else {
// If there are multiple arguments, then we're aggregating selectors, so we need
Expand Down