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

Side by Side Diff: lib/prefs.js

Issue 11163046: Properly save modified objects in Prefs (Closed)
Patch Set: Created July 23, 2013, 10:32 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 21 matching lines...) Expand all
32 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%V ERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNE LSTATUS%&responseStatus=%RESPONSESTATUS%", 32 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%V ERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNE LSTATUS%&responseStatus=%RESPONSESTATUS%",
33 subscriptions_autoupdate: true, 33 subscriptions_autoupdate: true,
34 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/excep tionrules.txt", 34 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/excep tionrules.txt",
35 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%" , 35 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%" ,
36 notificationdata: {}, 36 notificationdata: {},
37 notificationurl: "https://notification.adblockplus.org/notification.json" 37 notificationurl: "https://notification.adblockplus.org/notification.json"
38 }; 38 };
39 39
40 let listeners = []; 40 let listeners = [];
41 41
42 let cachedObjects = {};
Wladimir Palant 2013/07/24 08:49:40 Our property getter and setter are closures, we ca
43
42 function defineProperty(key) 44 function defineProperty(key)
43 { 45 {
44 Prefs.__defineGetter__(key, function() 46 Prefs.__defineGetter__(key, function()
45 { 47 {
48 if (key in cachedObjects)
49 return cachedObjects[key];
50
51 var value = null;
46 if (key in localStorage) 52 if (key in localStorage)
47 { 53 {
48 try 54 try
49 { 55 {
50 return JSON.parse(localStorage[key]); 56 value = JSON.parse(localStorage[key]);
51 } 57 }
52 catch(e) 58 catch(e)
53 { 59 {
54 Cu.reportError(e); 60 Cu.reportError(e);
55 } 61 }
56 } 62 }
57 return defaults[key]; 63 value = value || defaults[key];
Wladimir Palant 2013/07/24 08:49:40 What if value is false?
64
65 if (typeof value !== "object")
Wladimir Palant 2013/07/24 08:49:40 I don't really like the special case for objects h
66 return value;
67
68 cachedObjects[key] = JSON.parse(JSON.stringify(value));
Wladimir Palant 2013/07/24 08:49:40 This is wasteful - we only need the parse/stringif
69 return cachedObjects[key];
58 }); 70 });
59 Prefs.__defineSetter__(key, function(value) 71 Prefs.__defineSetter__(key, function(value)
60 { 72 {
61 if (typeof value != typeof defaults[key]) 73 if (typeof value != typeof defaults[key])
62 throw new Error("Attempt to change preference type"); 74 throw new Error("Attempt to change preference type");
63 75
64 let stringified = JSON.stringify(value); 76 let stringified = JSON.stringify(value);
65 if (stringified != JSON.stringify(defaults[key])) 77 if (stringified != JSON.stringify(defaults[key]))
66 localStorage[key] = stringified; 78 localStorage[key] = stringified;
67 else 79 else
68 delete localStorage[key]; 80 delete localStorage[key];
81
82 if (key in cachedObjects)
83 delete cachedObjects[key];
84
69 for each (let listener in listeners) 85 for each (let listener in listeners)
70 listener(key); 86 listener(key);
71 }); 87 });
72 } 88 }
73 89
74 90
75 let Prefs = exports.Prefs = { 91 let Prefs = exports.Prefs = {
76 addListener: function(listener) 92 addListener: function(listener)
77 { 93 {
78 if (listeners.indexOf(listener) < 0) 94 if (listeners.indexOf(listener) < 0)
79 listeners.push(listener); 95 listeners.push(listener);
80 }, 96 },
81 97
82 removeListener: function(listener) 98 removeListener: function(listener)
83 { 99 {
84 let index = listeners.indexOf(listener); 100 let index = listeners.indexOf(listener);
85 if (index >= 0) 101 if (index >= 0)
86 listeners.splice(index, 1); 102 listeners.splice(index, 1);
87 }, 103 },
88 }; 104 };
89 105
90 for (let key in defaults) 106 for (let key in defaults)
91 defineProperty(key); 107 defineProperty(key);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld