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

Unified Diff: lib/filterStorage.js

Issue 30013628: Issue 7029 - Remove subscriptions property of Filter object (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 24, 2019, 1:30 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
Index: lib/filterStorage.js
===================================================================
--- a/lib/filterStorage.js
+++ b/lib/filterStorage.js
@@ -105,22 +105,36 @@
* @type {string}
*/
get sourceFile()
{
return "patterns.ini";
}
/**
- * Yields all subscriptions in the storage.
+ * Yields subscriptions in the storage.
+ * @param {?string} [filterText] The filter text for which to look. If
+ * specified, the function yields only those subscriptions that contain the
+ * given filter text. By default the function yields all subscriptions.
* @yields {Subscription}
*/
- *subscriptions()
+ *subscriptions(filterText = null)
{
- yield* this.knownSubscriptions.values();
+ if (filterText == null)
+ {
+ yield* this.knownSubscriptions.values();
+ }
+ else
+ {
+ for (let subscription of this.knownSubscriptions.values())
+ {
+ if (subscription.hasFilterText(filterText))
+ yield subscription;
+ }
+ }
}
/**
* The number of subscriptions in the storage.
* @type {number}
*/
get subscriptionCount()
{
@@ -160,32 +174,29 @@
* @param {Subscription} subscription The subscription to be added.
*/
addSubscription(subscription)
{
if (this.knownSubscriptions.has(subscription.url))
return;
this.knownSubscriptions.set(subscription.url, subscription);
- connectSubscriptionFilters(subscription);
filterNotifier.emit("subscription.added", subscription);
}
/**
* Removes a subscription from the storage.
* @param {Subscription} subscription The subscription to be removed.
*/
removeSubscription(subscription)
{
if (!this.knownSubscriptions.has(subscription.url))
return;
- disconnectSubscriptionFilters(subscription);
-
this.knownSubscriptions.delete(subscription.url);
// This should be the last remaining reference to the Subscription
// object.
Subscription.knownSubscriptions.delete(subscription.url);
filterNotifier.emit("subscription.removed", subscription);
}
@@ -193,41 +204,38 @@
/**
* Replaces the list of filters in a subscription with a new list.
* @param {Subscription} subscription The subscription to be updated.
* @param {Array.<string>} filterText The new filter text.
*/
updateSubscriptionFilters(subscription, filterText)
{
let oldFilterText = [...subscription.filterText()];
- disconnectSubscriptionFilters(subscription, oldFilterText);
Manish Jethani 2019/02/26 12:29:57 You might think that looking up the subscriptions
hub 2019/03/08 21:36:19 my default config has 6. Add one language, that ma
Manish Jethani 2019/03/30 20:50:35 Each user-defined filter is not a special subscrip
subscription.clearFilters();
for (let text of filterText)
subscription.addFilterText(text);
- connectSubscriptionFilters(subscription, filterText);
-
filterNotifier.emit("subscription.updated", subscription, oldFilterText);
}
/**
* Adds a user-defined filter to the storage.
* @param {Filter} filter
* @param {?SpecialSubscription} [subscription] The subscription that the
* filter should be added to.
* @param {number} [position] The position within the subscription at which
* the filter should be added. If not specified, the filter is added at the
* end of the subscription.
*/
addFilter(filter, subscription, position)
{
if (!subscription)
{
- for (let currentSubscription of filter.subscriptions())
+ for (let currentSubscription of this.subscriptions(filter.text))
{
if (currentSubscription instanceof SpecialSubscription &&
!currentSubscription.disabled)
{
return; // No need to add
}
}
subscription = this.getGroupForFilter(filter);
@@ -238,17 +246,16 @@
subscription = SpecialSubscription.createForFilter(filter);
this.addSubscription(subscription);
return;
}
if (typeof position == "undefined")
position = subscription.filterCount;
- filter.addSubscription(subscription);
subscription.insertFilterAt(filter, position);
filterNotifier.emit("filter.added", filter, subscription, position);
}
/**
* Removes a user-defined filter from the storage.
* @param {Filter} filter
* @param {?SpecialSubscription} [subscription] The subscription that the
@@ -256,46 +263,44 @@
* removed from all subscriptions.
* @param {number} [position] The position within the subscription at which
* the filter should be removed. If not specified, all instances of the
* filter will be removed.
*/
removeFilter(filter, subscription, position)
{
let subscriptions = (
- subscription ? [subscription] : filter.subscriptions()
+ subscription ? [subscription] : this.subscriptions(filter.text)
);
for (let currentSubscription of subscriptions)
{
if (currentSubscription instanceof SpecialSubscription)
{
let positions = [];
if (typeof position == "undefined")
{
let index = -1;
do
{
- index = currentSubscription.searchFilter(filter, index + 1);
+ index = currentSubscription.findFilterIndex(filter, index + 1);
if (index >= 0)
positions.push(index);
} while (index >= 0);
}
else
positions.push(position);
for (let j = positions.length - 1; j >= 0; j--)
{
let currentPosition = positions[j];
let currentFilterText =
currentSubscription.filterTextAt(currentPosition);
if (currentFilterText && currentFilterText == filter.text)
{
currentSubscription.deleteFilterAt(currentPosition);
- if (currentSubscription.searchFilter(filter) < 0)
- filter.removeSubscription(currentSubscription);
filterNotifier.emit("filter.removed", filter, currentSubscription,
currentPosition);
}
}
}
}
}
@@ -645,42 +650,8 @@
/**
* Reads the user's filters from disk, manages them in memory, and writes them
* back to disk.
*/
let filterStorage = new FilterStorage();
exports.filterStorage = filterStorage;
-
-/**
- * Connects a subscription to its filters without any notifications.
- * @param {Subscription} subscription The subscription that should be
- * connected to its filters.
- * @param {?Array.<string>} [filterText] A list of filters (in text form) to
- * which the subscription should be connected. If this is not given, the
- * subscription is connected to its own filters.
- */
-function connectSubscriptionFilters(subscription, filterText)
-{
- if (!filterStorage.knownSubscriptions.has(subscription.url))
- return;
-
- for (let text of filterText || subscription.filterText())
- Filter.fromText(text).addSubscription(subscription);
-}
-
-/**
- * Disconnects a subscription from its filters without any notifications.
- * @param {Subscription} subscription The subscription that should be
- * disconnected from its filters.
- * @param {?Array.<string>} [filterText] A list of filters (in text form) from
- * which the subscription should be disconnected. If this is not given, the
- * subscription is disconnected from its own filters.
- */
-function disconnectSubscriptionFilters(subscription, filterText)
-{
- if (!filterStorage.knownSubscriptions.has(subscription.url))
- return;
-
- for (let text of filterText || subscription.filterText())
- Filter.fromText(text).removeSubscription(subscription);
-}

Powered by Google App Engine
This is Rietveld