Skip to content

Commit a2eefac

Browse files
committed
Merge pull request #127 from jsg2021/make-storage-private-mode-safe
Test the interface before just assuming its gtg.
2 parents bd45a28 + b922383 commit a2eefac

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Diff for: lib/environment/LocalStorageKeyEnvironment.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33
var Environment = require('./Environment');
44

55
/**
6-
* Routing environment which stores routing state in localStorage.
7-
*/
6+
* Routing environment which stores routing state in localStorage.
7+
*/
88
function LocalStorageKeyEnvironment(key) {
99
this.key = key;
1010
var store = this.onStorage = this.onStorage.bind(this);
11+
var storage;
1112

12-
this.storage = (typeof window !== 'undefined' && window.localStorage) || {
13+
try {
14+
storage = window.localStorage;
15+
storage.setItem(key, storage.getItem(key));
16+
} catch (e) {
17+
storage = null;
18+
}
19+
20+
this.storage = storage || {
1321
data: {},
1422
getItem: function(key) {return this.data[key];},
15-
setItem: function(key, val) {this.data[key]=val;store();}
23+
setItem: function(key, val) {
24+
this.data[key]=val;
25+
clearTimeout(this.storeEvent);
26+
this.storeEvent = setTimeout(store,1);
27+
}
1628
};
1729

1830
Environment.call(this);
@@ -41,13 +53,12 @@ LocalStorageKeyEnvironment.prototype.start = function() {
4153

4254
LocalStorageKeyEnvironment.prototype.stop = function() {
4355
if (typeof window !== 'undefined' && window.removeEventListener) {
44-
window.removeEventListener('storage', this.onStorage);
56+
window.removeEventListener('storage', this.onStorage, false);
4557
}
4658
};
4759

4860
LocalStorageKeyEnvironment.prototype.onStorage = function() {
4961
var path = this.getPath();
50-
5162
if (this.path !== path) {
5263
this.setPath(path, {});
5364
}

0 commit comments

Comments
 (0)