Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
remove overhead of tab selector update when no change
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed May 18, 2015
1 parent 8063ea1 commit 23aa1e7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ return {
// urls stats are kept on the back burner while waiting to be reactivated
// in a tab or another.
pageStores: {},
pageStoresToken: 0,
pageStoreCemetery: {},

// page url => permission scope
Expand Down
9 changes: 1 addition & 8 deletions src/js/contentscript-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,21 +445,14 @@ var nodeListsAddedHandler = function(nodeLists) {
what: 'contentScriptSummary',
locationURL: window.location.href,
inlineScript: false,
mustReport: true,
title: ''
mustReport: true
};
// https://github.com/gorhill/httpswitchboard/issues/25
// &
// Looks for inline javascript also in at least one a[href] element.
// https://github.com/gorhill/httpswitchboard/issues/131
hasInlineScript(document.querySelectorAll('a[href^="javascript:"],script'), summary);

if ( window === window.top ) {
if ( document.title ) {
summary.title = document.title.trim();
}
}

//console.debug('contentscript-end.js > firstObservationHandler(): found %d script tags in "%s"', Object.keys(summary.scriptSources).length, window.location.href);

localMessager.send(summary);
Expand Down
13 changes: 7 additions & 6 deletions src/js/logger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var lastVarDataIndex = 3; // currently, d0-d3
var maxEntries = 0;
var noTabId = '';
var allTabIds = {};
var allTabIdsToken;

var emphasizeTemplate = document.querySelector('#emphasizeTemplate > span');
var hiddenTemplate = document.querySelector('#hiddenTemplate > span');
Expand Down Expand Up @@ -349,10 +350,6 @@ var renderLogEntries = function(response) {

var synchronizeTabIds = function(newTabIds) {
var oldTabIds = allTabIds;

// Neuter rows for which a tab does not exist anymore
// TODO: sort to avoid using indexOf

var autoDeleteVoidRows = !!vAPI.localStorage.getItem('loggerAutoDeleteVoidRows');
var rowVoided = false;
var trs;
Expand Down Expand Up @@ -445,8 +442,12 @@ var onLogBufferRead = function(response) {
}

// Neuter rows for which a tab does not exist anymore
// TODO: sort to avoid using indexOf
var rowVoided = synchronizeTabIds(response.tabIds);
var rowVoided = false;
if ( response.tabIdsToken !== allTabIdsToken ) {
rowVoided = synchronizeTabIds(response.tabIds);
allTabIdsToken = response.tabIdsToken;
}

renderLogEntries(response);

if ( rowVoided ) {
Expand Down
61 changes: 29 additions & 32 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,14 @@ var contentScriptSummaryHandler = function(tabId, details) {
return;
}

// https://github.com/gorhill/httpswitchboard/issues/25
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
// scripts
if ( details.inlineScript !== true ) {
return;
}

if ( details.title ) {
pageStore.title = details.title;
}

// scripts
if ( details.inlineScript !== true ) {
// https://github.com/gorhill/httpswitchboard/issues/25
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}

Expand Down Expand Up @@ -907,31 +903,32 @@ var onMessage = function(request, sender, callback) {
var response;

switch ( request.what ) {
case 'readMany':
var tabIds = {};
var loggerURL = vAPI.getURL('logger-ui.html');
var pageStore;
for ( var tabId in µm.pageStores ) {
pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
continue;
}
if ( pageStore.rawUrl.lastIndexOf(loggerURL, 0) === 0 ) {
continue;
}
tabIds[tabId] = pageStore.title || pageStore.rawUrl;
case 'readMany':
var tabIds = {};
var loggerURL = vAPI.getURL('logger-ui.html');
var pageStore;
for ( var tabId in µm.pageStores ) {
pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
continue;
}
response = {
colorBlind: false,
entries: µm.logger.readAll(),
maxLoggedRequests: µm.userSettings.maxLoggedRequests,
noTabId: vAPI.noTabId,
tabIds: tabIds
};
break;
if ( pageStore.rawUrl.lastIndexOf(loggerURL, 0) === 0 ) {
continue;
}
tabIds[tabId] = pageStore.title || pageStore.rawUrl;
}
response = {
colorBlind: false,
entries: µm.logger.readAll(),
maxLoggedRequests: µm.userSettings.maxLoggedRequests,
noTabId: vAPI.noTabId,
tabIds: tabIds,
tabIdsToken: µm.pageStoresToken
};
break;

default:
return vAPI.messaging.UNHANDLED;
default:
return vAPI.messaging.UNHANDLED;
}

callback(response);
Expand Down
63 changes: 63 additions & 0 deletions src/js/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ vAPI.tabs.registerListeners();
if ( context === 'updated' && pageStore.pageHostname === tabContext.rootHostname ) {
pageStore.rawURL = tabContext.rawURL;
pageStore.normalURL = normalURL;
this.updateTitle(tabId);
this.pageStoresToken = Date.now();
return pageStore;
}

Expand All @@ -494,6 +496,8 @@ vAPI.tabs.registerListeners();
pageStore = this.PageStore.factory(tabContext);
}
this.pageStores[tabId] = pageStore;
this.updateTitle(tabId);
this.pageStoresToken = Date.now();

// console.debug('tab.js > bindTabToPageStats(): dispatching traffic in tab id %d to page store "%s"', tabId, pageUrl);

Expand All @@ -513,6 +517,7 @@ vAPI.tabs.registerListeners();
return;
}
delete this.pageStores[tabId];
this.pageStoresToken = Date.now();

if ( pageStore.incinerationTimer ) {
clearTimeout(pageStore.incinerationTimer);
Expand Down Expand Up @@ -618,6 +623,64 @@ vAPI.tabs.registerListeners();

/******************************************************************************/

µm.updateTitle = (function() {
var tabIdToTimer = Object.create(null);
var tabIdToTryCount = Object.create(null);
var delay = 499;

var tryNoMore = function(tabId) {
delete tabIdToTryCount[tabId];
};

var tryAgain = function(tabId) {
var count = tabIdToTryCount[tabId];
if ( count === undefined ) {
return false;
}
if ( count === 1 ) {
delete tabIdToTryCount[tabId];
return false;
}
tabIdToTryCount[tabId] = count - 1;
tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(µb, tabId), delay);

This comment has been minimized.

Copy link
@my-password-is-password

my-password-is-password May 21, 2015

Seeing µb is not defined in console

This comment has been minimized.

Copy link
@gorhill

gorhill May 22, 2015

Author Owner

Ugh thanks for spotting this. Must be a rare occurrence, I didn't see an instance triggered on my side all that time.

This comment has been minimized.

Copy link
@my-password-is-password

my-password-is-password May 22, 2015

On stable chrome I don't see this error either but on dev Chrome it shows.

return true;
};

var onTabReady = function(tabId, tab) {
if ( !tab ) {
return tryNoMore(tabId);
}
var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return tryNoMore(tabId);
}
if ( !tab.title && tryAgain(tabId) ) {
return;
}
tryNoMore(tabId);
pageStore.title = tab.title || tab.url || '';
this.pageStoresToken = Date.now();
};

var updateTitle = function(tabId) {
delete tabIdToTimer[tabId];
vAPI.tabs.get(tabId, onTabReady.bind(this, tabId));
};

return function(tabId) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return;
}
if ( tabIdToTimer[tabId] ) {
clearTimeout(tabIdToTimer[tabId]);
}
tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(this, tabId), delay);
tabIdToTryCount[tabId] = 5;
};
})();

/******************************************************************************/

// Stale page store entries janitor
// https://github.com/chrisaljoudi/uBlock/issues/455

Expand Down

0 comments on commit 23aa1e7

Please sign in to comment.