1
- const deepEqual = require ( 'deep-equal' ) ;
1
+ const deepEqual = require ( 'deep-equal' )
2
2
3
3
// Constants
4
4
5
- const INIT_PATH = " @@router/INIT_PATH" ;
6
- const UPDATE_PATH = " @@router/UPDATE_PATH" ;
7
- const SELECT_STATE = state => state . routing ;
5
+ const INIT_PATH = ' @@router/INIT_PATH'
6
+ const UPDATE_PATH = ' @@router/UPDATE_PATH'
7
+ const SELECT_STATE = state => state . routing
8
8
9
9
// Action creators
10
10
@@ -17,7 +17,7 @@ function initPath(path, state) {
17
17
replace : false ,
18
18
avoidRouterUpdate : true
19
19
}
20
- } ;
20
+ }
21
21
}
22
22
23
23
function pushPath ( path , state , { avoidRouterUpdate = false } = { } ) {
@@ -29,7 +29,7 @@ function pushPath(path, state, { avoidRouterUpdate = false } = {}) {
29
29
replace : false ,
30
30
avoidRouterUpdate : ! ! avoidRouterUpdate
31
31
}
32
- } ;
32
+ }
33
33
}
34
34
35
35
function replacePath ( path , state , { avoidRouterUpdate = false } = { } ) {
@@ -51,7 +51,7 @@ let initialState = {
51
51
path : undefined ,
52
52
state : undefined ,
53
53
replace : false
54
- } ;
54
+ }
55
55
56
56
function update ( state = initialState , { type, payload } ) {
57
57
if ( type === INIT_PATH || type === UPDATE_PATH ) {
@@ -60,19 +60,19 @@ function update(state=initialState, { type, payload }) {
60
60
changeId : state . changeId + ( payload . avoidRouterUpdate ? 0 : 1 ) ,
61
61
state : payload . state ,
62
62
replace : payload . replace
63
- } ) ;
63
+ } )
64
64
}
65
- return state ;
65
+ return state
66
66
}
67
67
68
68
// Syncing
69
69
70
70
function locationsAreEqual ( a , b ) {
71
- return a != null && b != null && a . path === b . path && deepEqual ( a . state , b . state ) ;
71
+ return a != null && b != null && a . path === b . path && deepEqual ( a . state , b . state )
72
72
}
73
73
74
74
function syncReduxAndRouter ( history , store , selectRouterState = SELECT_STATE ) {
75
- const getRouterState = ( ) => selectRouterState ( store . getState ( ) ) ;
75
+ const getRouterState = ( ) => selectRouterState ( store . getState ( ) )
76
76
77
77
// To properly handle store updates we need to track the last route.
78
78
// This route contains a `changeId` which is updated on every
@@ -81,20 +81,20 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
81
81
// check if the location has changed, and if it is we trigger a
82
82
// history update. It's possible for this to happen when something
83
83
// reloads the entire app state such as redux devtools.
84
- let lastRoute = undefined ;
84
+ let lastRoute = undefined
85
85
86
86
if ( ! getRouterState ( ) ) {
87
87
throw new Error (
88
- " Cannot sync router: route state does not exist. Did you " +
89
- " install the routing reducer?"
90
- ) ;
88
+ ' Cannot sync router: route state does not exist. Did you ' +
89
+ ' install the routing reducer?'
90
+ )
91
91
}
92
92
93
93
const unsubscribeHistory = history . listen ( location => {
94
94
const route = {
95
95
path : history . createPath ( location ) ,
96
96
state : location . state
97
- } ;
97
+ }
98
98
99
99
if ( ! lastRoute ) {
100
100
// `initialState` *should* represent the current location when
@@ -112,40 +112,40 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
112
112
path : route . path ,
113
113
state : route . state ,
114
114
replace : false
115
- } ;
115
+ }
116
116
117
117
// Also set `lastRoute` so that the store subscriber doesn't
118
118
// trigger an unnecessary `pushState` on load
119
- lastRoute = initialState ;
119
+ lastRoute = initialState
120
120
121
- store . dispatch ( initPath ( route . path , route . state ) ) ;
121
+ store . dispatch ( initPath ( route . path , route . state ) )
122
122
} else if ( ! locationsAreEqual ( getRouterState ( ) , route ) ) {
123
123
// The above check avoids dispatching an action if the store is
124
124
// already up-to-date
125
- const method = location . action === 'REPLACE' ? replacePath : pushPath ;
126
- store . dispatch ( method ( route . path , route . state , { avoidRouterUpdate : true } ) ) ;
125
+ const method = location . action === 'REPLACE' ? replacePath : pushPath
126
+ store . dispatch ( method ( route . path , route . state , { avoidRouterUpdate : true } ) )
127
127
}
128
- } ) ;
128
+ } )
129
129
130
130
const unsubscribeStore = store . subscribe ( ( ) => {
131
- let routing = getRouterState ( ) ;
131
+ let routing = getRouterState ( )
132
132
133
133
// Only trigger history update if this is a new change or the
134
134
// location has changed.
135
135
if ( lastRoute . changeId !== routing . changeId ||
136
136
! locationsAreEqual ( lastRoute , routing ) ) {
137
137
138
- lastRoute = routing ;
139
- const method = routing . replace ? 'replaceState' : 'pushState' ;
140
- history [ method ] ( routing . state , routing . path ) ;
138
+ lastRoute = routing
139
+ const method = routing . replace ? 'replaceState' : 'pushState'
140
+ history [ method ] ( routing . state , routing . path )
141
141
}
142
142
143
- } ) ;
143
+ } )
144
144
145
145
return function unsubscribe ( ) {
146
- unsubscribeHistory ( ) ;
147
- unsubscribeStore ( ) ;
148
- } ;
146
+ unsubscribeHistory ( )
147
+ unsubscribeStore ( )
148
+ }
149
149
}
150
150
151
151
module . exports = {
@@ -154,4 +154,4 @@ module.exports = {
154
154
replacePath,
155
155
syncReduxAndRouter,
156
156
routeReducer : update
157
- } ;
157
+ }
0 commit comments