1
- " use strict" ;
1
+ ' use strict' ;
2
2
3
3
var Environment = require ( './Environment' ) ;
4
4
@@ -7,31 +7,42 @@ var Environment = require('./Environment');
7
7
*/
8
8
function LocalStorageKeyEnvironment ( key ) {
9
9
this . key = key ;
10
- this . onStorage = this . onStorage . bind ( this ) ;
10
+ var store = this . onStorage = this . onStorage . bind ( this ) ;
11
+
12
+ this . storage = ( typeof window !== 'undefined' && window . localStorage ) || {
13
+ data : { } ,
14
+ getItem : function ( key ) { return this . data [ key ] ; } ,
15
+ setItem : function ( key , val ) { this . data [ key ] = val ; store ( ) ; }
16
+ } ;
17
+
11
18
Environment . call ( this ) ;
12
19
}
13
20
14
21
LocalStorageKeyEnvironment . prototype = Object . create ( Environment . prototype ) ;
15
22
LocalStorageKeyEnvironment . prototype . constructor = LocalStorageKeyEnvironment ;
16
23
17
24
LocalStorageKeyEnvironment . prototype . getPath = function ( ) {
18
- return window . localStorage . getItem ( this . key ) || '' ;
25
+ return this . storage . getItem ( this . key ) || '' ;
19
26
} ;
20
27
21
28
LocalStorageKeyEnvironment . prototype . pushState = function ( path , navigation ) {
22
- window . localStorage . setItem ( this . key , path ) ;
29
+ this . storage . setItem ( this . key , path ) ;
23
30
} ;
24
31
25
32
LocalStorageKeyEnvironment . prototype . replaceState = function ( path , navigation ) {
26
- window . localStorage . setItem ( this . key , path ) ;
33
+ this . storage . setItem ( this . key , path ) ;
27
34
} ;
28
35
29
36
LocalStorageKeyEnvironment . prototype . start = function ( ) {
30
- window . addEventListener ( 'storage' , this . onStorage , false ) ;
37
+ if ( typeof window !== 'undefined' && window . addEventListener ) {
38
+ window . addEventListener ( 'storage' , this . onStorage , false ) ;
39
+ }
31
40
} ;
32
41
33
42
LocalStorageKeyEnvironment . prototype . stop = function ( ) {
34
- window . removeEventListener ( 'storage' , this . onStorage ) ;
43
+ if ( typeof window !== 'undefined' && window . removeEventListener ) {
44
+ window . removeEventListener ( 'storage' , this . onStorage ) ;
45
+ }
35
46
} ;
36
47
37
48
LocalStorageKeyEnvironment . prototype . onStorage = function ( ) {
0 commit comments