Skip to content

Commit 36f2056

Browse files
author
Chris K
committed
DFL-3376 Random exception when switching debug context
1 parent c2220f6 commit 36f2056

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

src/client-en.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ window.load_screen_timeout = window.setTimeout(function()
219219
<script src="./lib/stringprototype.js"/>
220220
<script src="./lib/stylesheetlistprototype.js"/>
221221
<script src="./lib/xmlhttprequestprototype.js"/>
222+
<script src="./lib/storageprototype.js"/>
222223

223224

224225
<script src="./scripts/Timeouts.js"/>

src/lib/storageprototype.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Storage.prototype.get_and_parse_item = function(key, default_value)
2+
{
3+
var raw_value = this.getItem(key);
4+
var value = null;
5+
try
6+
{
7+
value = JSON.parse(raw_value);
8+
}
9+
catch(e)
10+
{
11+
value = default_value;
12+
};
13+
return value === null || value === undefined ? default_value : value;
14+
};
15+
16+
Storage.prototype.stringify_and_set_item = function(key, value)
17+
{
18+
this.setItem(key, JSON.stringify(value));
19+
};

src/ui-scripts/settings.js

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ var SettingsBase = function()
1717
*/
1818
this.set = function(key, value, sync_switches)
1919
{
20-
window.localStorage.setItem(key, JSON.stringify(this.map[key] = value));
20+
this.map[key] = value;
21+
this._storage.stringify_and_set_item(key, value);
2122
if (this.callback_map.hasOwnProperty(key))
22-
{
2323
this.callback_map[key].call(this, value);
24-
}
2524
messages.post("setting-changed", {id: this.view_id, key: key, value: value});
26-
}
25+
};
2726

2827
/**
2928
* Returns the value assosciated with "key". If the key does not exist,
@@ -32,12 +31,10 @@ var SettingsBase = function()
3231
*/
3332
this.get = function(key)
3433
{
35-
var val = "";
36-
return (
37-
typeof this.map[key] !== 'undefined' ?
38-
this.map[key] :
39-
(this.map[key] = ((val = window.localStorage.getItem(key)) ? JSON.parse(val) : null)));
40-
}
34+
if (typeof this.map[key] == "undefined")
35+
this.map[key] = this._storage.get_and_parse_item(key, this._default_values[key]);
36+
return this.map[key]
37+
};
4138

4239
/**
4340
* Check if a particular key exist in the settings object
@@ -53,7 +50,7 @@ var SettingsBase = function()
5350
views[this.view_id].update();
5451
};
5552

56-
this.init = function(view_id, key_map, label_map, setting_map, templates, group, callback_map)
53+
this.init = function(view_id, default_values, label_map, setting_map, templates, group, callback_map)
5754
{
5855
this.map = {};
5956
this.view_id = view_id;
@@ -62,21 +59,16 @@ var SettingsBase = function()
6259
this.templates = templates || {};
6360
this.group = group;
6461
this.callback_map = callback_map || {};
65-
var stored_map = key_map, key = '', val = '';
66-
for( key in stored_map)
62+
this._default_values = default_values;
63+
this._storage = window.localStorage;
64+
for (var key in default_values)
6765
{
68-
val = window.localStorage.getItem(key);
69-
this.map[key] = (val === undefined || val === null) ? key_map[key] :
70-
val === 'undefined' ? undefined : JSON.parse(val);
66+
this.map[key] = this._storage.get_and_parse_item(key, default_values[key]);
7167
}
72-
if(!window.settings)
73-
{
68+
if (!window.settings)
7469
window.settings = {};
75-
}
76-
window.settings[arguments[0]] = this;
77-
70+
window.settings[view_id] = this;
7871
window.messages.post("settings-initialized", {view_id: view_id, setting: this});
79-
8072
// Add a context menu
8173
var contextmenu = ContextMenu.get_instance();
8274
var menu = setting_map && setting_map.contextmenu;
@@ -93,30 +85,7 @@ var SettingsBase = function()
9385
}
9486
contextmenu.register(view_id, items);
9587
}
96-
}
97-
98-
if(!window.localStorage)
99-
{
100-
window.localStorage =
101-
{
102-
setItem: function(name, value)
103-
{
104-
document.cookie = name + "="+
105-
encodeURIComponent(value)+
106-
"; expires="+(new Date(new Date().getTime()+ (360 * 24 * 60 * 60 * 1000 ))).toGMTString()+
107-
"; path=/";
108-
},
109-
getItem: function(name)
110-
{
111-
var match = null;
112-
if (match = new RegExp(name+'\=([^;]*);','').exec(document.cookie+';'))
113-
{
114-
return decodeURIComponent(match[1]);
115-
}
116-
return null;
117-
}
118-
}
119-
}
88+
};
12089
}
12190

12291
/**

0 commit comments

Comments
 (0)