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

Side by Side Diff: lib/filterStorage.js

Issue 29935568: Issue 7096 - Make it possible to lazy initialize a filter's subscriptions (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use new methods in two more places Created Nov. 18, 2018, 4:57 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/filterListener.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 /** 121 /**
122 * The number of subscriptions in the storage. 122 * The number of subscriptions in the storage.
123 * @type {number} 123 * @type {number}
124 */ 124 */
125 get subscriptionCount() 125 get subscriptionCount()
126 { 126 {
127 return this.knownSubscriptions.size; 127 return this.knownSubscriptions.size;
128 } 128 }
129 129
130 /** 130 /**
131 * Yields subscriptions in the storage to which the given filter belongs.
132 * @param {Filter} filter
133 * @yields {Subscription}
134 */
135 *subscriptionsForFilter(filter)
136 {
137 if (!filter.subscriptionsAdded)
138 addSubscriptionsToFilter(filter);
139
140 yield* filter.subscriptions();
141 }
142
143 /**
144 * Returns the number of subscriptions in the storage to which the given
145 * filter belongs.
146 * @param {Filter} filter
147 * @return {number}
148 */
149 getSubscriptionCountForFilter(filter)
150 {
151 if (!filter.subscriptionsAdded)
152 addSubscriptionsToFilter(filter);
153
154 return filter.subscriptionCount;
155 }
156
157 /**
131 * Finds the filter group that a filter should be added to by default. Will 158 * Finds the filter group that a filter should be added to by default. Will
132 * return <code>null</code> if this group doesn't exist yet. 159 * return <code>null</code> if this group doesn't exist yet.
133 * @param {Filter} filter 160 * @param {Filter} filter
134 * @returns {?SpecialSubscription} 161 * @returns {?SpecialSubscription}
135 */ 162 */
136 getGroupForFilter(filter) 163 getGroupForFilter(filter)
137 { 164 {
138 let generalSubscription = null; 165 let generalSubscription = null;
139 for (let subscription of this.knownSubscriptions.values()) 166 for (let subscription of this.knownSubscriptions.values())
140 { 167 {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 * @param {?SpecialSubscription} [subscription] The subscription that the 246 * @param {?SpecialSubscription} [subscription] The subscription that the
220 * filter should be added to. 247 * filter should be added to.
221 * @param {number} [position] The position within the subscription at which 248 * @param {number} [position] The position within the subscription at which
222 * the filter should be added. If not specified, the filter is added at the 249 * the filter should be added. If not specified, the filter is added at the
223 * end of the subscription. 250 * end of the subscription.
224 */ 251 */
225 addFilter(filter, subscription, position) 252 addFilter(filter, subscription, position)
226 { 253 {
227 if (!subscription) 254 if (!subscription)
228 { 255 {
229 for (let currentSubscription of filter.subscriptions()) 256 for (let currentSubscription of this.subscriptionsForFilter(filter))
230 { 257 {
231 if (currentSubscription instanceof SpecialSubscription && 258 if (currentSubscription instanceof SpecialSubscription &&
232 !currentSubscription.disabled) 259 !currentSubscription.disabled)
233 { 260 {
234 return; // No need to add 261 return; // No need to add
235 } 262 }
236 } 263 }
237 subscription = this.getGroupForFilter(filter); 264 subscription = this.getGroupForFilter(filter);
238 } 265 }
239 if (!subscription) 266 if (!subscription)
(...skipping 18 matching lines...) Expand all
258 * @param {?SpecialSubscription} [subscription] The subscription that the 285 * @param {?SpecialSubscription} [subscription] The subscription that the
259 * filter should be removed from. If not specified, the filter will be 286 * filter should be removed from. If not specified, the filter will be
260 * removed from all subscriptions. 287 * removed from all subscriptions.
261 * @param {number} [position] The position within the subscription at which 288 * @param {number} [position] The position within the subscription at which
262 * the filter should be removed. If not specified, all instances of the 289 * the filter should be removed. If not specified, all instances of the
263 * filter will be removed. 290 * filter will be removed.
264 */ 291 */
265 removeFilter(filter, subscription, position) 292 removeFilter(filter, subscription, position)
266 { 293 {
267 let subscriptions = ( 294 let subscriptions = (
268 subscription ? [subscription] : filter.subscriptions() 295 subscription ? [subscription] : this.subscriptionsForFilter(filter)
269 ); 296 );
270 for (let currentSubscription of subscriptions) 297 for (let currentSubscription of subscriptions)
271 { 298 {
272 if (currentSubscription instanceof SpecialSubscription) 299 if (currentSubscription instanceof SpecialSubscription)
273 { 300 {
274 let positions = []; 301 let positions = [];
275 if (typeof position == "undefined") 302 if (typeof position == "undefined")
276 { 303 {
277 let index = -1; 304 let index = -1;
278 do 305 do
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 682
656 /** 683 /**
657 * Reads the user's filters from disk, manages them in memory, and writes them 684 * Reads the user's filters from disk, manages them in memory, and writes them
658 * back to disk. 685 * back to disk.
659 */ 686 */
660 let filterStorage = new FilterStorage(); 687 let filterStorage = new FilterStorage();
661 688
662 exports.filterStorage = filterStorage; 689 exports.filterStorage = filterStorage;
663 690
664 /** 691 /**
692 * Adds to a filter any subscriptions in the storage to which the filter
693 * belongs.
694 * @param {Filter} filter
695 */
696 function addSubscriptionsToFilter(filter)
697 {
698 for (let subscription of filterStorage.subscriptions())
699 {
700 if (subscription.hasFilter(filter))
701 filter.addSubscription(subscription);
702 }
703
704 filter.subscriptionsAdded = true;
705 }
706
707 /**
665 * Connects a subscription to its filters without any notifications. 708 * Connects a subscription to its filters without any notifications.
666 * @param {Subscription} subscription The subscription that should be 709 * @param {Subscription} subscription The subscription that should be
667 * connected to its filters. 710 * connected to its filters.
668 * @param {?Array.<Filter>} [filters] A list of filters to which the 711 * @param {?Array.<Filter>} [filters] A list of filters to which the
669 * subscription should be connected. If this is not given, the subscription 712 * subscription should be connected. If this is not given, the subscription
670 * is connected to its own filters. 713 * is connected to its own filters.
671 */ 714 */
672 function connectSubscriptionFilters(subscription, filters) 715 function connectSubscriptionFilters(subscription, filters)
673 { 716 {
674 if (!filterStorage.knownSubscriptions.has(subscription.url)) 717 if (!filterStorage.knownSubscriptions.has(subscription.url))
(...skipping 21 matching lines...) Expand all
696 } 739 }
697 740
698 /** 741 /**
699 * Clears any in-memory caches held by subscriptions in the storage. 742 * Clears any in-memory caches held by subscriptions in the storage.
700 */ 743 */
701 function clearSubscriptionCaches() 744 function clearSubscriptionCaches()
702 { 745 {
703 for (let subscription of filterStorage.subscriptions()) 746 for (let subscription of filterStorage.subscriptions())
704 subscription.clearCaches(); 747 subscription.clearCaches();
705 } 748 }
OLDNEW
« no previous file with comments | « lib/filterListener.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld