1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-30 03:50:41 +12:00

code review

This commit is contained in:
gorhill 2015-05-01 08:09:01 -04:00
parent 37331b1ef2
commit f4807b6b29

View file

@ -571,26 +571,38 @@ PageStore.prototype.recordRequest = function(type, url, block) {
// notifying me, and this causes internal cached state to be out of sync. // notifying me, and this causes internal cached state to be out of sync.
PageStore.prototype.updateBadgeAsync = (function() { PageStore.prototype.updateBadgeAsync = (function() {
var updateBadge = function() { var tabIdToTimer = {};
this.updateBadgeTimer = null;
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}
var iconId = null; var iconId = null;
var badgeStr = ''; var badgeStr = '';
var total = this.perLoadAllowedRequestCount + this.perLoadBlockedRequestCount; var total = pageStore.perLoadAllowedRequestCount +
pageStore.perLoadBlockedRequestCount;
if ( total ) { if ( total ) {
var squareSize = 19; var squareSize = 19;
var greenSize = squareSize * Math.sqrt(this.perLoadAllowedRequestCount / total); var greenSize = squareSize * Math.sqrt(pageStore.perLoadAllowedRequestCount / total);
iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize); iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize);
badgeStr = µm.formatCount(this.distinctRequestCount); badgeStr = µm.formatCount(pageStore.distinctRequestCount);
} }
vAPI.setIcon(this.tabId, iconId, badgeStr); vAPI.setIcon(tabId, iconId, badgeStr);
}; };
return function() { return function() {
if ( this.updateBadgeTimer === null ) { if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
this.updateBadgeTimer = setTimeout(updateBadge.bind(this), 500); return;
} }
if ( tabIdToTimer.hasOwnProperty(this.tabId) ) {
return;
}
tabIdToTimer[this.tabId] = setTimeout(updateBadge.bind(null, this.tabId), 500);
}; };
})(); })();