1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 23:42:39 +01:00

code review re. badge update

This commit is contained in:
gorhill 2015-05-01 08:27:41 -04:00 committed by Chris
parent 561e1ca93e
commit 10c7a40392
3 changed files with 20 additions and 24 deletions

View File

@ -16,7 +16,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/chrisaljoudi/uBlock
Home: https://github.com/gorhill/uBlock
*/
/* global µBlock */
@ -173,24 +173,31 @@ return asyncJobManager;
µ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() {
var pageStore = µb.pageStoreFromTabId(tabIdToUpdate);
if ( pageStore ) {
pageStore.updateBadge();
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
var pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}
vAPI.setIcon(tabIdToUpdate, '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

@ -588,17 +588,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

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