Work toward modernizing code base: promisification

Also, coallesce calls to selfieManager.destroy() so as
to avoid undue repeated calls to storage deletion of
selfie assets.

Related commit:
- e27328f931
This commit is contained in:
Raymond Hill 2019-09-15 09:36:50 -04:00
parent 55cc0c6997
commit 915687fddb
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 76 additions and 77 deletions

View File

@ -829,19 +829,25 @@ const backupUserData = async function() {
return { localData, userData }; return { localData, userData };
}; };
const restoreUserData = function(request) { const restoreUserData = async function(request) {
const userData = request.userData; const userData = request.userData;
const restart = function() { // https://github.com/chrisaljoudi/uBlock/issues/1102
vAPI.app.restart(); // Ensure all currently cached assets are flushed from storage AND memory.
}; µb.assets.rmrf();
let countdown = 2; // If we are going to restore all, might as well wipe out clean local
// storages
vAPI.localStorage.removeItem('immediateHiddenSettings');
await Promise.all([
µb.cacheStorage.clear(),
vAPI.storage.clear(),
]);
const onAllRemoved = function() { // Restore block stats
countdown -= 1;
if ( countdown !== 0 ) { return; }
µBlock.saveLocalSettings(); µBlock.saveLocalSettings();
// Restore user data
vAPI.storage.set(userData.userSettings); vAPI.storage.set(userData.userSettings);
let hiddenSettings = userData.hiddenSettings; let hiddenSettings = userData.hiddenSettings;
if ( hiddenSettings instanceof Object === false ) { if ( hiddenSettings instanceof Object === false ) {
@ -872,21 +878,10 @@ const restoreUserData = function(request) {
}); });
µb.saveUserFilters(userData.userFilters); µb.saveUserFilters(userData.userFilters);
if ( Array.isArray(userData.selectedFilterLists) ) { if ( Array.isArray(userData.selectedFilterLists) ) {
µb.saveSelectedFilterLists(userData.selectedFilterLists, restart); await µb.saveSelectedFilterLists(userData.selectedFilterLists);
} else {
restart();
} }
};
// https://github.com/chrisaljoudi/uBlock/issues/1102 vAPI.app.restart();
// Ensure all currently cached assets are flushed from storage AND memory.
µb.assets.rmrf();
// If we are going to restore all, might as well wipe out clean local
// storage
µb.cacheStorage.clear().then(( ) => { onAllRemoved(); });
vAPI.storage.clear(onAllRemoved);
vAPI.localStorage.removeItem('immediateHiddenSettings');
}; };
// Remove all stored data but keep global counts, people can become // Remove all stored data but keep global counts, people can become

View File

