-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.service.ts
130 lines (116 loc) · 3.55 KB
/
router.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
GuardsCheckEnd,
NavigationCancel,
NavigationEnd,
NavigationError,
ResolveEnd,
Router,
RoutesRecognized,
} from '@angular/router';
import { send } from '@ngneat/elf-devtools';
import { RouterStateService } from './router-state.service';
import { RouterRepository, RouterState } from './router.repository';
@Injectable({ providedIn: 'root' })
export class RouterService {
private lastRouterState: RouterState;
private navigationTriggeredByDispatch = false;
private dispatchTriggeredByRouter = false;
constructor(
private routerRepository: RouterRepository,
private routerStateService: RouterStateService,
private router: Router
) {}
init() {
this.setUpStoreListener();
this.setUpStateUpdateEvents();
}
dispatchRouterCancel(event: NavigationCancel) {
send({ type: '[Router] Navigation Cancelled' });
this.update({ navigationId: event.id });
this.routerStateService.navigationCancel.next(event);
}
dispatchRouterError(event: NavigationError) {
send({ type: '[Router] Navigation Error' });
this.update({ navigationId: event.id });
this.routerStateService.navigationError.next(event);
}
dispatchRouterSuccess() {
send({ type: '[Router] Navigation Succeeded' });
this.update(this.lastRouterState);
}
private setUpStateUpdateEvents(): void {
this.router.events.subscribe((e) => {
if (
e instanceof RoutesRecognized ||
e instanceof GuardsCheckEnd ||
e instanceof ResolveEnd
) {
this.lastRouterState = this.serializeRoute(e);
}
else if (e instanceof NavigationCancel) {
this.dispatchRouterCancel(e);
}
else if (e instanceof NavigationError) {
this.dispatchRouterError(e);
}
else if (
e instanceof NavigationEnd &&
!this.navigationTriggeredByDispatch
) {
this.dispatchRouterSuccess();
}
});
}
private setUpStoreListener(): void {
this.routerRepository
.select((state) => state)
.subscribe((s) => {
this.lastRouterState = s;
this.navigateIfNeeded();
});
}
private update(routerState: Partial<RouterState>) {
this.dispatchTriggeredByRouter = true;
this.routerRepository.update(routerState);
this.dispatchTriggeredByRouter = false;
this.navigationTriggeredByDispatch = false;
}
private serializeRoute(
navigationEvent: RoutesRecognized | GuardsCheckEnd | ResolveEnd
): RouterState {
let state: ActivatedRouteSnapshot = navigationEvent.state.root;
while (state.firstChild) {
state = state.firstChild;
}
const { params, data, queryParams, fragment } = state;
return {
navigationId: navigationEvent.id,
state: {
url: navigationEvent.url,
urlAfterRedirects: navigationEvent.urlAfterRedirects,
params,
queryParams,
fragment,
data,
navigationExtras: this.router.getCurrentNavigation()?.extras
? this.router.getCurrentNavigation()?.extras.state
: {},
},
};
}
private navigateIfNeeded(): void {
if (
!this.lastRouterState
|| !this.lastRouterState.state
|| this.dispatchTriggeredByRouter
) {
return;
}
if (this.router.url !== this.lastRouterState.state.url) {
this.navigationTriggeredByDispatch = true;
this.router.navigateByUrl(this.lastRouterState.state.url);
}
}
}