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

Unified Diff: lib/ui.js

Issue 29345630: Issue 4131 - Simplify UI initialization logic (Closed)
Patch Set: Addressed comments Created June 8, 2016, 8:50 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/ui.js
===================================================================
--- a/lib/ui.js
+++ b/lib/ui.js
@@ -272,75 +272,67 @@ let notificationTimer = null;
let UI = exports.UI =
{
/**
* Gets called on startup, initializes UI integration.
*/
init: function()
{
- // We should call initDone once both overlay and filters are loaded
- let overlayLoaded = false;
- let filtersLoaded = false;
- let sessionRestored = false;
+ // We have to wait for multiple events before running start-up actions
+ let prerequisites = [];
// Start loading overlay
- let request = new XMLHttpRequest();
- request.mozBackgroundRequest = true;
- request.open("GET", "chrome://adblockplus/content/ui/overlay.xul");
- request.channel.owner = Utils.systemPrincipal;
- request.addEventListener("load", function(event)
+ prerequisites.push(new Promise((resolve, reject) =>
{
- if (onShutdown.done)
- return;
+ let request = new XMLHttpRequest();
+ request.mozBackgroundRequest = true;
+ request.open("GET", "chrome://adblockplus/content/ui/overlay.xul");
+ request.channel.owner = Utils.systemPrincipal;
+ request.addEventListener("load", event =>
+ {
+ if (onShutdown.done)
+ return;
- this.processOverlay(request.responseXML.documentElement);
+ this.processOverlay(request.responseXML.documentElement);
- // Don't wait for the rest of the startup sequence, add icon already
- this.addToolbarButton();
+ // Don't wait for the rest of the startup sequence, add icon already
+ this.addToolbarButton();
- overlayLoaded = true;
- if (overlayLoaded && filtersLoaded && sessionRestored)
- this.initDone();
- }.bind(this), false);
- request.send(null);
+ resolve();
+ }, false);
+
+ request.addEventListener("error", event =>
+ {
+ reject(new Error("Unexpected: Failed to load overlay.xul"));
+ });
+
+ request.send(null);
+ }));
// Wait for filters to load
if (FilterStorage._loading)
+ prerequisites.push(FilterNotifier.once("load"));
+
+ // Wait for session to be restored
+ prerequisites.push(new Promise((resolve, reject) =>
{
- let listener = function(action)
+ let window = this.currentWindow;
+ if (!window && "nsISessionStore" in Ci)
{
- if (action != "load")
- return;
+ // No application windows yet, the application must be starting up. Wait
+ // for session to be restored before initializing our UI.
+ new SessionRestoreObserver(resolve);
+ }
+ else
+ resolve();
+ }));
- FilterNotifier.removeListener(listener);
- filtersLoaded = true;
- if (overlayLoaded && filtersLoaded && sessionRestored)
- this.initDone();
- }.bind(this);
- FilterNotifier.addListener(listener);
- }
- else
- filtersLoaded = true;
-
- // Initialize UI after the session is restored
- let window = this.currentWindow;
- if (!window && "nsISessionStore" in Ci)
- {
- // No application windows yet, the application must be starting up. Wait
- // for session to be restored before initializing our UI.
- new SessionRestoreObserver(function()
- {
- sessionRestored = true;
- if (overlayLoaded && filtersLoaded && sessionRestored)
- this.initDone();
- }.bind(this));
- }
- else
- sessionRestored = true;
+ Promise.all(prerequisites).then(() => this.initDone())
+ .catch(e => Cu.reportError(e));
},
/**
* Provesses overlay document data and initializes overlay property.
*/
processOverlay: function(/**Element*/ root)
{
Utils.splitAllLabels(root);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld