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

Delta Between Two Patch Sets: 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/
Left Patch Set: Rebase and refactor Created Nov. 18, 2018, 12:46 a.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/filterListener.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 */ 188 */
189 addSubscription(subscription) 189 addSubscription(subscription)
190 { 190 {
191 if (this.knownSubscriptions.has(subscription.url)) 191 if (this.knownSubscriptions.has(subscription.url))
192 return; 192 return;
193 193
194 this.knownSubscriptions.set(subscription.url, subscription); 194 this.knownSubscriptions.set(subscription.url, subscription);
195 connectSubscriptionFilters(subscription); 195 connectSubscriptionFilters(subscription);
196 196
197 filterNotifier.emit("subscription.added", subscription); 197 filterNotifier.emit("subscription.added", subscription);
198
199 subscription.clearCaches();
198 } 200 }
199 201
200 /** 202 /**
201 * Removes a subscription from the storage. 203 * Removes a subscription from the storage.
202 * @param {Subscription} subscription The subscription to be removed. 204 * @param {Subscription} subscription The subscription to be removed.
203 */ 205 */
204 removeSubscription(subscription) 206 removeSubscription(subscription)
205 { 207 {
206 if (!this.knownSubscriptions.has(subscription.url)) 208 if (!this.knownSubscriptions.has(subscription.url))
207 return; 209 return;
(...skipping 19 matching lines...) Expand all
227 let oldFilters = [...subscription.filters()]; 229 let oldFilters = [...subscription.filters()];
228 disconnectSubscriptionFilters(subscription, oldFilters); 230 disconnectSubscriptionFilters(subscription, oldFilters);
229 subscription.clearFilters(); 231 subscription.clearFilters();
230 232
231 for (let filter of filters) 233 for (let filter of filters)
232 subscription.addFilter(filter); 234 subscription.addFilter(filter);
233 235
234 connectSubscriptionFilters(subscription, filters); 236 connectSubscriptionFilters(subscription, filters);
235 237
236 filterNotifier.emit("subscription.updated", subscription, oldFilters); 238 filterNotifier.emit("subscription.updated", subscription, oldFilters);
239
240 subscription.clearCaches();
237 } 241 }
238 242
239 /** 243 /**
240 * Adds a user-defined filter to the storage. 244 * Adds a user-defined filter to the storage.
241 * @param {Filter} filter 245 * @param {Filter} filter
242 * @param {?SpecialSubscription} [subscription] The subscription that the 246 * @param {?SpecialSubscription} [subscription] The subscription that the
243 * filter should be added to. 247 * filter should be added to.
244 * @param {number} [position] The position within the subscription at which 248 * @param {number} [position] The position within the subscription at which
245 * 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
246 * end of the subscription. 250 * end of the subscription.
247 */ 251 */
248 addFilter(filter, subscription, position) 252 addFilter(filter, subscription, position)
249 { 253 {
250 if (!subscription) 254 if (!subscription)
251 { 255 {
252 for (let currentSubscription of filter.subscriptions()) 256 for (let currentSubscription of this.subscriptionsForFilter(filter))
253 { 257 {
254 if (currentSubscription instanceof SpecialSubscription && 258 if (currentSubscription instanceof SpecialSubscription &&
255 !currentSubscription.disabled) 259 !currentSubscription.disabled)
256 { 260 {
257 return; // No need to add 261 return; // No need to add
258 } 262 }
259 } 263 }
260 subscription = this.getGroupForFilter(filter); 264 subscription = this.getGroupForFilter(filter);
261 } 265 }
262 if (!subscription) 266 if (!subscription)
(...skipping 18 matching lines...) Expand all
281 * @param {?SpecialSubscription} [subscription] The subscription that the 285 * @param {?SpecialSubscription} [subscription] The subscription that the
282 * 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
283 * removed from all subscriptions. 287 * removed from all subscriptions.
284 * @param {number} [position] The position within the subscription at which 288 * @param {number} [position] The position within the subscription at which
285 * 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
286 * filter will be removed. 290 * filter will be removed.
287 */ 291 */
288 removeFilter(filter, subscription, position) 292 removeFilter(filter, subscription, position)
289 { 293 {
290 let subscriptions = ( 294 let subscriptions = (
291 subscription ? [subscription] : filter.subscriptions() 295 subscription ? [subscription] : this.subscriptionsForFilter(filter)
292 ); 296 );
293 for (let currentSubscription of subscriptions) 297 for (let currentSubscription of subscriptions)
294 { 298 {
295 if (currentSubscription instanceof SpecialSubscription) 299 if (currentSubscription instanceof SpecialSubscription)
296 { 300 {
297 let positions = []; 301 let positions = [];
298 if (typeof position == "undefined") 302 if (typeof position == "undefined")
299 { 303 {
300 let index = -1; 304 let index = -1;
301 do 305 do
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 let knownSubscriptions = new Map(); 411 let knownSubscriptions = new Map();
408 for (let subscription of parser.subscriptions) 412 for (let subscription of parser.subscriptions)
409 knownSubscriptions.set(subscription.url, subscription); 413 knownSubscriptions.set(subscription.url, subscription);
410 414
411 this.fileProperties = parser.fileProperties; 415 this.fileProperties = parser.fileProperties;
412 this.knownSubscriptions = knownSubscriptions; 416 this.knownSubscriptions = knownSubscriptions;
413 Filter.knownFilters = parser.knownFilters; 417 Filter.knownFilters = parser.knownFilters;
414 Subscription.knownSubscriptions = parser.knownSubscriptions; 418 Subscription.knownSubscriptions = parser.knownSubscriptions;
415 419
416 if (!silent) 420 if (!silent)
421 {
417 filterNotifier.emit("load"); 422 filterNotifier.emit("load");
423
424 // Clear any in-memory caches that may have been created during
425 // initialization.
426 clearSubscriptionCaches();
427 }
418 } 428 }
419 }; 429 };
420 } 430 }
421 431
422 /** 432 /**
423 * Loads all subscriptions from disk. 433 * Loads all subscriptions from disk.
424 * @returns {Promise} A promise resolved or rejected when loading is complete. 434 * @returns {Promise} A promise resolved or rejected when loading is complete.
425 */ 435 */
426 loadFromDisk() 436 loadFromDisk()
427 { 437 {
(...skipping 28 matching lines...) Expand all
456 } 466 }
457 }); 467 });
458 }).catch(error => 468 }).catch(error =>
459 { 469 {
460 Cu.reportError(error); 470 Cu.reportError(error);
461 return tryBackup(1); 471 return tryBackup(1);
462 }).then(() => 472 }).then(() =>
463 { 473 {
464 this.initialized = true; 474 this.initialized = true;
465 filterNotifier.emit("load"); 475 filterNotifier.emit("load");
476 clearSubscriptionCaches();
466 }); 477 });
467 } 478 }
468 479
469 /** 480 /**
470 * Constructs the file name for a <code>patterns.ini</code> backup. 481 * Constructs the file name for a <code>patterns.ini</code> backup.
471 * @param {number} backupIndex Number of the backup file (1 being the most 482 * @param {number} backupIndex Number of the backup file (1 being the most
472 * recent). 483 * recent).
473 * @returns {string} Backup file name. 484 * @returns {string} Backup file name.
474 */ 485 */
475 getBackupName(backupIndex) 486 getBackupName(backupIndex)
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 682
672 /** 683 /**
673 * 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
674 * back to disk. 685 * back to disk.
675 */ 686 */
676 let filterStorage = new FilterStorage(); 687 let filterStorage = new FilterStorage();
677 688
678 exports.filterStorage = filterStorage; 689 exports.filterStorage = filterStorage;
679 690
680 /** 691 /**
681 * Adds to the filter any subscriptions in the storage to which a filter 692 * Adds to a filter any subscriptions in the storage to which the filter
682 * belongs. 693 * belongs.
683 * @param {Filter} filter 694 * @param {Filter} filter
684 */ 695 */
685 function addSubscriptionsToFilter(filter) 696 function addSubscriptionsToFilter(filter)
686 { 697 {
687 for (let subscription of filterStorage.subscriptions()) 698 for (let subscription of filterStorage.subscriptions())
688 { 699 {
689 if (subscription.hasFilter(filter)) 700 if (subscription.hasFilter(filter))
690 filter.addSubscription(subscription); 701 filter.addSubscription(subscription);
691 } 702 }
(...skipping 27 matching lines...) Expand all
719 * subscription is disconnected from its own filters. 730 * subscription is disconnected from its own filters.
720 */ 731 */
721 function disconnectSubscriptionFilters(subscription, filters) 732 function disconnectSubscriptionFilters(subscription, filters)
722 { 733 {
723 if (!filterStorage.knownSubscriptions.has(subscription.url)) 734 if (!filterStorage.knownSubscriptions.has(subscription.url))
724 return; 735 return;
725 736
726 for (let filter of filters || subscription.filters()) 737 for (let filter of filters || subscription.filters())
727 filter.removeSubscription(subscription); 738 filter.removeSubscription(subscription);
728 } 739 }
740
741 /**
742 * Clears any in-memory caches held by subscriptions in the storage.
743 */
744 function clearSubscriptionCaches()
745 {
746 for (let subscription of filterStorage.subscriptions())
747 subscription.clearCaches();
748 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld