Ensure the "Filter lists" pane is in sync with update status

Related issue:
- https://github.com/gorhill/uBlock/issues/2394

Additionally, I added a new advanced setting to control
how long after launch an auto-update session should be
started -- value is in seconds:

    autoUpdateDelayAfterLaunch 180
This commit is contained in:
Raymond Hill 2019-05-19 18:31:12 -04:00
parent a0ac1b7ee8
commit 72d9758faa
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
5 changed files with 62 additions and 45 deletions

View file

@ -299,6 +299,10 @@ const renderFilterLists = function(soft) {
if ( !soft ) { if ( !soft ) {
filteringSettingsHash = hashFromCurrentFromSettings(); filteringSettingsHash = hashFromCurrentFromSettings();
} }
// https://github.com/gorhill/uBlock/issues/2394
document.body.classList.toggle('updating', listDetails.isUpdating);
renderWidgets(); renderWidgets();
}; };

View file

@ -916,15 +916,16 @@ api.rmrf = function() {
/******************************************************************************/ /******************************************************************************/
// Asset updater area. // Asset updater area.
var updaterStatus, const updaterAssetDelayDefault = 120000;
const updaterUpdated = [];
const updaterFetched = new Set();
let updaterStatus,
updaterTimer, updaterTimer,
updaterAssetDelayDefault = 120000,
updaterAssetDelay = updaterAssetDelayDefault, updaterAssetDelay = updaterAssetDelayDefault,
updaterUpdated = [],
updaterFetched = new Set(),
noRemoteResources; noRemoteResources;
var updateFirst = function() { const updateFirst = function() {
// https://github.com/gorhill/uBlock/commit/126110c9a0a0630cd556f5cb215422296a961029 // https://github.com/gorhill/uBlock/commit/126110c9a0a0630cd556f5cb215422296a961029
// Firefox extension reviewers do not want uBO/webext to fetch its own // Firefox extension reviewers do not want uBO/webext to fetch its own
// scriptlets/resources asset from the project's own repo (github.com). // scriptlets/resources asset from the project's own repo (github.com).
@ -939,31 +940,33 @@ var updateFirst = function() {
} }
updaterStatus = 'updating'; updaterStatus = 'updating';
updaterFetched.clear(); updaterFetched.clear();
updaterUpdated = []; updaterUpdated.length = 0;
fireNotification('before-assets-updated'); fireNotification('before-assets-updated');
updateNext(); updateNext();
}; };
var updateNext = function() { const updateNext = function() {
var assetDict, cacheDict; let assetDict, cacheDict;
// This will remove a cached asset when it's no longer in use. // This will remove a cached asset when it's no longer in use.
var garbageCollectOne = function(assetKey) { const garbageCollectOne = function(assetKey) {
var cacheEntry = cacheDict[assetKey]; const cacheEntry = cacheDict[assetKey];
if ( cacheEntry && cacheEntry.readTime < assetCacheRegistryStartTime ) { if ( cacheEntry && cacheEntry.readTime < assetCacheRegistryStartTime ) {
assetCacheRemove(assetKey); assetCacheRemove(assetKey);
} }
}; };
var findOne = function() { const findOne = function() {
var now = Date.now(), const now = Date.now();
assetEntry, cacheEntry; for ( const assetKey in assetDict ) {
for ( var assetKey in assetDict ) { const assetEntry = assetDict[assetKey];
assetEntry = assetDict[assetKey];
if ( assetEntry.hasRemoteURL !== true ) { continue; } if ( assetEntry.hasRemoteURL !== true ) { continue; }
if ( updaterFetched.has(assetKey) ) { continue; } if ( updaterFetched.has(assetKey) ) { continue; }
cacheEntry = cacheDict[assetKey]; const cacheEntry = cacheDict[assetKey];
if ( cacheEntry && (cacheEntry.writeTime + assetEntry.updateAfter * 86400000) > now ) { if (
cacheEntry &&
(cacheEntry.writeTime + assetEntry.updateAfter * 86400000) > now
) {
continue; continue;
} }
// Update of user scripts/resources forbidden? // Update of user scripts/resources forbidden?
@ -982,7 +985,7 @@ var updateNext = function() {
} }
}; };
var updatedOne = function(details) { const updatedOne = function(details) {
if ( details.content !== '' ) { if ( details.content !== '' ) {
updaterUpdated.push(details.assetKey); updaterUpdated.push(details.assetKey);
if ( details.assetKey === 'assets.json' ) { if ( details.assetKey === 'assets.json' ) {
@ -998,8 +1001,8 @@ var updateNext = function() {
} }
}; };
var updateOne = function() { const updateOne = function() {
var assetKey = findOne(); const assetKey = findOne();
if ( assetKey === undefined ) { if ( assetKey === undefined ) {
return updateDone(); return updateDone();
} }
@ -1020,20 +1023,20 @@ var updateNext = function() {
}); });
}; };
var updateDone = function() { const updateDone = function() {
var assetKeys = updaterUpdated.slice(0); const assetKeys = updaterUpdated.slice(0);
updaterFetched.clear(); updaterFetched.clear();
updaterUpdated = []; updaterUpdated.length = 0;
updaterStatus = undefined; updaterStatus = undefined;
updaterAssetDelay = updaterAssetDelayDefault; updaterAssetDelay = updaterAssetDelayDefault;
fireNotification('after-assets-updated', { assetKeys: assetKeys }); fireNotification('after-assets-updated', { assetKeys: assetKeys });
}; };
api.updateStart = function(details) { api.updateStart = function(details) {
var oldUpdateDelay = updaterAssetDelay, const oldUpdateDelay = updaterAssetDelay;
newUpdateDelay = typeof details.delay === 'number' ? const newUpdateDelay = typeof details.delay === 'number' ?
details.delay : details.delay :
updaterAssetDelayDefault; updaterAssetDelayDefault;
updaterAssetDelay = Math.min(oldUpdateDelay, newUpdateDelay); updaterAssetDelay = Math.min(oldUpdateDelay, newUpdateDelay);
if ( updaterStatus !== undefined ) { if ( updaterStatus !== undefined ) {
if ( newUpdateDelay < oldUpdateDelay ) { if ( newUpdateDelay < oldUpdateDelay ) {
@ -1055,6 +1058,11 @@ api.updateStop = function() {
} }
}; };
api.isUpdating = function() {
return updaterStatus === 'updating' &&
updaterAssetDelay <= µBlock.hiddenSettings.manualUpdateAssetFetchPeriod;
};
/******************************************************************************/ /******************************************************************************/
return api; return api;

View file

@ -40,6 +40,7 @@ const µBlock = (function() { // jshint ignore:line
assetFetchTimeout: 30, assetFetchTimeout: 30,
autoCommentFilterTemplate: '{{date}} {{origin}}', autoCommentFilterTemplate: '{{date}} {{origin}}',
autoUpdateAssetFetchPeriod: 120, autoUpdateAssetFetchPeriod: 120,
autoUpdateDelayAfterLaunch: 180,
autoUpdatePeriod: 7, autoUpdatePeriod: 7,
cacheStorageAPI: 'unset', cacheStorageAPI: 'unset',
cacheStorageCompression: true, cacheStorageCompression: true,

View file

@ -862,24 +862,23 @@ const resetUserData = function() {
// 3rd-party filters // 3rd-party filters
var prepListEntries = function(entries) { const prepListEntries = function(entries) {
var µburi = µb.URI; const µburi = µb.URI;
var entry, hn; for ( const k in entries ) {
for ( var k in entries ) {
if ( entries.hasOwnProperty(k) === false ) { continue; } if ( entries.hasOwnProperty(k) === false ) { continue; }
entry = entries[k]; const entry = entries[k];
if ( typeof entry.supportURL === 'string' && entry.supportURL !== '' ) { if ( typeof entry.supportURL === 'string' && entry.supportURL !== '' ) {
entry.supportName = µburi.hostnameFromURI(entry.supportURL); entry.supportName = µburi.hostnameFromURI(entry.supportURL);
} else if ( typeof entry.homeURL === 'string' && entry.homeURL !== '' ) { } else if ( typeof entry.homeURL === 'string' && entry.homeURL !== '' ) {
hn = µburi.hostnameFromURI(entry.homeURL); const hn = µburi.hostnameFromURI(entry.homeURL);
entry.supportURL = 'http://' + hn + '/'; entry.supportURL = `http://${hn}/`;
entry.supportName = µburi.domainFromHostname(hn); entry.supportName = µburi.domainFromHostname(hn);
} }
} }
}; };
var getLists = function(callback) { const getLists = function(callback) {
var r = { const r = {
autoUpdate: µb.userSettings.autoUpdate, autoUpdate: µb.userSettings.autoUpdate,
available: null, available: null,
cache: null, cache: null,
@ -887,16 +886,17 @@ var getLists = function(callback) {
current: µb.availableFilterLists, current: µb.availableFilterLists,
externalLists: µb.userSettings.externalLists, externalLists: µb.userSettings.externalLists,
ignoreGenericCosmeticFilters: µb.userSettings.ignoreGenericCosmeticFilters, ignoreGenericCosmeticFilters: µb.userSettings.ignoreGenericCosmeticFilters,
isUpdating: µb.assets.isUpdating(),
netFilterCount: µb.staticNetFilteringEngine.getFilterCount(), netFilterCount: µb.staticNetFilteringEngine.getFilterCount(),
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters, parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
userFiltersPath: µb.userFiltersPath userFiltersPath: µb.userFiltersPath
}; };
var onMetadataReady = function(entries) { const onMetadataReady = function(entries) {
r.cache = entries; r.cache = entries;
prepListEntries(r.cache); prepListEntries(r.cache);
callback(r); callback(r);
}; };
var onLists = function(lists) { const onLists = function(lists) {
r.available = lists; r.available = lists;
prepListEntries(r.available); prepListEntries(r.available);
µb.assets.metadata(onMetadataReady); µb.assets.metadata(onMetadataReady);
@ -908,7 +908,7 @@ var getLists = function(callback) {
// My rules // My rules
var getRules = function() { const getRules = function() {
return { return {
permanentRules: permanentRules:
µb.permanentFirewall.toArray().concat( µb.permanentFirewall.toArray().concat(
@ -923,7 +923,7 @@ var getRules = function() {
}; };
}; };
var modifyRuleset = function(details) { const modifyRuleset = function(details) {
let swRuleset, hnRuleset, urlRuleset; let swRuleset, hnRuleset, urlRuleset;
if ( details.permanent ) { if ( details.permanent ) {
swRuleset = µb.permanentSwitches; swRuleset = µb.permanentSwitches;
@ -974,7 +974,7 @@ var modifyRuleset = function(details) {
// Shortcuts pane // Shortcuts pane
let getShortcuts = function(callback) { const getShortcuts = function(callback) {
if ( µb.canUseShortcuts === false ) { if ( µb.canUseShortcuts === false ) {
return callback([]); return callback([]);
} }
@ -995,7 +995,7 @@ let getShortcuts = function(callback) {
}); });
}; };
let setShortcut = function(details) { const setShortcut = function(details) {
if ( µb.canUpdateShortcuts === false ) { return; } if ( µb.canUpdateShortcuts === false ) { return; }
if ( details.shortcut === undefined ) { if ( details.shortcut === undefined ) {
vAPI.commands.reset(details.name); vAPI.commands.reset(details.name);
@ -1009,7 +1009,7 @@ let setShortcut = function(details) {
/******************************************************************************/ /******************************************************************************/
var onMessage = function(request, sender, callback) { const onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'backupUserData': case 'backupUserData':

View file

@ -65,9 +65,13 @@ var onAllReady = function() {
initializeTabs(); initializeTabs();
// https://github.com/chrisaljoudi/uBlock/issues/184 // https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future. // Check for updates not too far in the future.
µb.assets.addObserver(µb.assetObserver.bind(µb)); µb.assets.addObserver(µb.assetObserver.bind(µb));
µb.scheduleAssetUpdater(µb.userSettings.autoUpdate ? 5 * 60 * 1000 : 0); µb.scheduleAssetUpdater(
µb.userSettings.autoUpdate
? µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000
: 0
);
// vAPI.cloud is optional. // vAPI.cloud is optional.
if ( µb.cloudStorageSupported ) { if ( µb.cloudStorageSupported ) {