Skip to content

Commit

Permalink
fix(store): add Signal equal function for immutable object comparison (
Browse files Browse the repository at this point in the history
…#3883)

Co-authored-by: Brandon Roberts <robertsbt@gmail.com>
Co-authored-by: Marko Stanimirović <markostanimirovic95@gmail.com>
  • Loading branch information
3 people committed May 9, 2023
1 parent c870b30 commit 634fdcb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 23 additions & 0 deletions modules/store/spec/integration_signals.spec.ts
Expand Up @@ -12,6 +12,7 @@ import {
VisibilityFilters,
resetId,
} from './fixtures/todos';
import { computed } from '@angular/core';

interface Todo {
id: number;
Expand Down Expand Up @@ -130,4 +131,26 @@ describe('NgRx and Signals Integration spec', () => {
expect(error).toBeUndefined();
});
});

describe('immutable state integration spec', () => {
it('Store.selectSignal should not trigger on unrelated global state changes', () => {
let todosTriggerCount = 0;

const todos = store.selectSignal((state) => state.todos);

const todosTriggerState = computed(() => {
todos();
return ++todosTriggerCount;
});

store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } });
expect(todosTriggerState()).toBe(1);

store.dispatch({
type: SET_VISIBILITY_FILTER,
payload: VisibilityFilters.SHOW_ACTIVE,
});
expect(todosTriggerState()).toBe(1);
});
});
});
4 changes: 3 additions & 1 deletion modules/store/src/store.ts
Expand Up @@ -114,7 +114,9 @@ export class Store<T = object>
selector: (state: T) => K,
options?: SelectSignalOptions<K>
): Signal<K> {
return computed(() => selector(this.state()), { equal: options?.equal });
return computed(() => selector(this.state()), {
equal: options?.equal || ((previous, current) => previous === current),
});
}

override lift<R>(operator: Operator<T, R>): Store<R> {
Expand Down

0 comments on commit 634fdcb

Please sign in to comment.