code review: simplify PSL selfie generator

pull/2/head
Raymond Hill 7 years ago
parent c2a46c5da4
commit b963ccb778
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

@ -98,7 +98,7 @@ var onPSLReady = function() {
/******************************************************************************/ /******************************************************************************/
processCallbackQueue(µm.onBeforeStartQueue, function() { processCallbackQueue(µm.onBeforeStartQueue, function() {
µm.loadPublicSuffixList(onPSLReady); µm.publicSuffixList.load(onPSLReady);
}); });
/******************************************************************************/ /******************************************************************************/

@ -733,68 +733,47 @@
/******************************************************************************/ /******************************************************************************/
µMatrix.loadPublicSuffixList = function(callback) { µMatrix.publicSuffixList = (function() {
let µm = this; let µm = µMatrix;
if ( typeof callback !== 'function' ) { var onPSLReady = function(details, callback) {
callback = this.noopFunc; if (
} !details.error &&
typeof details.content === 'string' &&
var applyPublicSuffixList = function(details) { details.content.length !== 0
if ( !details.error ) { ) {
publicSuffixList.parse(details.content, punycode.toASCII); publicSuffixList.parse(details.content, punycode.toASCII);
µm.publicSuffixListSelfie.create(); vAPI.cacheStorage.set({
publicSuffixListSelfie: publicSuffixList.toSelfie()
});
} }
callback(); callback();
}; };
let onSelfieReady = function(status) { let onSelfieReady = function(bin, callback) {
if ( status === true ) { if (
bin instanceof Object &&
bin.publicSuffixListSelfie instanceof Object &&
publicSuffixList.fromSelfie(bin.publicSuffixListSelfie)
) {
return callback(); return callback();
} }
µm.assets.get(µm.pslAssetKey, applyPublicSuffixList); µm.assets.get(µm.pslAssetKey, function(details) {
onPSLReady(details, callback);
});
}; };
this.publicSuffixListSelfie.load(onSelfieReady);
};
/******************************************************************************/
µMatrix.publicSuffixListSelfie = (function() {
let timer;
return { return {
create: function() { update: function(details) {
this.cancel(); onPSLReady(details, µm.noopFunc);
timer = vAPI.setTimeout(
function() {
timer = undefined;
vAPI.cacheStorage.set({
publicSuffixListSelfie: publicSuffixList.toSelfie()
});
},
60000
);
},
destroy: function() {
this.cancel();
vAPI.cacheStorage.remove('publicSuffixListSelfie');
}, },
load: function(callback) { load: function(callback) {
this.cancel(); if ( typeof callback !== 'function' ) {
callback = µm.noopFunc;
}
vAPI.cacheStorage.get('publicSuffixListSelfie', function(bin) { vAPI.cacheStorage.get('publicSuffixListSelfie', function(bin) {
callback( onSelfieReady(bin, callback);
bin instanceof Object &&
bin.publicSuffixListSelfie instanceof Object &&
publicSuffixList.fromSelfie(bin.publicSuffixListSelfie)
);
}); });
},
cancel: function() {
if ( timer !== undefined ) {
clearTimeout(timer);
}
timer = undefined;
} }
}; };
})(); })();
@ -854,7 +833,7 @@
) { ) {
this.hostsFilesSelfie.destroy(); this.hostsFilesSelfie.destroy();
} else if ( details.assetKey === this.pslAssetKey ) { } else if ( details.assetKey === this.pslAssetKey ) {
this.publicSuffixListSelfie.destroy(); this.publicSuffixList.update(details);
} }
vAPI.messaging.broadcast({ vAPI.messaging.broadcast({
what: 'assetUpdated', what: 'assetUpdated',

Loading…
Cancel
Save