Skip to content

Commit

Permalink
feat(store): add selectSignal options
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and markostanimirovic committed May 3, 2023
1 parent 503e9d8 commit 0a13c4d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
16 changes: 16 additions & 0 deletions modules/store/spec/types/select_signal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ describe('Store.selectSignal()', () => {
`const selector = store.selectSignal(s => s.foo.bar);`
).toInfer('selector', 'Signal<{ baz: []; }>');
});

it('should infer correctly (with options)', () => {
expectSnippet(
`const selector = store.selectSignal(s => s.foo, {equal: (a, b) => a === b});`
).toInfer('selector', 'Signal<{ bar: { baz: []; }; }>');
});
});

describe('with selectors', () => {
Expand All @@ -53,6 +59,16 @@ describe('Store.selectSignal()', () => {
`const selector = store.selectSignal(barSelector);`
).toInfer('selector', 'Signal<{ baz: []; }>');
});

it('should infer correctly (with options)', () => {
expectSnippet(
`const selector = store.selectSignal(fooSelector, {equal: (a,b) => a === b});`
).toInfer('selector', 'Signal<{ bar: { baz: []; }; }>');

expectSnippet(
`const selector = store.selectSignal(barSelector);`
).toInfer('selector', 'Signal<{ baz: []; }>');
});
});
});
});
9 changes: 9 additions & 0 deletions modules/store/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type ValueEqualityFn } from '@angular/core';

export interface Action {
type: string;
}
Expand Down Expand Up @@ -169,3 +171,10 @@ export interface RuntimeChecks {
*/
strictActionTypeUniqueness?: boolean;
}

export interface SelectSignalOptions<T> {
/**
* A comparison function which defines equality for select results.
*/
equal?: ValueEqualityFn<T>;
}
15 changes: 12 additions & 3 deletions modules/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { Observable, Observer, Operator } from 'rxjs';
import { distinctUntilChanged, map, pluck } from 'rxjs/operators';

import { ActionsSubject } from './actions_subject';
import { Action, ActionReducer, FunctionIsNotAllowed } from './models';
import {
Action,
ActionReducer,
SelectSignalOptions,
FunctionIsNotAllowed,
} from './models';
import { ReducerManager } from './reducer_manager';
import { StateObservable } from './state';
import { toSignal } from './to_signal';
Expand Down Expand Up @@ -101,9 +106,13 @@ export class Store<T = object>
* Returns a signal of the provided selector.
*
* @param selector selector function
* @param options select signal options
*/
selectSignal<K>(selector: (state: T) => K): Signal<K> {
return computed(() => selector(this.state()));
selectSignal<K>(
selector: (state: T) => K,
options?: SelectSignalOptions<K>
): Signal<K> {
return computed(() => selector(this.state()), { equal: options?.equal });
}

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

0 comments on commit 0a13c4d

Please sign in to comment.