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

Side by Side Diff: lib/filterStorage.js

Issue 29870577: Issue 6916 - Encapsulate filter subscriptions (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Sept. 1, 2018, 4:53 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') | lib/iniParser.js » ('j') | 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 * @param {Filter} filter 209 * @param {Filter} filter
210 * @param {SpecialSubscription} [subscription] 210 * @param {SpecialSubscription} [subscription]
211 * particular group that the filter should be added to 211 * particular group that the filter should be added to
212 * @param {number} [position] 212 * @param {number} [position]
213 * position within the subscription at which the filter should be added 213 * position within the subscription at which the filter should be added
214 */ 214 */
215 addFilter(filter, subscription, position) 215 addFilter(filter, subscription, position)
216 { 216 {
217 if (!subscription) 217 if (!subscription)
218 { 218 {
219 for (let currentSubscription of filter.subscriptions) 219 for (let currentSubscription of filter.subscriptions())
220 { 220 {
221 if (currentSubscription instanceof SpecialSubscription && 221 if (currentSubscription instanceof SpecialSubscription &&
222 !currentSubscription.disabled) 222 !currentSubscription.disabled)
223 { 223 {
224 return; // No need to add 224 return; // No need to add
225 } 225 }
226 } 226 }
227 subscription = FilterStorage.getGroupForFilter(filter); 227 subscription = FilterStorage.getGroupForFilter(filter);
228 } 228 }
229 if (!subscription) 229 if (!subscription)
230 { 230 {
231 // No group for this filter exists, create one 231 // No group for this filter exists, create one
232 subscription = SpecialSubscription.createForFilter(filter); 232 subscription = SpecialSubscription.createForFilter(filter);
233 this.addSubscription(subscription); 233 this.addSubscription(subscription);
234 return; 234 return;
235 } 235 }
236 236
237 if (typeof position == "undefined") 237 if (typeof position == "undefined")
238 position = subscription.filters.length; 238 position = subscription.filters.length;
239 239
240 filter.subscriptions.add(subscription); 240 filter.addSubscription(subscription);
241 subscription.filters.splice(position, 0, filter); 241 subscription.filters.splice(position, 0, filter);
242 filterNotifier.emit("filter.added", filter, subscription, position); 242 filterNotifier.emit("filter.added", filter, subscription, position);
243 }, 243 },
244 244
245 /** 245 /**
246 * Removes a user-defined filter from the list 246 * Removes a user-defined filter from the list
247 * @param {Filter} filter 247 * @param {Filter} filter
248 * @param {SpecialSubscription} [subscription] a particular filter group that 248 * @param {SpecialSubscription} [subscription] a particular filter group that
249 * the filter should be removed from (if ommited will be removed from all 249 * the filter should be removed from (if ommited will be removed from all
250 * subscriptions) 250 * subscriptions)
251 * @param {number} [position] position inside the filter group at which the 251 * @param {number} [position] position inside the filter group at which the
252 * filter should be removed (if ommited all instances will be removed) 252 * filter should be removed (if ommited all instances will be removed)
253 */ 253 */
254 removeFilter(filter, subscription, position) 254 removeFilter(filter, subscription, position)
255 { 255 {
256 let subscriptions = ( 256 let subscriptions = (
257 subscription ? [subscription] : filter.subscriptions 257 subscription ? [subscription] : filter.subscriptions()
258 ); 258 );
259 for (let currentSubscription of subscriptions) 259 for (let currentSubscription of subscriptions)
260 { 260 {
261 if (currentSubscription instanceof SpecialSubscription) 261 if (currentSubscription instanceof SpecialSubscription)
262 { 262 {
263 let positions = []; 263 let positions = [];
264 if (typeof position == "undefined") 264 if (typeof position == "undefined")
265 { 265 {
266 let index = -1; 266 let index = -1;
267 do 267 do
268 { 268 {
269 index = currentSubscription.filters.indexOf(filter, index + 1); 269 index = currentSubscription.filters.indexOf(filter, index + 1);
270 if (index >= 0) 270 if (index >= 0)
271 positions.push(index); 271 positions.push(index);
272 } while (index >= 0); 272 } while (index >= 0);
273 } 273 }
274 else 274 else
275 positions.push(position); 275 positions.push(position);
276 276
277 for (let j = positions.length - 1; j >= 0; j--) 277 for (let j = positions.length - 1; j >= 0; j--)
278 { 278 {
279 let currentPosition = positions[j]; 279 let currentPosition = positions[j];
280 if (currentSubscription.filters[currentPosition] == filter) 280 if (currentSubscription.filters[currentPosition] == filter)
281 { 281 {
282 currentSubscription.filters.splice(currentPosition, 1); 282 currentSubscription.filters.splice(currentPosition, 1);
283 if (currentSubscription.filters.indexOf(filter) < 0) 283 if (currentSubscription.filters.indexOf(filter) < 0)
284 filter.subscriptions.delete(currentSubscription); 284 filter.removeSubscription(currentSubscription);
285 filterNotifier.emit("filter.removed", filter, currentSubscription, 285 filterNotifier.emit("filter.removed", filter, currentSubscription,
286 currentPosition); 286 currentPosition);
287 } 287 }
288 } 288 }
289 } 289 }
290 } 290 }
291 }, 291 },
292 292
293 /** 293 /**
294 * Moves a user-defined filter to a new position 294 * Moves a user-defined filter to a new position
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 * Joins subscription's filters to the subscription without any notifications. 656 * Joins subscription's filters to the subscription without any notifications.
657 * @param {Subscription} subscription 657 * @param {Subscription} subscription
658 * filter subscription that should be connected to its filters 658 * filter subscription that should be connected to its filters
659 */ 659 */
660 function addSubscriptionFilters(subscription) 660 function addSubscriptionFilters(subscription)
661 { 661 {
662 if (!FilterStorage.knownSubscriptions.has(subscription.url)) 662 if (!FilterStorage.knownSubscriptions.has(subscription.url))
663 return; 663 return;
664 664
665 for (let filter of subscription.filters) 665 for (let filter of subscription.filters)
666 filter.subscriptions.add(subscription); 666 filter.addSubscription(subscription);
667 } 667 }
668 668
669 /** 669 /**
670 * Removes subscription's filters from the subscription without any 670 * Removes subscription's filters from the subscription without any
671 * notifications. 671 * notifications.
672 * @param {Subscription} subscription filter subscription to be removed 672 * @param {Subscription} subscription filter subscription to be removed
673 */ 673 */
674 function removeSubscriptionFilters(subscription) 674 function removeSubscriptionFilters(subscription)
675 { 675 {
676 if (!FilterStorage.knownSubscriptions.has(subscription.url)) 676 if (!FilterStorage.knownSubscriptions.has(subscription.url))
677 return; 677 return;
678 678
679 for (let filter of subscription.filters) 679 for (let filter of subscription.filters)
680 filter.subscriptions.delete(subscription); 680 filter.removeSubscription(subscription);
681 } 681 }
OLDNEW
« no previous file with comments | « lib/filterListener.js ('k') | lib/iniParser.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld