Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor rules.js to use tree lookups.
  • Loading branch information
cowlicks committed Sep 12, 2017
commit b8ea7a3e417c0b18bf02f8e850bb29e2a32cfed2
39 changes: 19 additions & 20 deletions chromium/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ function RuleSets(ruleActiveStates) {


RuleSets.prototype = {
_pushTarget: function(target, rule_set) {
this.targets.setNode(target, node => {
if (typeof node.data == 'undefined') {
node.data = [];
}
node.data.push(rule_set);
});
},

/**
* Iterate through data XML and load rulesets
*/
Expand Down Expand Up @@ -274,10 +283,7 @@ RuleSets.prototype = {
var targets = ruletag["target"];
for (let target of targets) {
if (target != null) {
if (!this.targets.has(target)) {
this.targets.set(target, []);
}
this.targets.get(target).push(rule_set);
this._pushTarget(target, rule_set);
}
}
},
Expand All @@ -292,12 +298,10 @@ RuleSets.prototype = {
var new_rule_set = new RuleSet(params.host, true, "user rule");
var new_rule = new Rule(params.urlMatcher, params.redirectTo);
new_rule_set.rules.push(new_rule);
if (!this.targets.has(params.host)) {
this.targets.set(params.host, []);
}
this.ruleCache.delete(params.host);
// TODO: maybe promote this rule?
this.targets.get(params.host).push(new_rule_set);

this._pushTarget(params.host, new_rule_set);

if (new_rule_set.name in this.ruleActiveStates) {
new_rule_set.active = (this.ruleActiveStates[new_rule_set.name] == "true");
}
Expand All @@ -314,13 +318,11 @@ RuleSets.prototype = {
log(INFO, 'removing user rule for ' + JSON.stringify(ruleset));
this.ruleCache.delete(ruleset.name);


var tmp = this.targets.get(ruleset.name).filter(r =>
!(r.isEquivalentTo(ruleset))
);
this.targets.set(ruleset.name, tmp);

if (this.targets.get(ruleset.name).length == 0) {
let len = this.targets.getNode(ruleset.name, node => {
node.data = node.data.filter(r => !(r.isEquivalentTo(ruleset)));
return node.data.len;
});
if (len == 0) {
this.targets.delete(ruleset.name);
}

Expand Down Expand Up @@ -389,10 +391,7 @@ RuleSets.prototype = {
var targets = ruletag.getElementsByTagName("target");
for (let target of targets) {
var host = target.getAttribute("host");
if (!this.targets.has(host)) {
this.targets.set(host, []);
}
this.targets.get(host).push(rule_set);
this._pushTarget(host, rule_set);
}
},

Expand Down