1
1
import 'rxjs/add/operator/filter' ;
2
2
import 'rxjs/add/operator/map' ;
3
+ import 'rxjs/add/operator/distinctUntilChanged' ;
3
4
import { Injectable , ApplicationRef } from '@angular/core' ;
4
5
import { Location } from '@angular/common' ;
5
- import { Router , NavigationEnd , NavigationCancel , DefaultUrlSerializer } from '@angular/router' ;
6
+ import {
7
+ Router ,
8
+ NavigationEnd ,
9
+ NavigationCancel ,
10
+ DefaultUrlSerializer
11
+ } from '@angular/router' ;
6
12
import { NgRedux } from '@angular-redux/store' ;
7
13
import { Observable } from 'rxjs/Observable' ;
8
- import { ISubscription } from 'rxjs/Subscription'
14
+ import { ISubscription } from 'rxjs/Subscription' ;
9
15
import { UPDATE_LOCATION } from './actions' ;
10
- import {
11
- RouterAction ,
12
- DefaultRouterState
13
- } from './reducer' ;
16
+ import { RouterAction , DefaultRouterState } from './reducer' ;
14
17
15
18
@Injectable ( )
16
19
export class NgReduxRouter {
17
20
private initialized = false ;
18
21
private currentLocation : string ;
19
22
private initialLocation : string ;
20
23
21
- private selectLocationFromState : ( state : any ) => string = ( state ) => state . router ;
24
+ private selectLocationFromState : ( state : any ) => string = state =>
25
+ state . router ;
22
26
private urlState : Observable < string > ;
23
27
24
28
private urlStateSubscription : ISubscription ;
@@ -64,14 +68,16 @@ export class NgReduxRouter {
64
68
* you can supply this argument as an Observable of the current url state.
65
69
*/
66
70
initialize (
67
- selectLocationFromState : ( state : any ) => string = ( state ) => state . router ,
71
+ selectLocationFromState : ( state : any ) => string = state => state . router ,
68
72
urlState$ : Observable < string > | undefined = undefined
69
73
) {
70
74
if ( this . initialized ) {
71
- throw new Error ( '@angular-redux/router already initialized! If you meant to re-initialize, call destroy first.' ) ;
75
+ throw new Error (
76
+ '@angular-redux/router already initialized! If you meant to re-initialize, call destroy first.'
77
+ ) ;
72
78
}
73
79
74
- this . selectLocationFromState = selectLocationFromState
80
+ this . selectLocationFromState = selectLocationFromState ;
75
81
76
82
this . urlState = urlState$ || this . getDefaultUrlStateObservable ( ) ;
77
83
@@ -82,19 +88,21 @@ export class NgReduxRouter {
82
88
83
89
private getDefaultUrlStateObservable ( ) {
84
90
return this . router . events
85
- . filter ( event => event instanceof NavigationEnd )
86
- . map ( event => this . location . path ( ) )
87
- . distinctUntilChanged ( )
91
+ . filter ( event => event instanceof NavigationEnd )
92
+ . map ( event => this . location . path ( ) )
93
+ . distinctUntilChanged ( ) ;
88
94
}
89
95
90
96
private getLocationFromStore ( useInitial : boolean = false ) {
91
- return this . selectLocationFromState ( this . ngRedux . getState ( ) ) ||
92
- ( useInitial ? this . initialLocation : '' ) ;
97
+ return (
98
+ this . selectLocationFromState ( this . ngRedux . getState ( ) ) ||
99
+ ( useInitial ? this . initialLocation : '' )
100
+ ) ;
93
101
}
94
102
95
103
private listenToRouterChanges ( ) {
96
104
const handleLocationChange = ( location : string ) => {
97
- if ( this . currentLocation === location ) {
105
+ if ( this . currentLocation === location ) {
98
106
// Dont dispatch changes if we haven't changed location.
99
107
return ;
100
108
}
@@ -107,7 +115,7 @@ export class NgReduxRouter {
107
115
// we dont dispath an event if the current url equals
108
116
// the initial url.
109
117
let locationFromStore = this . getLocationFromStore ( ) ;
110
- if ( locationFromStore === this . currentLocation ) {
118
+ if ( locationFromStore === this . currentLocation ) {
111
119
return ;
112
120
}
113
121
}
@@ -116,7 +124,7 @@ export class NgReduxRouter {
116
124
type : UPDATE_LOCATION ,
117
125
payload : location
118
126
} ) ;
119
- }
127
+ } ;
120
128
121
129
this . urlStateSubscription = this . urlState . subscribe ( handleLocationChange ) ;
122
130
}
@@ -134,9 +142,9 @@ export class NgReduxRouter {
134
142
return ;
135
143
}
136
144
137
- this . currentLocation = location
145
+ this . currentLocation = location ;
138
146
this . router . navigateByUrl ( location ) ;
139
- }
147
+ } ;
140
148
141
149
this . reduxSubscription = this . ngRedux
142
150
. select ( state => this . selectLocationFromState ( state ) )
0 commit comments