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

Side by Side Diff: lib/prefs.js

Issue 29338534: Issue 3826 - Filter preference change events (Closed)
Patch Set: Created March 17, 2016, 4:30 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
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module prefs */ 18 /** @module prefs */
19 19
20 let {EventEmitter} = require("events");
21
20 const keyPrefix = "pref:"; 22 const keyPrefix = "pref:";
21 23
24 let eventEmitter = new EventEmitter();
25 let overrides = Object.create(null);
26
22 /** @lends module:prefs.Prefs */ 27 /** @lends module:prefs.Prefs */
23 let defaults = Object.create(null); 28 let defaults = Object.create(null);
24 let overrides = Object.create(null);
25 29
26 /** 30 /**
27 * Only for compatibility with core code. Please do not change! 31 * Only for compatibility with core code. Please do not change!
28 * 32 *
29 * @type {boolean} 33 * @type {boolean}
30 */ 34 */
31 defaults.enabled = true; 35 defaults.enabled = true;
32 /** 36 /**
33 * The application version as set during initialization. Used to detect updates. 37 * The application version as set during initialization. Used to detect updates.
34 * 38 *
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 * @type {string[]} 175 * @type {string[]}
172 */ 176 */
173 defaults.additional_subscriptions = []; 177 defaults.additional_subscriptions = [];
174 178
175 /** 179 /**
176 * @namespace 180 * @namespace
177 * @static 181 * @static
178 */ 182 */
179 let Prefs = exports.Prefs = { 183 let Prefs = exports.Prefs = {
180 /** 184 /**
181 * Fired when the value of a preference changes. 185 * Adds a callback that is called when the
186 * value of the specified preference changed.
kzar 2016/03/18 15:49:59 Nit: Should be "a specified"
Sebastian Noack 2016/03/18 18:15:49 Done.
182 * 187 *
183 * @event 188 * @param {string} preference
184 * @property {string} pref The name of the preference that changed. 189 * @param {function} callback
185 */ 190 */
186 onChanged: new ext._EventTarget(), 191 on: function(preference, callback)
192 {
193 eventEmitter.on(preference, callback);
194 },
195
196 /**
197 * Removes a callback for the specified preference.
198 *
199 * @param {string} preference
200 * @param {function} callback
201 */
202 off: function(preference, callback)
203 {
204 eventEmitter.off(preference, callback);
205 },
187 206
188 /** 207 /**
189 * A promise that is fullfilled when all preferences have been loaded. 208 * A promise that is fullfilled when all preferences have been loaded.
190 * Wait for this promise to be fulfilled before using preferences during 209 * Wait for this promise to be fulfilled before using preferences during
191 * extension initialization. 210 * extension initialization.
192 * 211 *
193 * @type {Promise} 212 * @type {Promise}
194 */ 213 */
195 untilLoaded: null 214 untilLoaded: null
196 }; 215 };
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 { 302 {
284 let pref = keyToPref(key); 303 let pref = keyToPref(key);
285 if (pref && pref in defaults) 304 if (pref && pref in defaults)
286 { 305 {
287 let change = changes[key]; 306 let change = changes[key];
288 if ("newValue" in change && change.newValue != defaults[pref]) 307 if ("newValue" in change && change.newValue != defaults[pref])
289 overrides[pref] = change.newValue; 308 overrides[pref] = change.newValue;
290 else 309 else
291 delete overrides[pref]; 310 delete overrides[pref];
292 311
293 Prefs.onChanged._dispatch(pref); 312 eventEmitter.emit(pref);
294 } 313 }
295 } 314 }
296 }); 315 });
297 } 316 }
298 317
299 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 318 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
300 } 319 }
301 320
302 init(); 321 init();
OLDNEW

Powered by Google App Engine
This is Rietveld