1
+ var invariant = require ( 'react/lib/invariant' ) ;
1
2
var EventEmitter = require ( 'events' ) . EventEmitter ;
2
3
var LocationActions = require ( '../actions/LocationActions' ) ;
3
- var LocationDispatcher = require ( '../dispatchers/LocationDispatcher' ) ;
4
4
5
5
var CHANGE_EVENT = 'change' ;
6
6
var _events = new EventEmitter ;
@@ -9,7 +9,15 @@ function notifyChange() {
9
9
_events . emit ( CHANGE_EVENT ) ;
10
10
}
11
11
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
+ }
13
21
14
22
/**
15
23
* The PathStore keeps track of the current URL path.
@@ -28,6 +36,26 @@ var PathStore = {
28
36
_events . removeAllListeners ( CHANGE_EVENT ) ;
29
37
} ,
30
38
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
+
31
59
/**
32
60
* Returns the current URL path.
33
61
*/
@@ -40,24 +68,7 @@ var PathStore = {
40
68
*/
41
69
getCurrentActionType : function ( ) {
42
70
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
+ }
61
72
62
73
} ;
63
74
0 commit comments