@ -120,7 +120,7 @@
// This way the new default values in the future will properly apply for those // This way the new default values in the future will properly apply for those
// which were not modified by the user. // which were not modified by the user.
µBlock.saveHiddenSettings = function(callback) { µBlock.saveHiddenSettings = function() {
const bin = { hiddenSettings: {} }; const bin = { hiddenSettings: {} };
for ( const prop in this.hiddenSettings ) { for ( const prop in this.hiddenSettings ) {
if ( if (
@ -130,7 +130,7 @@
bin.hiddenSettings[prop] = this.hiddenSettings[prop]; bin.hiddenSettings[prop] = this.hiddenSettings[prop];
} }
} }
vAPI.storage.set(bin, callback); vAPI.storage.set(bin);
this.saveImmediateHiddenSettings(); this.saveImmediateHiddenSettings();
self.log.verbosity = this.hiddenSettings.consoleLogLevel; self.log.verbosity = this.hiddenSettings.consoleLogLevel;
}; };
@ -260,11 +260,7 @@
this.saveSelectedFilterLists(this.autoSelectRegionalFilterLists(lists)); this.saveSelectedFilterLists(this.autoSelectRegionalFilterLists(lists));
}; };
µBlock.saveSelectedFilterLists = function(newKeys, append, callback) { µBlock.saveSelectedFilterLists = function(newKeys, append = false) {
if ( typeof append === 'function' ) {
callback = append;
append = false;
}
const oldKeys = this.selectedFilterLists.slice(); const oldKeys = this.selectedFilterLists.slice();
if ( append ) { if ( append ) {
newKeys = newKeys.concat(oldKeys); newKeys = newKeys.concat(oldKeys);
@ -278,12 +274,12 @@
} }
newKeys = Array.from(newSet); newKeys = Array.from(newSet);
this.selectedFilterLists = newKeys; this.selectedFilterLists = newKeys;
vAPI.storage.set({ selectedFilterLists: newKeys }, callback); return vAPI.storage.set({ selectedFilterLists: newKeys });
}; };
/******************************************************************************/ /******************************************************************************/
µBlock.applyFilterListSelection = function(details, callback) { µBlock.applyFilterListSelection = function(details) {
let selectedListKeySet = new Set(this.selectedFilterLists); let selectedListKeySet = new Set(this.selectedFilterLists);
let externalLists = this.userSettings.externalLists; let externalLists = this.userSettings.externalLists;
@ -360,9 +356,6 @@
vAPI.storage.set({ externalLists: externalLists }); vAPI.storage.set({ externalLists: externalLists });
} }
this.saveSelectedFilterLists(result); this.saveSelectedFilterLists(result);
if ( typeof callback === 'function' ) {
callback(result);
}
}; };
/******************************************************************************/ /******************************************************************************/
@ -1032,16 +1025,17 @@
// be generated if the user doesn't change his filter lists selection for // be generated if the user doesn't change his filter lists selection for
// some set time. // some set time.
µBlock.selfieManager = (function() { µBlock.selfieManager = (( ) => {
const µb = µBlock; const µb = µBlock;
let timer; let createTimer;
let destroyTimer;
// As of 2018-05-31: // As of 2018-05-31:
// JSON.stringify-ing ourselves results in a better baseline // JSON.stringify-ing ourselves results in a better baseline
// memory usage at selfie-load time. For some reasons. // memory usage at selfie-load time. For some reasons.
const create = function() { const create = async function() {
Promise.all([ await Promise.all([
µb.assets.put( µb.assets.put(
'selfie/main', 'selfie/main',
JSON.stringify({ JSON.stringify({
@ -1052,9 +1046,8 @@
µb.redirectEngine.toSelfie('selfie/redirectEngine'), µb.redirectEngine.toSelfie('selfie/redirectEngine'),
µb.staticExtFilteringEngine.toSelfie('selfie/staticExtFilteringEngine'), µb.staticExtFilteringEngine.toSelfie('selfie/staticExtFilteringEngine'),
µb.staticNetFilteringEngine.toSelfie('selfie/staticNetFilteringEngine'), µb.staticNetFilteringEngine.toSelfie('selfie/staticNetFilteringEngine'),
]).then(( ) => { ]);
µb.lz4Codec.relinquish(); µb.lz4Codec.relinquish();
});
}; };
const load = async function() { const load = async function() {
@ -1096,19 +1089,30 @@
}; };
const destroy = function() { const destroy = function() {
if ( timer !== undefined ) {
clearTimeout(timer);
timer = undefined;
}
µb.cacheStorage.remove('selfie'); // TODO: obsolete, remove eventually. µb.cacheStorage.remove('selfie'); // TODO: obsolete, remove eventually.
µb.assets.remove(/^selfie\//); µb.assets.remove(/^selfie\//);
timer = vAPI.setTimeout(( ) => { createTimer = vAPI.setTimeout(( ) => {
timer = undefined; createTimer = undefined;
create(); create();
}, µb.hiddenSettings.selfieAfter * 60000); }, µb.hiddenSettings.selfieAfter * 60000);
}; };
return { load, destroy }; const destroyAsync = function() {
if ( destroyTimer !== undefined ) { return; }
if ( createTimer !== undefined ) {
clearTimeout(createTimer);
createTimer = undefined;
}
destroyTimer = vAPI.setTimeout(
( ) => {
destroyTimer = undefined;
destroy();
},
1019
);
};
return { load, destroy: destroyAsync };
})(); })();
/******************************************************************************/ /******************************************************************************/