From 8aebf060b83dc6739dd3dd6c44fdbaed99664ff3 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 30 Jun 2016 15:47:51 -0400 Subject: [PATCH] code review: return asap when in shouldLoadPopupListener --- platform/firefox/vapi-background.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index d907fac22..67e7f7681 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -2331,15 +2331,8 @@ vAPI.net.registerListeners = function() { } var shouldLoadPopupListenerMessageName = location.host + ':shouldLoadPopup'; - var shouldLoadPopupListener = function(e) { - if ( typeof vAPI.tabs.onPopupCreated !== 'function' ) { - return; - } - - var openerURL = e.data; - var popupTabId = tabWatcher.tabIdFromTarget(e.target); + var shouldLoadPopupListener = function(openerURL, popupTabId) { var uri, openerTabId; - for ( var browser of tabWatcher.browsers() ) { uri = browser.currentURI; @@ -2362,10 +2355,20 @@ vAPI.net.registerListeners = function() { } } }; + var shouldLoadPopupListenerAsync = function(e) { + if ( typeof vAPI.tabs.onPopupCreated !== 'function' ) { + return; + } + // We are handling a synchronous message: do not block. + vAPI.setTimeout( + shouldLoadPopupListener.bind(null, e.data, tabWatcher.tabIdFromTarget(e.target)), + 1 + ); + }; vAPI.messaging.globalMessageManager.addMessageListener( shouldLoadPopupListenerMessageName, - shouldLoadPopupListener + shouldLoadPopupListenerAsync ); var shouldLoadListenerMessageName = location.host + ':shouldLoad'; @@ -2451,7 +2454,7 @@ vAPI.net.registerListeners = function() { cleanupTasks.push(function() { vAPI.messaging.globalMessageManager.removeMessageListener( shouldLoadPopupListenerMessageName, - shouldLoadPopupListener + shouldLoadPopupListenerAsync ); vAPI.messaging.globalMessageManager.removeMessageListener(