diff --git a/modules/router-store/spec/integration.spec.ts b/modules/router-store/spec/integration.spec.ts index 476fbf2580..c3a248bde6 100644 --- a/modules/router-store/spec/integration.spec.ts +++ b/modules/router-store/spec/integration.spec.ts @@ -12,21 +12,19 @@ import { Store, StoreModule, ScannedActionsSubject } from '@ngrx/store'; import { filter, first, mapTo, take } from 'rxjs/operators'; import { + NavigationActionTiming, ROUTER_CANCEL, ROUTER_ERROR, + ROUTER_NAVIGATED, ROUTER_NAVIGATION, + ROUTER_REQUEST, RouterAction, routerReducer, + RouterReducerState, RouterStateSerializer, + StoreRouterConfig, StoreRouterConnectingModule, } from '../src'; -import { - StoreRouterConfig, - ROUTER_REQUEST, - ROUTER_NAVIGATED, - NavigationActionTiming, - RouterReducerState, -} from '../src/router_store_module'; describe('integration spec', () => { it('should work', (done: any) => { @@ -628,7 +626,10 @@ describe('integration spec', () => { reducers: { routerReducer }, canActivate: () => { store.dispatch({ type: 'USER_EVENT' }); - return store.pipe(take(1), mapTo(true)); + return store.pipe( + take(1), + mapTo(true) + ); }, }); @@ -882,7 +883,10 @@ function createTestModule( function waitForNavigation(router: Router, event: any = NavigationEnd) { return router.events - .pipe(filter(e => e instanceof event), first()) + .pipe( + filter(e => e instanceof event), + first() + ) .toPromise(); } diff --git a/modules/router-store/src/actions.ts b/modules/router-store/src/actions.ts new file mode 100644 index 0000000000..3712d18e78 --- /dev/null +++ b/modules/router-store/src/actions.ts @@ -0,0 +1,138 @@ +import { + NavigationCancel, + NavigationEnd, + NavigationError, + NavigationStart, + RoutesRecognized, +} from '@angular/router'; + +import { + BaseRouterStoreState, + SerializedRouterStateSnapshot, +} from './serializer'; + +/** + * An action dispatched when a router navigation request is fired. + */ +export const ROUTER_REQUEST = 'ROUTER_REQUEST'; + +/** + * Payload of ROUTER_REQUEST + */ +export type RouterRequestPayload = { + event: NavigationStart; +}; + +/** + * An action dispatched when a router navigation request is fired. + */ +export type RouterRequestAction = { + type: typeof ROUTER_REQUEST; + payload: RouterRequestPayload; +}; + +/** + * An action dispatched when the router navigates. + */ +export const ROUTER_NAVIGATION = 'ROUTER_NAVIGATION'; + +/** + * Payload of ROUTER_NAVIGATION. + */ +export type RouterNavigationPayload = { + routerState: T; + event: RoutesRecognized; +}; + +/** + * An action dispatched when the router navigates. + */ +export type RouterNavigationAction< + T extends BaseRouterStoreState = SerializedRouterStateSnapshot +> = { + type: typeof ROUTER_NAVIGATION; + payload: RouterNavigationPayload; +}; + +/** + * An action dispatched when the router cancels navigation. + */ +export const ROUTER_CANCEL = 'ROUTER_CANCEL'; + +/** + * Payload of ROUTER_CANCEL. + */ +export type RouterCancelPayload = { + routerState: V; + storeState: T; + event: NavigationCancel; +}; + +/** + * An action dispatched when the router cancel navigation. + */ +export type RouterCancelAction< + T, + V extends BaseRouterStoreState = SerializedRouterStateSnapshot +> = { + type: typeof ROUTER_CANCEL; + payload: RouterCancelPayload; +}; + +/** + * An action dispatched when the router errors. + */ +export const ROUTER_ERROR = 'ROUTE_ERROR'; + +/** + * Payload of ROUTER_ERROR. + */ +export type RouterErrorPayload = { + routerState: V; + storeState: T; + event: NavigationError; +}; + +/** + * An action dispatched when the router errors. + */ +export type RouterErrorAction< + T, + V extends BaseRouterStoreState = SerializedRouterStateSnapshot +> = { + type: typeof ROUTER_ERROR; + payload: RouterErrorPayload; +}; + +/** + * An action dispatched after navigation has ended and new route is active. + */ +export const ROUTER_NAVIGATED = 'ROUTER_NAVIGATED'; + +/** + * Payload of ROUTER_NAVIGATED. + */ +export type RouterNavigatedPayload = { + event: NavigationEnd; +}; + +/** + * An action dispatched after navigation has ended and new route is active. + */ +export type RouterNavigatedAction = { + type: typeof ROUTER_NAVIGATED; + payload: RouterNavigatedPayload; +}; + +/** + * An union type of router actions. + */ +export type RouterAction< + T, + V extends BaseRouterStoreState = SerializedRouterStateSnapshot +> = + | RouterRequestAction + | RouterNavigationAction + | RouterCancelAction + | RouterErrorAction + | RouterNavigatedAction; diff --git a/modules/router-store/src/index.ts b/modules/router-store/src/index.ts index ff87689a78..b57b0c59b7 100644 --- a/modules/router-store/src/index.ts +++ b/modules/router-store/src/index.ts @@ -2,28 +2,28 @@ export { ROUTER_ERROR, ROUTER_CANCEL, ROUTER_NAVIGATION, - ROUTER_REQUEST, ROUTER_NAVIGATED, - RouterNavigationAction, + ROUTER_REQUEST, + RouterAction, RouterCancelAction, + RouterCancelPayload, RouterErrorAction, - RouterRequestAction, - RouterNavigatedAction, - RouterAction, - routerReducer, RouterErrorPayload, - RouterReducerState, - RouterCancelPayload, + RouterNavigatedAction, + RouterNavigatedPayload, + RouterNavigationAction, RouterNavigationPayload, + RouterRequestAction, RouterRequestPayload, - RouterNavigatedPayload, +} from './actions'; +export { routerReducer, RouterReducerState } from './reducer'; +export { StoreRouterConnectingModule, StoreRouterConfig, NavigationActionTiming, ROUTER_CONFIG, DEFAULT_ROUTER_FEATURENAME, } from './router_store_module'; - export { RouterStateSerializer, DefaultRouterStateSerializer, diff --git a/modules/router-store/src/reducer.ts b/modules/router-store/src/reducer.ts new file mode 100644 index 0000000000..53d59fd9f0 --- /dev/null +++ b/modules/router-store/src/reducer.ts @@ -0,0 +1,36 @@ +import { + ROUTER_CANCEL, + ROUTER_ERROR, + ROUTER_NAVIGATION, + RouterAction, +} from './actions'; +import { + BaseRouterStoreState, + SerializedRouterStateSnapshot, +} from './serializer'; + +export type RouterReducerState< + T extends BaseRouterStoreState = SerializedRouterStateSnapshot +> = { + state: T; + navigationId: number; +}; + +export function routerReducer< + T extends BaseRouterStoreState = SerializedRouterStateSnapshot +>( + state: RouterReducerState | undefined, + action: RouterAction +): RouterReducerState { + switch (action.type) { + case ROUTER_NAVIGATION: + case ROUTER_ERROR: + case ROUTER_CANCEL: + return { + state: action.payload.routerState, + navigationId: action.payload.event.id, + }; + default: + return state as RouterReducerState; + } +} diff --git a/modules/router-store/src/router_store_module.ts b/modules/router-store/src/router_store_module.ts index 17cc2b1ae3..91242234ad 100644 --- a/modules/router-store/src/router_store_module.ts +++ b/modules/router-store/src/router_store_module.ts @@ -14,166 +14,21 @@ import { NavigationStart, } from '@angular/router'; import { select, Store } from '@ngrx/store'; +import { withLatestFrom } from 'rxjs/operators'; +import { + ROUTER_CANCEL, + ROUTER_ERROR, + ROUTER_NAVIGATED, + ROUTER_NAVIGATION, + ROUTER_REQUEST, +} from './actions'; +import { RouterReducerState } from './reducer'; import { DefaultRouterStateSerializer, RouterStateSerializer, SerializedRouterStateSnapshot, - BaseRouterStoreState, } from './serializer'; -import { withLatestFrom } from 'rxjs/operators'; - -/** - * An action dispatched when a router navigation request is fired. - */ -export const ROUTER_REQUEST = 'ROUTER_REQUEST'; - -/** - * Payload of ROUTER_REQUEST - */ -export type RouterRequestPayload = { - event: NavigationStart; -}; - -/** - * An action dispatched when a router navigation request is fired. - */ -export type RouterRequestAction = { - type: typeof ROUTER_REQUEST; - payload: RouterRequestPayload; -}; - -/** - * An action dispatched when the router navigates. - */ -export const ROUTER_NAVIGATION = 'ROUTER_NAVIGATION'; - -/** - * Payload of ROUTER_NAVIGATION. - */ -export type RouterNavigationPayload = { - routerState: T; - event: RoutesRecognized; -}; - -/** - * An action dispatched when the router navigates. - */ -export type RouterNavigationAction< - T extends BaseRouterStoreState = SerializedRouterStateSnapshot -> = { - type: typeof ROUTER_NAVIGATION; - payload: RouterNavigationPayload; -}; - -/** - * An action dispatched when the router cancels navigation. - */ -export const ROUTER_CANCEL = 'ROUTER_CANCEL'; - -/** - * Payload of ROUTER_CANCEL. - */ -export type RouterCancelPayload = { - routerState: V; - storeState: T; - event: NavigationCancel; -}; - -/** - * An action dispatched when the router cancel navigation. - */ -export type RouterCancelAction< - T, - V extends BaseRouterStoreState = SerializedRouterStateSnapshot -> = { - type: typeof ROUTER_CANCEL; - payload: RouterCancelPayload; -}; - -/** - * An action dispatched when the router errors. - */ -export const ROUTER_ERROR = 'ROUTE_ERROR'; - -/** - * Payload of ROUTER_ERROR. - */ -export type RouterErrorPayload = { - routerState: V; - storeState: T; - event: NavigationError; -}; - -/** - * An action dispatched when the router errors. - */ -export type RouterErrorAction< - T, - V extends BaseRouterStoreState = SerializedRouterStateSnapshot -> = { - type: typeof ROUTER_ERROR; - payload: RouterErrorPayload; -}; - -/** - * An action dispatched after navigation has ended and new route is active. - */ -export const ROUTER_NAVIGATED = 'ROUTER_NAVIGATED'; - -/** - * Payload of ROUTER_NAVIGATED. - */ -export type RouterNavigatedPayload = { - event: NavigationEnd; -}; - -/** - * An action dispatched after navigation has ended and new route is active. - */ -export type RouterNavigatedAction = { - type: typeof ROUTER_NAVIGATED; - payload: RouterNavigatedPayload; -}; - -/** - * An union type of router actions. - */ -export type RouterAction< - T, - V extends BaseRouterStoreState = SerializedRouterStateSnapshot -> = - | RouterRequestAction - | RouterNavigationAction - | RouterCancelAction - | RouterErrorAction - | RouterNavigatedAction; - -export type RouterReducerState< - T extends BaseRouterStoreState = SerializedRouterStateSnapshot -> = { - state: T; - navigationId: number; -}; - -export function routerReducer< - T extends BaseRouterStoreState = SerializedRouterStateSnapshot ->( - state: RouterReducerState | undefined, - action: RouterAction -): RouterReducerState { - switch (action.type) { - case ROUTER_NAVIGATION: - case ROUTER_ERROR: - case ROUTER_CANCEL: - return { - state: action.payload.routerState, - navigationId: action.payload.event.id, - }; - default: - return state as RouterReducerState; - } -} export interface StoreRouterConfig { stateKey?: string; @@ -314,7 +169,10 @@ export class StoreRouterConnectingModule { private setUpStoreStateListener(): void { this.store - .pipe(select(this.stateKey), withLatestFrom(this.store)) + .pipe( + select(this.stateKey), + withLatestFrom(this.store) + ) .subscribe(([routerStoreState, storeState]) => { this.navigateIfNeeded(routerStoreState, storeState); });