Skip to content

Commit 9c694e4

Browse files
committed
refactor: extract PopstateNavigationStart type and type guard
1 parent 8370281 commit 9c694e4

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NavigationStart } from '@angular/router';
2+
import { NonNullish } from '../util-types/non-nullish';
3+
import { Override } from '../util-types/override';
4+
5+
/**
6+
* A `NavigationStart` event triggered by a `popstate` event.
7+
*/
8+
export type PopstateNavigationStart = Override<
9+
NavigationStart,
10+
{
11+
navigationTrigger: 'popstate';
12+
}
13+
> &
14+
NonNullish<Pick<NavigationStart, 'restoredState'>>;
15+
16+
export function isPopstateNavigationStart(
17+
event: NavigationStart
18+
): event is PopstateNavigationStart {
19+
return event.navigationTrigger === 'popstate';
20+
}

packages/router-component-store/src/lib/router-history-store/router-history.store.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NavigationEnd, NavigationStart, Router } from '@angular/router';
99
import { ComponentStore, provideComponentStore } from '@ngrx/component-store';
1010
import { filter, map, Observable, switchMap, take } from 'rxjs';
1111
import { filterRouterEvents } from '../filter-router-event.operator';
12+
import { isPopstateNavigationStart } from './popstate-navigation-start';
1213

1314
interface RouterHistoryState {
1415
/**
@@ -182,14 +183,8 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
182183
): RouterNavigatedSequence {
183184
let navigation = history[navigationId];
184185

185-
while (navigation[0].navigationTrigger === 'popstate') {
186-
navigation =
187-
history[
188-
// Navigation events triggered by `popstate` always have a
189-
// `restoredState`
190-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
191-
navigation[0].restoredState!.navigationId
192-
];
186+
while (isPopstateNavigationStart(navigation[0])) {
187+
navigation = history[navigation[0].restoredState.navigationId];
193188
}
194189

195190
return navigation;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Shallow removal of `null` and `undefined` from the types of all members of a shape.
3+
*
4+
* @example
5+
* interface LaxPerson {
6+
* name?: string;
7+
* address?: Address | null;
8+
* age: number | null;
9+
* }
10+
* type StrictPerson = NonNullish<LaxPerson>;
11+
* // -> interface {
12+
* // name: string;
13+
* // address: Address;
14+
* // age: number;
15+
* // }
16+
*
17+
* @license
18+
* Copyright 2022 Lars Gyrup Brink Nielsen
19+
*
20+
* Permission is hereby granted, free of charge, to any person obtaining a copy
21+
* of this software and associated documentation files (the "Software"), to deal
22+
* in the Software without restriction, including without limitation the rights
23+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
* copies of the Software, and to permit persons to whom the Software is
25+
* furnished to do so, subject to the following conditions:
26+
*
27+
* The above copyright notice and this permission notice shall be included in
28+
* all copies or substantial portions of the Software.
29+
*
30+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
* SOFTWARE.
37+
*/
38+
export type NonNullish<TShape> = {
39+
[TMember in keyof Required<TShape>]: NonNullable<TShape[TMember]>;
40+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Shallow override of one or more members of a shape.
3+
*
4+
* @example
5+
* interface LaxPerson {
6+
* name?: string;
7+
* address?: Address | null;
8+
* age: number | null;
9+
* }
10+
* type NamedPerson = Override<
11+
* LaxPerson,
12+
* {
13+
* name: string;
14+
* }
15+
* >;
16+
* // -> interface {
17+
* // name: string;
18+
* // address?: Address | null;
19+
* // age: number | null;
20+
* // }
21+
*
22+
* @license
23+
* Copyright 2022 Lars Gyrup Brink Nielsen
24+
*
25+
* Permission is hereby granted, free of charge, to any person obtaining a copy
26+
* of this software and associated documentation files (the "Software"), to deal
27+
* in the Software without restriction, including without limitation the rights
28+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29+
* copies of the Software, and to permit persons to whom the Software is
30+
* furnished to do so, subject to the following conditions:
31+
*
32+
* The above copyright notice and this permission notice shall be included in
33+
* all copies or substantial portions of the Software.
34+
*
35+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41+
* SOFTWARE.
42+
*/
43+
export type Override<TOriginal, TOverrides extends Partial<TOriginal>> = Omit<
44+
TOriginal,
45+
keyof TOverrides
46+
> &
47+
TOverrides;

0 commit comments

Comments
 (0)