From 6fcad5dc9f1927426d49131d740dff9a39311ff9 Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 29 Apr 2015 12:03:06 -0400 Subject: [PATCH] minor code review re. #150 --- src/js/uritools.js | 2 +- src/lib/publicsuffixlist.js | 39 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/js/uritools.js b/src/js/uritools.js index 59f111728..7c11a1191 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -376,7 +376,7 @@ var domainCacheCount = 0; var domainCacheCountLowWaterMark = 75; var domainCacheCountHighWaterMark = 100; -psl.onModified.addListener(domainCacheReset); +psl.onChanged.addListener(domainCacheReset); /******************************************************************************/ diff --git a/src/lib/publicsuffixlist.js b/src/lib/publicsuffixlist.js index 908f7c4ce..89749867c 100644 --- a/src/lib/publicsuffixlist.js +++ b/src/lib/publicsuffixlist.js @@ -47,7 +47,7 @@ var selfieMagic = 'iscjsfsaolnm'; var cutoffLength = 256; var mustPunycode = /[^a-z0-9.-]/; -var onModifyListeners = []; +var onChangedListeners = []; /******************************************************************************/ @@ -238,8 +238,7 @@ function parse(text, toAscii) { } crystallize(exceptions); crystallize(rules); - - callListeners(onModifyListeners); + callListeners(onChangedListeners); } /******************************************************************************/ @@ -308,12 +307,28 @@ function fromSelfie(selfie) { } rules = selfie.rules; exceptions = selfie.exceptions; - callListeners(onModifyListeners); + callListeners(onChangedListeners); 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) { for ( var i = 0; i < listeners.length; i++ ) { listeners[i](); @@ -322,20 +337,12 @@ var callListeners = function(listeners) { /******************************************************************************/ -var onModified = { +var onChanged = { addListener: function(callback) { - if ( - typeof callback === 'function' && - onModifyListeners.indexOf(callback) === -1 - ) { - onModifyListeners.push(callback); - } + addListener(onChangedListeners, callback); }, removeListener: function(callback) { - var pos = onModifyListeners.indexOf(callback); - if ( pos !== -1 ) { - onModifyListeners.splice(pos, 1); - } + removeListener(onChangedListeners, callback); } }; @@ -352,7 +359,7 @@ root.publicSuffixList = { 'getPublicSuffix': getPublicSuffix, 'toSelfie': toSelfie, 'fromSelfie': fromSelfie, - 'onModified': onModified + 'onChanged': onChanged }; /******************************************************************************/