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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « background.js ('k') | lib/uninstall.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
« 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