Index: lib/filterStorage.js |
=================================================================== |
--- a/lib/filterStorage.js |
+++ b/lib/filterStorage.js |
@@ -123,16 +123,43 @@ |
* @type {number} |
*/ |
get subscriptionCount() |
{ |
return this.knownSubscriptions.size; |
} |
/** |
+ * Yields subscriptions in the storage to which the given filter belongs. |
+ * @param {Filter} filter |
+ * @yields {Subscription} |
+ */ |
+ *subscriptionsForFilter(filter) |
+ { |
+ if (!filter.subscriptionsAdded) |
+ addSubscriptionsToFilter(filter); |
+ |
+ yield* filter.subscriptions(); |
+ } |
+ |
+ /** |
+ * Returns the number of subscriptions in the storage to which the given |
+ * filter belongs. |
+ * @param {Filter} filter |
+ * @return {number} |
+ */ |
+ getSubscriptionCountForFilter(filter) |
+ { |
+ if (!filter.subscriptionsAdded) |
+ addSubscriptionsToFilter(filter); |
+ |
+ return filter.subscriptionCount; |
+ } |
+ |
+ /** |
* Finds the filter group that a filter should be added to by default. Will |
* return <code>null</code> if this group doesn't exist yet. |
* @param {Filter} filter |
* @returns {?SpecialSubscription} |
*/ |
getGroupForFilter(filter) |
{ |
let generalSubscription = null; |
@@ -221,17 +248,17 @@ |
* @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.subscriptionsForFilter(filter)) |
{ |
if (currentSubscription instanceof SpecialSubscription && |
!currentSubscription.disabled) |
{ |
return; // No need to add |
} |
} |
subscription = this.getGroupForFilter(filter); |
@@ -260,17 +287,17 @@ |
* 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.subscriptionsForFilter(filter) |
); |
for (let currentSubscription of subscriptions) |
{ |
if (currentSubscription instanceof SpecialSubscription) |
{ |
let positions = []; |
if (typeof position == "undefined") |
{ |
@@ -657,16 +684,32 @@ |
* Reads the user's filters from disk, manages them in memory, and writes them |
* back to disk. |
*/ |
let filterStorage = new FilterStorage(); |
exports.filterStorage = filterStorage; |
/** |
+ * Adds to a filter any subscriptions in the storage to which the filter |
+ * belongs. |
+ * @param {Filter} filter |
+ */ |
+function addSubscriptionsToFilter(filter) |
+{ |
+ for (let subscription of filterStorage.subscriptions()) |
+ { |
+ if (subscription.hasFilter(filter)) |
+ filter.addSubscription(subscription); |
+ } |
+ |
+ filter.subscriptionsAdded = true; |
+} |
+ |
+/** |
* Connects a subscription to its filters without any notifications. |
* @param {Subscription} subscription The subscription that should be |
* connected to its filters. |
* @param {?Array.<Filter>} [filters] A list of filters to which the |
* subscription should be connected. If this is not given, the subscription |
* is connected to its own filters. |
*/ |
function connectSubscriptionFilters(subscription, filters) |