From b4e71d745bb91df6edad6094bf93da76c462fdf6 Mon Sep 17 00:00:00 2001 From: kouMatsumoto Date: Mon, 27 Aug 2018 23:08:48 +0900 Subject: [PATCH 1/2] refactor(router-store): refactor router_store_module into separate files --- modules/router-store/spec/integration.spec.ts | 22 ++- modules/router-store/src/actions.ts | 138 ++++++++++++++ modules/router-store/src/index.ts | 20 +-- modules/router-store/src/reducer.ts | 36 ++++ .../router-store/src/router_store_module.ts | 168 ++---------------- modules/store-devtools/spec/store.spec.ts | 12 +- modules/store-devtools/src/reducer.ts | 26 +-- 7 files changed, 235 insertions(+), 187 deletions(-) create mode 100644 modules/router-store/src/actions.ts create mode 100644 modules/router-store/src/reducer.ts 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); }); diff --git a/modules/store-devtools/spec/store.spec.ts b/modules/store-devtools/spec/store.spec.ts index 778bf8af11..1e005c0609 100644 --- a/modules/store-devtools/spec/store.spec.ts +++ b/modules/store-devtools/spec/store.spec.ts @@ -709,7 +709,9 @@ describe('Store Devtools', () => { it('should add pause action', () => { const liftedState = fixture.getLiftedState(); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); + expect(liftedState.actionsById[3].action.type).toEqual( + '@ngrx/devtools/pause' + ); }); it('should overwrite last state during pause but keep action', () => { @@ -719,7 +721,9 @@ describe('Store Devtools', () => { expect(liftedState.computedStates.length).toBe(4); expect(fixture.getState()).toEqual(1); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); + expect(liftedState.actionsById[3].action.type).toEqual( + '@ngrx/devtools/pause' + ); }); it('recomputation of states should preserve last state', () => { @@ -739,7 +743,9 @@ describe('Store Devtools', () => { expect(fixture.getState()).toBe(2); const liftedState = fixture.getLiftedState(); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); + expect(liftedState.actionsById[3].action.type).toEqual( + '@ngrx/devtools/pause' + ); expect(oldComputedStates).not.toBe(liftedState.computedStates); }); }); diff --git a/modules/store-devtools/src/reducer.ts b/modules/store-devtools/src/reducer.ts index 84aa41599e..97dc22a67e 100644 --- a/modules/store-devtools/src/reducer.ts +++ b/modules/store-devtools/src/reducer.ts @@ -123,12 +123,12 @@ function recomputeStates( const entry: ComputedState = shouldSkip ? previousEntry : computeNextEntry( - reducer, - action, - previousState, - previousError, - errorHandler - ); + reducer, + action, + previousState, + previousError, + errorHandler + ); nextComputedStates.push(entry); } @@ -250,9 +250,12 @@ export function liftReducerWith( // The corresponding state will be overwritten on each update to always contain // the latest state (see Actions.PERFORM_ACTION). stagedActionIds = [...stagedActionIds, nextActionId]; - actionsById[nextActionId] = new PerformAction({ - type: '@ngrx/devtools/pause', - }, +Date.now()); + actionsById[nextActionId] = new PerformAction( + { + type: '@ngrx/devtools/pause', + }, + +Date.now() + ); nextActionId++; minInvalidatedStateIndex = stagedActionIds.length - 1; computedStates = computedStates.concat( @@ -474,7 +477,10 @@ export function liftReducerWith( // Add a new action to only recompute state const actionId = nextActionId++; - actionsById[actionId] = new PerformAction(liftedAction, +Date.now()); + actionsById[actionId] = new PerformAction( + liftedAction, + +Date.now() + ); stagedActionIds = [...stagedActionIds, actionId]; minInvalidatedStateIndex = stagedActionIds.length - 1; From b13e48ec741cbd4fd8fdd666bdab6d89a34600fb Mon Sep 17 00:00:00 2001 From: kouMatsumoto Date: Tue, 28 Aug 2018 00:27:53 +0900 Subject: [PATCH 2/2] style: revert indent for irrelevant files --- modules/store-devtools/spec/store.spec.ts | 12 +++-------- modules/store-devtools/src/reducer.ts | 26 +++++++++-------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/modules/store-devtools/spec/store.spec.ts b/modules/store-devtools/spec/store.spec.ts index 1e005c0609..778bf8af11 100644 --- a/modules/store-devtools/spec/store.spec.ts +++ b/modules/store-devtools/spec/store.spec.ts @@ -709,9 +709,7 @@ describe('Store Devtools', () => { it('should add pause action', () => { const liftedState = fixture.getLiftedState(); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual( - '@ngrx/devtools/pause' - ); + expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); }); it('should overwrite last state during pause but keep action', () => { @@ -721,9 +719,7 @@ describe('Store Devtools', () => { expect(liftedState.computedStates.length).toBe(4); expect(fixture.getState()).toEqual(1); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual( - '@ngrx/devtools/pause' - ); + expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); }); it('recomputation of states should preserve last state', () => { @@ -743,9 +739,7 @@ describe('Store Devtools', () => { expect(fixture.getState()).toBe(2); const liftedState = fixture.getLiftedState(); expect(liftedState.nextActionId).toBe(4); - expect(liftedState.actionsById[3].action.type).toEqual( - '@ngrx/devtools/pause' - ); + expect(liftedState.actionsById[3].action.type).toEqual('@ngrx/devtools/pause'); expect(oldComputedStates).not.toBe(liftedState.computedStates); }); }); diff --git a/modules/store-devtools/src/reducer.ts b/modules/store-devtools/src/reducer.ts index 97dc22a67e..84aa41599e 100644 --- a/modules/store-devtools/src/reducer.ts +++ b/modules/store-devtools/src/reducer.ts @@ -123,12 +123,12 @@ function recomputeStates( const entry: ComputedState = shouldSkip ? previousEntry : computeNextEntry( - reducer, - action, - previousState, - previousError, - errorHandler - ); + reducer, + action, + previousState, + previousError, + errorHandler + ); nextComputedStates.push(entry); } @@ -250,12 +250,9 @@ export function liftReducerWith( // The corresponding state will be overwritten on each update to always contain // the latest state (see Actions.PERFORM_ACTION). stagedActionIds = [...stagedActionIds, nextActionId]; - actionsById[nextActionId] = new PerformAction( - { - type: '@ngrx/devtools/pause', - }, - +Date.now() - ); + actionsById[nextActionId] = new PerformAction({ + type: '@ngrx/devtools/pause', + }, +Date.now()); nextActionId++; minInvalidatedStateIndex = stagedActionIds.length - 1; computedStates = computedStates.concat( @@ -477,10 +474,7 @@ export function liftReducerWith( // Add a new action to only recompute state const actionId = nextActionId++; - actionsById[actionId] = new PerformAction( - liftedAction, - +Date.now() - ); + actionsById[actionId] = new PerformAction(liftedAction, +Date.now()); stagedActionIds = [...stagedActionIds, actionId]; minInvalidatedStateIndex = stagedActionIds.length - 1;