1+ var invariant = require ( 'react/lib/invariant' ) ;
12var EventEmitter = require ( 'events' ) . EventEmitter ;
23var LocationActions = require ( '../actions/LocationActions' ) ;
3- var LocationDispatcher = require ( '../dispatchers/LocationDispatcher' ) ;
44
55var CHANGE_EVENT = 'change' ;
66var _events = new EventEmitter ;
@@ -9,7 +9,15 @@ function notifyChange() {
99 _events . emit ( CHANGE_EVENT ) ;
1010}
1111
12- var _currentPath , _currentActionType ;
12+ var _currentLocation , _currentPath , _currentActionType ;
13+
14+ function handleLocationChangeAction ( action ) {
15+ if ( _currentPath !== action . path ) {
16+ _currentPath = action . path ;
17+ _currentActionType = action . type ;
18+ notifyChange ( ) ;
19+ }
20+ }
1321
1422/**
1523 * The PathStore keeps track of the current URL path.
@@ -28,6 +36,26 @@ var PathStore = {
2836 _events . removeAllListeners ( CHANGE_EVENT ) ;
2937 } ,
3038
39+ /**
40+ * Setup the PathStore to use the given location.
41+ */
42+ useLocation : function ( location ) {
43+ invariant (
44+ _currentLocation == null || _currentLocation === location ,
45+ 'You cannot use %s and %s on the same page' ,
46+ _currentLocation , location
47+ ) ;
48+
49+ if ( _currentLocation !== location ) {
50+ if ( location . setup )
51+ location . setup ( handleLocationChangeAction ) ;
52+
53+ _currentPath = location . getCurrentPath ( ) ;
54+ }
55+
56+ _currentLocation = location ;
57+ } ,
58+
3159 /**
3260 * Returns the current URL path.
3361 */
@@ -40,24 +68,7 @@ var PathStore = {
4068 */
4169 getCurrentActionType : function ( ) {
4270 return _currentActionType ;
43- } ,
44-
45- dispatchToken : LocationDispatcher . register ( function ( payload ) {
46- var action = payload . action ;
47-
48- switch ( action . type ) {
49- case LocationActions . SETUP :
50- case LocationActions . PUSH :
51- case LocationActions . REPLACE :
52- case LocationActions . POP :
53- if ( _currentPath !== action . path ) {
54- _currentPath = action . path ;
55- _currentActionType = action . type ;
56- notifyChange ( ) ;
57- }
58- break ;
59- }
60- } )
71+ }
6172
6273} ;
6374
0 commit comments