Skip to content

Commit

Permalink
feat(router-store): rename getSelectors to getRouterSelectors (#3745)
Browse files Browse the repository at this point in the history
Closes #3738
  • Loading branch information
markostanimirovic authored Jan 11, 2023
1 parent c21ee92 commit 7ad76b8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
10 changes: 5 additions & 5 deletions modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
getSelectors,
getRouterSelectors,
RouterReducerState,
DEFAULT_ROUTER_FEATURENAME,
createRouterSelector,
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('Router State Selectors', () => {
router: mockData,
};

selectors = getSelectors();
selectors = getRouterSelectors();
});

it('should create selectCurrentRoute selector for selecting the current route', () => {
Expand All @@ -148,7 +148,7 @@ describe('Router State Selectors', () => {
const stateOverwrite = {
anotherRouterKey: mockData,
};
const selectorOverwrite = getSelectors(
const selectorOverwrite = getRouterSelectors(
(state: typeof stateOverwrite) => state.anotherRouterKey
);

Expand All @@ -162,7 +162,7 @@ describe('Router State Selectors', () => {
const stateOverwrite = {
[DEFAULT_ROUTER_FEATURENAME]: mockData,
};
const selectorOverwrite = getSelectors(createRouterSelector());
const selectorOverwrite = getRouterSelectors(createRouterSelector());

const result = selectorOverwrite.selectCurrentRoute(stateOverwrite);
expect(result).toEqual(
Expand All @@ -178,7 +178,7 @@ describe('Router State Selectors', () => {
const state: State = {
router: undefined,
};
selectors = getSelectors((state: State) => state.router);
selectors = getRouterSelectors((state: State) => state.router);

const result = selectors.selectCurrentRoute(state);

Expand Down
8 changes: 4 additions & 4 deletions modules/router-store/spec/types/selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { compilerOptions } from './utils';
describe('router selectors', () => {
const expectSnippet = expecter(
(code) => `
import * as fromRouter from '@ngrx/router-store';
import { getRouterSelectors, RouterReducerState } from '@ngrx/router-store';
import { createSelector, createFeatureSelector } from '@ngrx/store';
export interface State {
router: fromRouter.RouterReducerState<any>;
router: RouterReducerState<any>;
}
export const selectRouter = createFeatureSelector<
State,
fromRouter.RouterReducerState<any>
RouterReducerState<any>
>('router');
export const {
Expand All @@ -25,7 +25,7 @@ describe('router selectors', () => {
selectRouteData,
selectUrl,
selectTitle,
} = fromRouter.getSelectors(selectRouter);
} = getRouterSelectors(selectRouter);
${code}
`,
Expand Down
6 changes: 5 additions & 1 deletion modules/router-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ export {
MinimalRouterStateSnapshot,
MinimalRouterStateSerializer,
} from './serializers/minimal_serializer';
export { getSelectors, createRouterSelector } from './router_selectors';
export {
getRouterSelectors,
getSelectors,
createRouterSelector,
} from './router_selectors';
export { provideRouterStore } from './provide_router_store';
8 changes: 7 additions & 1 deletion modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export function createRouterSelector<
return createFeatureSelector(DEFAULT_ROUTER_FEATURENAME);
}

export function getSelectors<V extends Record<string, any>>(
/**
* @deprecated This function is deprecated in favor of `getRouterSelectors`.
* For more info see: https://github.com/ngrx/platform/issues/3738
*/
export const getSelectors = getRouterSelectors;

export function getRouterSelectors<V extends Record<string, any>>(
selectState: (state: V) => RouterReducerState<any> = createRouterSelector<V>()
): RouterStateSelectors<V> {
const selectRouterState = createSelector(
Expand Down
13 changes: 8 additions & 5 deletions projects/example-app/src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
ActionReducerMap,
MetaReducer,
} from '@ngrx/store';
import * as fromRouter from '@ngrx/router-store';
import {
getRouterSelectors,
routerReducer,
RouterReducerState,
} from '@ngrx/router-store';

/**
* Every reducer module's default export is the reducer function itself. In
Expand All @@ -24,7 +28,7 @@ import { InjectionToken, isDevMode } from '@angular/core';
*/
export interface State {
[fromLayout.layoutFeatureKey]: fromLayout.State;
router: fromRouter.RouterReducerState<any>;
router: RouterReducerState<any>;
}

/**
Expand All @@ -37,7 +41,7 @@ export const ROOT_REDUCERS = new InjectionToken<
>('Root reducers token', {
factory: () => ({
[fromLayout.layoutFeatureKey]: fromLayout.reducer,
router: fromRouter.routerReducer,
router: routerReducer,
}),
});

Expand All @@ -60,7 +64,6 @@ export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
* the root meta-reducer. To add more meta-reducers, provide an array of meta-reducers
* that will be composed to form the root meta-reducer.
*/

export const metaReducers: MetaReducer<State>[] = isDevMode() ? [logger] : [];

/**
Expand All @@ -78,4 +81,4 @@ export const selectShowSidenav = createSelector(
/**
* Router Selectors
*/
export const { selectRouteData } = fromRouter.getSelectors();
export const { selectRouteData } = getRouterSelectors();
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// #docregion routerSelectors
import { getSelectors, RouterReducerState } from '@ngrx/router-store';
import { getRouterSelectors, RouterReducerState } from '@ngrx/router-store';

// `router` is used as the default feature name. You can use the feature name
// of your choice by creating a feature selector and pass it to the `getSelectors` function
// of your choice by creating a feature selector and pass it to the `getRouterSelectors` function
// export const selectRouter = createFeatureSelector<RouterReducerState>('yourFeatureName');

export const {
Expand All @@ -16,5 +16,5 @@ export const {
selectRouteDataParam, // factory function to select a route data param
selectUrl, // select the current url
selectTitle, // select the title if available
} = getSelectors();
} = getRouterSelectors();
// #enddocregion routerSelectors
23 changes: 23 additions & 0 deletions projects/ngrx.io/content/guide/migration/v15.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,26 @@ export class TestComponent {
}
```

## Deprecations

### @ngrx/router-store

#### Renamed `getSelectors` Function (Introduced in v15.2)

The `getSelectors` function is deprecated in favor of `getRouterSelectors`.

BEFORE:

```ts
import { getSelectors } from '@ngrx/router-store';

const routerSelectors = getSelectors();
```

AFTER:

```ts
import { getRouterSelectors } from '@ngrx/router-store';

const routerSelectors = getRouterSelectors();
```
8 changes: 4 additions & 4 deletions projects/ngrx.io/content/guide/router-store/selectors.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Router selectors

The `getSelectors` method supplied within `@ngrx/router-store` provides functions for selecting common information from the router state.
The `getRouterSelectors` method supplied within `@ngrx/router-store` provides functions for selecting common information from the router state.

The default behavior of `getSelectors` selects the router state for the `router` state key.
If the default router state config is overwritten with a different router state key, the `getSelectors` method takes a selector function to select the piece of state where the router state is being stored.
The default behavior of `getRouterSelectors` selects the router state for the `router` state key.
If the default router state config is overwritten with a different router state key, the `getRouterSelectors` method takes a selector function to select the piece of state where the router state is being stored.
The example below shows how to provide a selector for the top level `router` key in your state object.

**Note:** The `getSelectors` method works with the `routerReducer` provided by `@ngrx/router-store`. If you use a [custom serializer](guide/router-store/configuration#custom-router-state-serializer), you'll need to provide your own selectors.
**Note:** The `getRouterSelectors` method works with the `routerReducer` provided by `@ngrx/router-store`. If you use a [custom serializer](guide/router-store/configuration#custom-router-state-serializer), you'll need to provide your own selectors.

Usage:

Expand Down

0 comments on commit 7ad76b8

Please sign in to comment.