Coalesce tab reloads in burst "relax blocking mode" ops

Quickly firing "Relax blocking mode" commands will
cause the tab to reload only once.
This commit is contained in:
Raymond Hill 2019-09-08 12:52:28 -04:00
parent ad0315a4cd
commit 4792e0e291
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 77 additions and 59 deletions

View File

@ -38,7 +38,10 @@
if ( µBlock.canUseShortcuts === false ) { return; }
const relaxBlockingMode = function(tab) {
const relaxBlockingMode = (( ) => {
const reloadTimers = new Map();
return function(tab) {
if ( tab instanceof Object === false || tab.id <= 0 ) { return; }
const µb = µBlock;
@ -105,10 +108,25 @@ const relaxBlockingMode = function(tab) {
}
}
if ( newProfileBits & 0b00000001 ) {
vAPI.tabs.reload(tab.id);
// Reload the target tab?
if ( (newProfileBits & 0b00000001) === 0 ) { return; }
// Reload: use a timer to coalesce bursts of reload commands.
let timer = reloadTimers.get(tab.id);
if ( timer !== undefined ) {
clearTimeout(timer);
}
timer = vAPI.setTimeout(
tabId => {
reloadTimers.delete(tabId);
vAPI.tabs.reload(tabId);
},
547,
tab.id
);
reloadTimers.set(tab.id, timer);
};
})();
vAPI.commands.onCommand.addListener(command => {
const µb = µBlock;