Index: lib/prefs.js |
=================================================================== |
--- a/lib/prefs.js |
+++ b/lib/prefs.js |
@@ -169,12 +169,13 @@ |
onChanged: new ext._EventTarget(), |
/** |
- * Fired when all preferences have been loaded. You must wait for |
- * this event before using preferences during extension initialization. |
+ * A promise that is fullfilled when all preferences have been loaded. |
+ * Wait for this promise to be fulfilled before using preferences during |
+ * extension initialization. |
* |
- * @event |
+ * @type {Promise} |
*/ |
- onLoaded: new ext._EventTarget() |
+ isLoaded: null |
}; |
function keyToPref(key) |
@@ -224,14 +225,41 @@ |
let prefs = Object.keys(defaults); |
prefs.forEach(addPreference); |
- let localLoaded = false; |
- let managedLoaded = false; |
+ let localLoaded = new Promise(resolve => { |
+ ext.storage.get(prefs.map(prefToKey), function(items) |
+ { |
+ for (let key in items) |
+ overrides[keyToPref(key)] = items[key]; |
- let checkLoaded = function() |
+ resolve(); |
+ }); |
+ }); |
+ |
+ let managedLoaded = new Promise(resolve => { |
+ if (require("info").platform == "chromium" && "managed" in chrome.storage) |
+ { |
+ chrome.storage.managed.get(null, function(items) |
+ { |
+ // Opera doesn't support chrome.storage.managed, but instead simply |
+ // removing the API, Opera sets chrome.runtime.lastError when using it. |
+ // So we have to retrieve that error, to prevent it from showing up |
+ // in the console. |
+ chrome.runtime.lastError; |
+ |
+ for (let key in items) |
+ defaults[key] = items[key]; |
+ |
+ resolve(); |
+ }); |
+ } |
+ else |
+ { |
+ resolve(); |
+ } |
+ }); |
+ |
+ function onLoaded() |
{ |
- if (!localLoaded || !managedLoaded) |
- return; |
- |
ext.storage.onChanged.addListener(function(changes) |
{ |
for (let key in changes) |
@@ -249,41 +277,9 @@ |
} |
} |
}); |
+ } |
- Prefs.onLoaded._dispatch(); |
- }; |
- |
- ext.storage.get(prefs.map(prefToKey), function(items) |
- { |
- for (let key in items) |
- overrides[keyToPref(key)] = items[key]; |
- |
- localLoaded = true; |
- checkLoaded(); |
- }); |
- |
- if (require("info").platform == "chromium" && "managed" in chrome.storage) |
- { |
- chrome.storage.managed.get(null, function(items) |
- { |
- // Opera doesn't support chrome.storage.managed, but instead simply |
- // removing the API, Opera sets chrome.runtime.lastError when using it. |
- // So we have to retrieve that error, to prevent it from showing up |
- // in the console. |
- chrome.runtime.lastError; |
- |
- for (let key in items) |
- defaults[key] = items[key]; |
- |
- managedLoaded = true; |
- checkLoaded(); |
- }); |
- } |
- else |
- { |
- managedLoaded = true; |
- checkLoaded(); |
- } |
+ Prefs.isLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); |
} |
init(); |