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

Side by Side Diff: lib/prefs.js

Issue 29329760: Issue 3260 - Remove expression closure from build tools (Closed)
Patch Set: Created Nov. 5, 2015, 10:55 a.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 /* This Source Code Form is subject to the terms of the Mozilla Public 1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 4
5 Cu.import("resource://gre/modules/Services.jsm"); 5 Cu.import("resource://gre/modules/Services.jsm");
6 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 6 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
7 7
8 let {addonRoot, addonName} = require("info"); 8 let {addonRoot, addonName} = require("info");
9 let branchName = "extensions." + addonName + "."; 9 let branchName = "extensions." + addonName + ".";
10 let branch = Services.prefs.getBranch(branchName); 10 let branch = Services.prefs.getBranch(branchName);
(...skipping 26 matching lines...) Expand all
37 catch (e) {} 37 catch (e) {}
38 } 38 }
39 setter(defaultBranch, pref, value); 39 setter(defaultBranch, pref, value);
40 defineProperty(pref, false, getter, setter); 40 defineProperty(pref, false, getter, setter);
41 } 41 }
42 42
43 // Add preference change observer 43 // Add preference change observer
44 try 44 try
45 { 45 {
46 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true); 46 branch.QueryInterface(Ci.nsIPrefBranch2).addObserver("", Prefs, true);
47 onShutdown.add(function() branch.removeObserver("", Prefs)); 47 onShutdown.add(function()
48 {
49 branch.removeObserver("", Prefs);
50 });
Wladimir Palant 2015/11/05 11:25:16 I'd really prefer an arrow function here instead o
48 } 51 }
49 catch (e) 52 catch (e)
50 { 53 {
51 Cu.reportError(e); 54 Cu.reportError(e);
52 } 55 }
53 } 56 }
54 57
55 /** 58 /**
56 * Sets up getter/setter on Prefs object for preference. 59 * Sets up getter/setter on Prefs object for preference.
57 */ 60 */
58 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc) 61 function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
59 { 62 {
60 let value = defaultValue; 63 let value = defaultValue;
61 Prefs["_update_" + name] = function() 64 Prefs["_update_" + name] = function()
62 { 65 {
63 try 66 try
64 { 67 {
65 value = readFunc(branch, name); 68 value = readFunc(branch, name);
66 triggerListeners(name); 69 triggerListeners(name);
67 } 70 }
68 catch(e) 71 catch(e)
69 { 72 {
70 Cu.reportError(e); 73 Cu.reportError(e);
71 } 74 }
72 }; 75 };
73 Prefs.__defineGetter__(name, function() value); 76 Prefs.__defineGetter__(name, function()
77 {
78 return value;
79 });
Wladimir Palant 2015/11/05 11:25:16 Same here, I'd rather have this changed into an ar
74 Prefs.__defineSetter__(name, function(newValue) 80 Prefs.__defineSetter__(name, function(newValue)
75 { 81 {
76 if (value == newValue) 82 if (value == newValue)
77 return value; 83 return value;
78 84
79 try 85 try
80 { 86 {
81 ignorePrefChanges = true; 87 ignorePrefChanges = true;
82 writeFunc(branch, name, newValue); 88 writeFunc(branch, name, newValue);
83 value = newValue; 89 value = newValue;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 177
172 // Getter/setter functions for difference preference types 178 // Getter/setter functions for difference preference types
173 let typeMap = 179 let typeMap =
174 { 180 {
175 boolean: [getBoolPref, setBoolPref], 181 boolean: [getBoolPref, setBoolPref],
176 number: [getIntPref, setIntPref], 182 number: [getIntPref, setIntPref],
177 string: [getCharPref, setCharPref], 183 string: [getCharPref, setCharPref],
178 object: [getJSONPref, setJSONPref] 184 object: [getJSONPref, setJSONPref]
179 }; 185 };
180 186
181 function getIntPref(branch, pref) branch.getIntPref(pref) 187 function getIntPref(branch, pref)
182 function setIntPref(branch, pref, newValue) branch.setIntPref(pref, newValue) 188 {
189 return branch.getIntPref(pref);
190 }
191 function setIntPref(branch, pref, newValue)
192 {
193 branch.setIntPref(pref, newValue);
194 }
183 195
184 function getBoolPref(branch, pref) branch.getBoolPref(pref) 196 function getBoolPref(branch, pref)
185 function setBoolPref(branch, pref, newValue) branch.setBoolPref(pref, newValue) 197 {
198 return branch.getBoolPref(pref);
199 }
200 function setBoolPref(branch, pref, newValue)
201 {
202 branch.setBoolPref(pref, newValue);
203 }
186 204
187 function getCharPref(branch, pref) branch.getComplexValue(pref, Ci.nsISupportsSt ring).data 205 function getCharPref(branch, pref)
206 {
207 return branch.getComplexValue(pref, Ci.nsISupportsString).data;
208 }
188 function setCharPref(branch, pref, newValue) 209 function setCharPref(branch, pref, newValue)
189 { 210 {
190 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring); 211 let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsSt ring);
191 str.data = newValue; 212 str.data = newValue;
192 branch.setComplexValue(pref, Ci.nsISupportsString, str); 213 branch.setComplexValue(pref, Ci.nsISupportsString, str);
193 } 214 }
194 215
195 function getJSONPref(branch, pref) JSON.parse(getCharPref(branch, pref)) 216 function getJSONPref(branch, pref)
196 function setJSONPref(branch, pref, newValue) setCharPref(branch, pref, JSON.stri ngify(newValue)) 217 {
218 return JSON.parse(getCharPref(branch, pref));
219 }
220 function setJSONPref(branch, pref, newValue)
221 {
222 setCharPref(branch, pref, JSON.stringify(newValue))
223 }
197 224
198 init(); 225 init();
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