code review re. badge update

This commit is contained in:
gorhill 2015-05-01 08:27:41 -04:00
parent 9f529bd6b0
commit ca1e1f55e7
3 changed files with 18 additions and 23 deletions

View File

@ -170,28 +170,34 @@ return asyncJobManager;
/******************************************************************************/
// Update visual of extension icon.
// A time out is used to coalesce adjacent requests to update badge.
µBlock.updateBadgeAsync = (function() {
var µb = µBlock;
var tabIdToTimer = {};
// Cache callback definition, it was a bad idea to define this one inside
// updateBadgeAsync
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
var pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore ) {
pageStore.updateBadge();
if ( pageStore === null ) {
return;
}
vAPI.setIcon(tabId, 'off', '');
var netFiltering = pageStore.getNetFilteringSwitch();
var badge = '';
if ( µb.userSettings.showIconBadge && netFiltering && pageStore.perLoadBlockedRequestCount ) {
badge = µb.utils.formatCount(pageStore.perLoadBlockedRequestCount);
}
vAPI.setIcon(tabId, netFiltering ? 'on' : 'off', badge);
};
var updateBadgeAsync = function(tabId) {
return function(tabId) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return;
}
µb.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250);
if ( tabIdToTimer.hasOwnProperty(tabId) ) {
return;
}
tabIdToTimer[tabId] = setTimeout(updateBadge.bind(null, tabId), 500);
};
return updateBadgeAsync;
})();

View File

@ -589,17 +589,6 @@ PageStore.prototype.logRequest = function(context, result) {
µb.localSettingsModifyTime = now;
};
/******************************************************************************/
PageStore.prototype.updateBadge = function() {
var netFiltering = this.getNetFilteringSwitch();
var badge = '';
if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) {
badge = µb.utils.formatCount(this.perLoadBlockedRequestCount);
}
vAPI.setIcon(this.tabId, netFiltering ? 'on' : 'off', badge);
};
// https://www.youtube.com/watch?v=drW8p_dTLD4
/******************************************************************************/

View File

@ -556,7 +556,7 @@ vAPI.tabs.registerListeners();
/******************************************************************************/
µb.pageStoreFromTabId = function(tabId) {
return this.pageStores[tabId];
return this.pageStores[tabId] || null;
};
/******************************************************************************/