Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 * @param {string} preference | 219 * @param {string} preference |
220 * @param {any} value | 220 * @param {any} value |
221 * @return {Promise} A promise that resolves when the underlying | 221 * @return {Promise} A promise that resolves when the underlying |
222 browser.storage.local.set/remove() operation completes | 222 browser.storage.local.set/remove() operation completes |
223 */ | 223 */ |
224 set(preference, value) | 224 set(preference, value) |
225 { | 225 { |
226 let defaultValue = defaults[preference]; | 226 let defaultValue = defaults[preference]; |
227 | 227 |
228 if (typeof value != typeof defaultValue) | 228 if (typeof value != typeof defaultValue) |
229 throw new Error("Attempt to change preference type"); | 229 throw new Error("Attempt to change preference type"); |
Thomas Greiner
2018/04/27 14:50:12
Why don't we return `Promise.reject()` here? It's
Sebastian Noack
2018/04/27 16:01:03
Well, if we already know immediately/synchronously
Thomas Greiner
2018/04/27 16:27:33
True, if that's a requirement we could check twice
Sebastian Noack
2018/04/27 16:33:22
Well, compatibility with the setter property isn't
Thomas Greiner
2018/04/27 17:03:32
As I said, we can still throw the error in the set
Sebastian Noack
2018/04/27 17:11:11
Yes, we could signal the error asynchronously here
Thomas Greiner
2018/04/27 17:16:06
Note that assertions may fail asynchronously in wh
| |
230 | 230 |
231 if (value == defaultValue) | 231 if (value == defaultValue) |
232 { | 232 { |
233 delete overrides[preference]; | 233 delete overrides[preference]; |
234 return browser.storage.local.remove(prefToKey(preference)); | 234 return browser.storage.local.remove(prefToKey(preference)); |
235 } | 235 } |
236 | 236 |
237 overrides[preference] = value; | 237 overrides[preference] = value; |
238 return (customSave.get(preference) || savePref)(preference); | 238 return (customSave.get(preference) || savePref)(preference); |
239 }, | 239 }, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 } | 290 } |
291 | 291 |
292 let customSave = new Map(); | 292 let customSave = new Map(); |
293 if (require("../buildtools/info").platform == "gecko") | 293 if (require("../buildtools/info").platform == "gecko") |
294 { | 294 { |
295 // Saving one storage value causes all others to be saved as well on Gecko. | 295 // Saving one storage value causes all others to be saved as well on Gecko. |
296 // Make sure that updating ad counter doesn't cause the filters data to be | 296 // Make sure that updating ad counter doesn't cause the filters data to be |
297 // saved frequently as a side-effect. | 297 // saved frequently as a side-effect. |
298 const MIN_UPDATE_INTERVAL = 60 * 1000; | 298 const MIN_UPDATE_INTERVAL = 60 * 1000; |
299 let lastUpdate = -MIN_UPDATE_INTERVAL; | 299 let lastUpdate = -MIN_UPDATE_INTERVAL; |
300 let updateScheduled = false; | 300 let promise = null; |
301 customSave.set("blocked_total", pref => | 301 customSave.set("blocked_total", pref => |
302 { | 302 { |
303 if (!updateScheduled) | 303 if (!promise) |
304 { | 304 { |
305 let callback = () => | 305 promise = new Promise((resolve, reject) => |
306 { | 306 { |
307 lastUpdate = performance.now(); | 307 setTimeout( |
308 updateScheduled = false; | 308 () => |
309 savePref(pref); | 309 { |
310 }; | 310 lastUpdate = performance.now(); |
311 | 311 promise = null; |
312 let timeElapsed = performance.now() - lastUpdate; | 312 savePref(pref).then(resolve, reject); |
313 if (timeElapsed < MIN_UPDATE_INTERVAL) | 313 }, |
314 { | 314 lastUpdate + MIN_UPDATE_INTERVAL - performance.now() |
315 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); | 315 ); |
316 updateScheduled = true; | 316 }); |
317 } | |
318 else | |
319 callback(); | |
Thomas Greiner
2018/04/27 14:50:12
What if an error occurs in `savePref()`? I'd argue
Sebastian Noack
2018/04/27 16:01:04
Yes, returning a resolved promise immediately is a
Sebastian Noack
2018/04/27 16:14:52
Never mind, it turned out returning asynchronously
Thomas Greiner
2018/04/27 16:27:33
Thanks.
| |
320 } | 317 } |
321 return Promise.resolve(); | 318 return promise; |
322 }); | 319 }); |
323 } | 320 } |
324 | 321 |
325 function addPreference(pref) | 322 function addPreference(pref) |
326 { | 323 { |
327 Object.defineProperty(Prefs, pref, { | 324 Object.defineProperty(Prefs, pref, { |
328 get() { return (pref in overrides ? overrides : defaults)[pref]; }, | 325 get() { return (pref in overrides ? overrides : defaults)[pref]; }, |
329 set(value) | 326 set(value) |
Thomas Greiner
2018/04/27 14:50:12
Suggestion: What about marking this setter as depr
Sebastian Noack
2018/04/27 16:01:03
We didn't decide yet, whether we want to consider
Thomas Greiner
2018/04/27 16:27:33
Acknowledged.
| |
330 { | 327 { |
331 Prefs.set(pref, value); | 328 Prefs.set(pref, value); |
332 }, | 329 }, |
333 enumerable: true | 330 enumerable: true |
334 }); | 331 }); |
335 } | 332 } |
336 | 333 |
337 function init() | 334 function init() |
338 { | 335 { |
339 let prefs = Object.keys(defaults); | 336 let prefs = Object.keys(defaults); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 eventEmitter.emit(pref); | 382 eventEmitter.emit(pref); |
386 } | 383 } |
387 } | 384 } |
388 }); | 385 }); |
389 } | 386 } |
390 | 387 |
391 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); | 388 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
392 } | 389 } |
393 | 390 |
394 init(); | 391 init(); |
LEFT | RIGHT |