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

Side by Side Diff: lib/prefs.js

Issue 29333533: Issue 3515 - Replace Prefs.onLoaded event by Prefs.isLoaded promise (Closed)
Patch Set: Created Jan. 14, 2016, 5:29 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 | « background.js ('k') | lib/uninstall.js » ('j') | 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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 let Prefs = exports.Prefs = { 162 let Prefs = exports.Prefs = {
163 /** 163 /**
164 * Fired when the value of a preference changes. 164 * Fired when the value of a preference changes.
165 * 165 *
166 * @event 166 * @event
167 * @property {string} pref The name of the preference that changed. 167 * @property {string} pref The name of the preference that changed.
168 */ 168 */
169 onChanged: new ext._EventTarget(), 169 onChanged: new ext._EventTarget(),
170 170
171 /** 171 /**
172 * Fired when all preferences have been loaded. You must wait for 172 * A promise that is fullfilled when all preferences have been loaded.
173 * this event before using preferences during extension initialization. 173 * Wait for this promise to be fulfilled before using preferences during
174 * extension initialization.
174 * 175 *
175 * @event 176 * @type {Promise}
176 */ 177 */
177 onLoaded: new ext._EventTarget() 178 isLoaded: null
178 }; 179 };
179 180
180 function keyToPref(key) 181 function keyToPref(key)
181 { 182 {
182 if (key.indexOf(keyPrefix) != 0) 183 if (key.indexOf(keyPrefix) != 0)
183 return null; 184 return null;
184 185
185 return key.substr(keyPrefix.length); 186 return key.substr(keyPrefix.length);
186 } 187 }
187 188
(...skipping 29 matching lines...) Expand all
217 }, 218 },
218 enumerable: true 219 enumerable: true
219 }); 220 });
220 } 221 }
221 222
222 function init() 223 function init()
223 { 224 {
224 let prefs = Object.keys(defaults); 225 let prefs = Object.keys(defaults);
225 prefs.forEach(addPreference); 226 prefs.forEach(addPreference);
226 227
227 let localLoaded = false; 228 let localLoaded = new Promise(resolve => {
228 let managedLoaded = false; 229 ext.storage.get(prefs.map(prefToKey), function(items)
230 {
231 for (let key in items)
232 overrides[keyToPref(key)] = items[key];
229 233
230 let checkLoaded = function() 234 resolve();
235 });
236 });
237
238 let managedLoaded = new Promise(resolve => {
239 if (require("info").platform == "chromium" && "managed" in chrome.storage)
240 {
241 chrome.storage.managed.get(null, function(items)
242 {
243 // Opera doesn't support chrome.storage.managed, but instead simply
244 // removing the API, Opera sets chrome.runtime.lastError when using it.
245 // So we have to retrieve that error, to prevent it from showing up
246 // in the console.
247 chrome.runtime.lastError;
248
249 for (let key in items)
250 defaults[key] = items[key];
251
252 resolve();
253 });
254 }
255 else
256 {
257 resolve();
258 }
259 });
260
261 function onLoaded()
231 { 262 {
232 if (!localLoaded || !managedLoaded)
233 return;
234
235 ext.storage.onChanged.addListener(function(changes) 263 ext.storage.onChanged.addListener(function(changes)
236 { 264 {
237 for (let key in changes) 265 for (let key in changes)
238 { 266 {
239 let pref = keyToPref(key); 267 let pref = keyToPref(key);
240 if (pref && pref in defaults) 268 if (pref && pref in defaults)
241 { 269 {
242 let change = changes[key]; 270 let change = changes[key];
243 if ("newValue" in change && change.newValue != defaults[pref]) 271 if ("newValue" in change && change.newValue != defaults[pref])
244 overrides[pref] = change.newValue; 272 overrides[pref] = change.newValue;
245 else 273 else
246 delete overrides[pref]; 274 delete overrides[pref];
247 275
248 Prefs.onChanged._dispatch(pref); 276 Prefs.onChanged._dispatch(pref);
249 } 277 }
250 } 278 }
251 }); 279 });
280 }
252 281
253 Prefs.onLoaded._dispatch(); 282 Prefs.isLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
254 };
255
256 ext.storage.get(prefs.map(prefToKey), function(items)
257 {
258 for (let key in items)
259 overrides[keyToPref(key)] = items[key];
260
261 localLoaded = true;
262 checkLoaded();
263 });
264
265 if (require("info").platform == "chromium" && "managed" in chrome.storage)
266 {
267 chrome.storage.managed.get(null, function(items)
268 {
269 // Opera doesn't support chrome.storage.managed, but instead simply
270 // removing the API, Opera sets chrome.runtime.lastError when using it.
271 // So we have to retrieve that error, to prevent it from showing up
272 // in the console.
273 chrome.runtime.lastError;
274
275 for (let key in items)
276 defaults[key] = items[key];
277
278 managedLoaded = true;
279 checkLoaded();
280 });
281 }
282 else
283 {
284 managedLoaded = true;
285 checkLoaded();
286 }
287 } 283 }
288 284
289 init(); 285 init();
OLDNEW
« no previous file with comments | « background.js ('k') | lib/uninstall.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld