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

refactor(router-store): refactor router_store_module into separate files #1299

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
22 changes: 13 additions & 9 deletions modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
);
},
});

Expand Down Expand Up @@ -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();
}

Expand Down
138 changes: 138 additions & 0 deletions modules/router-store/src/actions.ts
Original file line number Diff line number Diff line change
@@ -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<T extends BaseRouterStoreState> = {
routerState: T;
event: RoutesRecognized;
};

/**
* An action dispatched when the router navigates.
*/
export type RouterNavigationAction<
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
> = {
type: typeof ROUTER_NAVIGATION;
payload: RouterNavigationPayload<T>;
};

/**
* An action dispatched when the router cancels navigation.
*/
export const ROUTER_CANCEL = 'ROUTER_CANCEL';

/**
* Payload of ROUTER_CANCEL.
*/
export type RouterCancelPayload<T, V extends BaseRouterStoreState> = {
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<T, V>;
};

/**
* An action dispatched when the router errors.
*/
export const ROUTER_ERROR = 'ROUTE_ERROR';

/**
* Payload of ROUTER_ERROR.
*/
export type RouterErrorPayload<T, V extends BaseRouterStoreState> = {
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<T, V>;
};

/**
* 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<V>
| RouterCancelAction<T, V>
| RouterErrorAction<T, V>
| RouterNavigatedAction;
20 changes: 10 additions & 10 deletions modules/router-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions modules/router-store/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -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<T> | undefined,
action: RouterAction<any, T>
): RouterReducerState<T> {
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<T>;
}
}
Loading