|
3 | 3 | var Environment = require('./Environment');
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * Routing environment which stores routing state in localStorage. |
7 |
| - */ |
| 6 | +* Routing environment which stores routing state in localStorage. |
| 7 | +*/ |
8 | 8 | function LocalStorageKeyEnvironment(key) {
|
9 | 9 | this.key = key;
|
10 | 10 | var store = this.onStorage = this.onStorage.bind(this);
|
| 11 | + var storage; |
11 | 12 |
|
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 || { |
13 | 21 | data: {},
|
14 | 22 | 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 | + } |
16 | 28 | };
|
17 | 29 |
|
18 | 30 | Environment.call(this);
|
@@ -41,13 +53,12 @@ LocalStorageKeyEnvironment.prototype.start = function() {
|
41 | 53 |
|
42 | 54 | LocalStorageKeyEnvironment.prototype.stop = function() {
|
43 | 55 | if (typeof window !== 'undefined' && window.removeEventListener) {
|
44 |
| - window.removeEventListener('storage', this.onStorage); |
| 56 | + window.removeEventListener('storage', this.onStorage, false); |
45 | 57 | }
|
46 | 58 | };
|
47 | 59 |
|
48 | 60 | LocalStorageKeyEnvironment.prototype.onStorage = function() {
|
49 | 61 | var path = this.getPath();
|
50 |
| - |
51 | 62 | if (this.path !== path) {
|
52 | 63 | this.setPath(path, {});
|
53 | 64 | }
|
|
0 commit comments