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

feat(store): deprecate selectors with props #2993

Merged
merged 2 commits into from
Apr 22, 2021
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
3 changes: 3 additions & 0 deletions modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export interface StoreFeature<T, V extends Action = Action> {

export type Selector<T, V> = (state: T) => V;

/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export type SelectorWithProps<State, Props, Result> = (
state: State,
props: Props
Expand Down
59 changes: 58 additions & 1 deletion modules/store/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface MemoizedSelector<
clearResult: () => void;
}

/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export interface MemoizedSelectorWithProps<
State,
Props,
Expand Down Expand Up @@ -122,6 +125,9 @@ export function createSelector<State, S1, Result>(
s1: Selector<State, S1>,
projector: (s1: S1) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, Result>(
s1: SelectorWithProps<State, Props, S1>,
projector: (s1: S1, props: Props) => Result
Expand All @@ -130,6 +136,9 @@ export function createSelector<State, S1, Result>(
selectors: [Selector<State, S1>],
projector: (s1: S1) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, Result>(
selectors: [SelectorWithProps<State, Props, S1>],
projector: (s1: S1, props: Props) => Result
Expand All @@ -140,15 +149,22 @@ export function createSelector<State, S1, S2, Result>(
s2: Selector<State, S2>,
projector: (s1: S1, s2: S2) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, Result>(
s1: SelectorWithProps<State, Props, S1>,
s2: SelectorWithProps<State, Props, S2>,
projector: (s1: S1, s2: S2, props: Props) => Result
): MemoizedSelectorWithProps<State, Props, Result>;

export function createSelector<State, S1, S2, Result>(
selectors: [Selector<State, S1>, Selector<State, S2>],
projector: (s1: S1, s2: S2) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, Result>(
selectors: [
SelectorWithProps<State, Props, S1>,
Expand All @@ -163,6 +179,9 @@ export function createSelector<State, S1, S2, S3, Result>(
s3: Selector<State, S3>,
projector: (s1: S1, s2: S2, s3: S3) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, Result>(
s1: SelectorWithProps<State, Props, S1>,
s2: SelectorWithProps<State, Props, S2>,
Expand All @@ -173,6 +192,9 @@ export function createSelector<State, S1, S2, S3, Result>(
selectors: [Selector<State, S1>, Selector<State, S2>, Selector<State, S3>],
projector: (s1: S1, s2: S2, s3: S3) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, Result>(
selectors: [
SelectorWithProps<State, Props, S1>,
Expand All @@ -189,6 +211,9 @@ export function createSelector<State, S1, S2, S3, S4, Result>(
s4: Selector<State, S4>,
projector: (s1: S1, s2: S2, s3: S3, s4: S4) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, Result>(
s1: SelectorWithProps<State, Props, S1>,
s2: SelectorWithProps<State, Props, S2>,
Expand All @@ -205,6 +230,9 @@ export function createSelector<State, S1, S2, S3, S4, Result>(
],
projector: (s1: S1, s2: S2, s3: S3, s4: S4) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, Result>(
selectors: [
SelectorWithProps<State, Props, S1>,
Expand All @@ -223,6 +251,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, Result>(
s5: Selector<State, S5>,
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, S5, Result>(
s1: SelectorWithProps<State, Props, S1>,
s2: SelectorWithProps<State, Props, S2>,
Expand All @@ -241,6 +272,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, Result>(
],
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, S5, Result>(
selectors: [
SelectorWithProps<State, Props, S1>,
Expand All @@ -261,6 +295,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, Result>(
s6: Selector<State, S6>,
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, S5, S6, Result>(
s1: SelectorWithProps<State, Props, S1>,
s2: SelectorWithProps<State, Props, S2>,
Expand All @@ -283,13 +320,15 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, Result>(
Selector<State, S1>,
Selector<State, S2>,
Selector<State, S3>,

Selector<State, S4>,
Selector<State, S5>,
Selector<State, S6>
],
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<State, Props, S1, S2, S3, S4, S5, S6, Result>(
selectors: [
SelectorWithProps<State, Props, S1>,
Expand Down Expand Up @@ -320,6 +359,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, S7, Result>(
s7: Selector<State, S7>,
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6, s7: S7) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<
State,
Props,
Expand Down Expand Up @@ -362,6 +404,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, S7, Result>(
],
projector: (s1: S1, s2: S2, s3: S3, s4: S4, s5: S5, s6: S6, s7: S7) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<
State,
Props,
Expand Down Expand Up @@ -415,6 +460,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, S7, S8, Result>(
s8: S8
) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<
State,
Props,
Expand Down Expand Up @@ -470,6 +518,9 @@ export function createSelector<State, S1, S2, S3, S4, S5, S6, S7, S8, Result>(
s8: S8
) => Result
): MemoizedSelector<State, Result>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelector<
State,
Props,
Expand Down Expand Up @@ -545,9 +596,15 @@ export function createSelectorFactory<T = any, V = any>(
memoize: MemoizeFn,
options: SelectorFactoryConfig<T, V>
): (...input: any[]) => MemoizedSelector<T, V>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelectorFactory<T = any, Props = any, V = any>(
memoize: MemoizeFn
): (...input: any[]) => MemoizedSelectorWithProps<T, Props, V>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function createSelectorFactory<T = any, Props = any, V = any>(
memoize: MemoizeFn,
options: SelectorFactoryConfig<T, V>
Expand Down
14 changes: 12 additions & 2 deletions modules/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { ReducerManager } from './reducer_manager';
import { StateObservable } from './state';

@Injectable()
export class Store<T = object> extends Observable<T>
export class Store<T = object>
extends Observable<T>
implements Observer<Action> {
constructor(
state$: StateObservable,
Expand All @@ -21,6 +22,9 @@ export class Store<T = object> extends Observable<T>
}

select<K>(mapFn: (state: T) => K): Observable<K>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
select<K, Props = any>(
mapFn: (state: T, props: Props) => K,
props: Props
Expand Down Expand Up @@ -130,9 +134,15 @@ export class Store<T = object> extends Observable<T>

export const STORE_PROVIDERS: Provider[] = [Store];

export function select<T, K>(
mapFn: (state: T) => K
): (source$: Observable<T>) => Observable<K>;
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
export function select<T, Props, K>(
mapFn: (state: T, props: Props) => K,
props?: Props
props: Props
): (source$: Observable<T>) => Observable<K>;
export function select<T, a extends keyof T>(
key: a
Expand Down
6 changes: 6 additions & 0 deletions projects/ngrx.io/content/guide/store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const selectVisibleBooks = createSelector(

### Using selectors with props

<div class="alert is-critical">

Selectors with props are [deprecated](https://github.com/ngrx/platform/issues/2980).

</div>

To select a piece of state based on data that isn't available in the store you can pass `props` to the selector function. These `props` gets passed through every selector and the projector function.
To do so we must specify these `props` when we use the selector inside our component.

Expand Down