Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/prefs.js

Issue 11163046: Properly save modified objects in Prefs (Closed)
Patch Set: Created July 23, 2013, 10:32 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/prefs.js
===================================================================
--- a/lib/prefs.js
+++ b/lib/prefs.js
@@ -39,22 +39,34 @@
let listeners = [];
+let cachedObjects = {};
Wladimir Palant 2013/07/24 08:49:40 Our property getter and setter are closures, we ca
+
function defineProperty(key)
{
Prefs.__defineGetter__(key, function()
{
+ if (key in cachedObjects)
+ return cachedObjects[key];
+
+ var value = null;
if (key in localStorage)
{
try
{
- return JSON.parse(localStorage[key]);
+ value = JSON.parse(localStorage[key]);
}
catch(e)
{
Cu.reportError(e);
}
}
- return defaults[key];
+ value = value || defaults[key];
Wladimir Palant 2013/07/24 08:49:40 What if value is false?
+
+ if (typeof value !== "object")
Wladimir Palant 2013/07/24 08:49:40 I don't really like the special case for objects h
+ return value;
+
+ cachedObjects[key] = JSON.parse(JSON.stringify(value));
Wladimir Palant 2013/07/24 08:49:40 This is wasteful - we only need the parse/stringif
+ return cachedObjects[key];
});
Prefs.__defineSetter__(key, function(value)
{
@@ -66,6 +78,10 @@
localStorage[key] = stringified;
else
delete localStorage[key];
+
+ if (key in cachedObjects)
+ delete cachedObjects[key];
+
for each (let listener in listeners)
listener(key);
});
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld