minor code review re. #150

This commit is contained in:
gorhill 2015-04-29 12:03:06 -04:00
parent 52b2868c74
commit 6fcad5dc9f
2 changed files with 24 additions and 17 deletions

View File

@ -376,7 +376,7 @@ var domainCacheCount = 0;
var domainCacheCountLowWaterMark = 75; var domainCacheCountLowWaterMark = 75;
var domainCacheCountHighWaterMark = 100; var domainCacheCountHighWaterMark = 100;
psl.onModified.addListener(domainCacheReset); psl.onChanged.addListener(domainCacheReset);
/******************************************************************************/ /******************************************************************************/

View File

@ -47,7 +47,7 @@ var selfieMagic = 'iscjsfsaolnm';
var cutoffLength = 256; var cutoffLength = 256;
var mustPunycode = /[^a-z0-9.-]/; var mustPunycode = /[^a-z0-9.-]/;
var onModifyListeners = []; var onChangedListeners = [];
/******************************************************************************/ /******************************************************************************/
@ -238,8 +238,7 @@ function parse(text, toAscii) {
} }
crystallize(exceptions); crystallize(exceptions);
crystallize(rules); crystallize(rules);
callListeners(onChangedListeners);
callListeners(onModifyListeners);
} }
/******************************************************************************/ /******************************************************************************/
@ -308,12 +307,28 @@ function fromSelfie(selfie) {
} }
rules = selfie.rules; rules = selfie.rules;
exceptions = selfie.exceptions; exceptions = selfie.exceptions;
callListeners(onModifyListeners); callListeners(onChangedListeners);
return true; return true;
} }
/******************************************************************************/ /******************************************************************************/
var addListener = function(listeners, callback) {
if ( typeof callback !== 'function' ) {
return;
}
if ( listeners.indexOf(callback) === -1 ) {
listeners.push(callback);
}
};
var removeListener = function(listeners, callback) {
var pos = listeners.indexOf(callback);
if ( pos !== -1 ) {
listeners.splice(pos, 1);
}
};
var callListeners = function(listeners) { var callListeners = function(listeners) {
for ( var i = 0; i < listeners.length; i++ ) { for ( var i = 0; i < listeners.length; i++ ) {
listeners[i](); listeners[i]();
@ -322,20 +337,12 @@ var callListeners = function(listeners) {
/******************************************************************************/ /******************************************************************************/
var onModified = { var onChanged = {
addListener: function(callback) { addListener: function(callback) {
if ( addListener(onChangedListeners, callback);
typeof callback === 'function' &&
onModifyListeners.indexOf(callback) === -1
) {
onModifyListeners.push(callback);
}
}, },
removeListener: function(callback) { removeListener: function(callback) {
var pos = onModifyListeners.indexOf(callback); removeListener(onChangedListeners, callback);
if ( pos !== -1 ) {
onModifyListeners.splice(pos, 1);
}
} }
}; };
@ -352,7 +359,7 @@ root.publicSuffixList = {
'getPublicSuffix': getPublicSuffix, 'getPublicSuffix': getPublicSuffix,
'toSelfie': toSelfie, 'toSelfie': toSelfie,
'fromSelfie': fromSelfie, 'fromSelfie': fromSelfie,
'onModified': onModified 'onChanged': onChanged
}; };
/******************************************************************************/ /******************************************************************************/