Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/elemHideExceptions.js

Issue 29882558: Issue 6955 - Avoid making copies of common selector list (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Keep track of exceptions per domain Created Sept. 17, 2018, 12:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/elemHide.js ('k') | test/elemHide.js » ('j') | test/elemHide.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHideExceptions.js
===================================================================
--- a/lib/elemHideExceptions.js
+++ b/lib/elemHideExceptions.js
@@ -26,32 +26,39 @@
/**
* Lookup table, lists of element hiding exceptions by selector
* @type {Map.<string,ElemHideException[]>}
*/
let exceptions = new Map();
/**
+ * Number of element hiding exceptions per domain.
+ * @type {Map.<string,number>}
+ */
+let exceptionCountByDomain = new Map();
+
+/**
* Set containing known element exceptions
* @type {Set.<ElemHideException>}
*/
let knownExceptions = new Set();
/**
* Container for element hiding exceptions
* @class
*/
exports.ElemHideExceptions = Object.assign(Object.create(new EventEmitter()), {
/**
* Removes all known exceptions
*/
clear()
{
exceptions.clear();
+ exceptionCountByDomain.clear();
knownExceptions.clear();
filterNotifier.emit("elemhideupdate");
},
/**
* Add a new element hiding exception
* @param {ElemHideException} exception
@@ -63,16 +70,25 @@
let {selector} = exception;
let list = exceptions.get(selector);
if (list)
list.push(exception);
else
exceptions.set(selector, [exception]);
+ for (let [domain] of exception.domains || [])
+ {
+ if (domain == "")
+ continue;
+
+ let count = exceptionCountByDomain.get(domain) || 0;
+ exceptionCountByDomain.set(domain, count + 1);
+ }
+
knownExceptions.add(exception);
this.emit("added", exception);
filterNotifier.emit("elemhideupdate");
},
/**
@@ -84,16 +100,28 @@
if (!knownExceptions.has(exception))
return;
let list = exceptions.get(exception.selector);
let index = list.indexOf(exception);
if (index >= 0)
list.splice(index, 1);
+ for (let [domain] of exception.domains || [])
+ {
+ if (domain == "")
+ continue;
+
+ let count = exceptionCountByDomain.get(domain) || 0;
+ if (count == 1)
+ exceptionCountByDomain.delete(domain);
+ else if (count > 1)
+ exceptionCountByDomain.set(domain, count - 1);
+ }
+
knownExceptions.delete(exception);
this.emit("removed", exception);
filterNotifier.emit("elemhideupdate");
},
/**
@@ -102,16 +130,26 @@
* @returns {boolean}
*/
hasExceptions(selector)
{
return exceptions.has(selector);
},
/**
+ * Checks whether any exception rules are registered for a domain
+ * @param {string} domain
+ * @returns {boolean}
+ */
+ domainHasExceptions(domain)
+ {
+ return exceptionCountByDomain.has(domain);
+ },
+
+ /**
* Checks whether an exception rule is registered for a selector on a
* particular domain.
* @param {string} selector
* @param {?string} docDomain
* @return {?ElemHideException}
*/
getException(selector, docDomain)
{
« no previous file with comments | « lib/elemHide.js ('k') | test/elemHide.js » ('j') | test/elemHide.js » ('J')

Powered by Google App Engine
This is Rietveld