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

Side by Side Diff: lib/subscriptionClasses.js

Issue 29800557: Issue 6559 - Use Map object for known subscriptions (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Move check for known subscription into removeSubscription Created June 6, 2018, 11 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
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 21 matching lines...) Expand all
32 * @param {string} url download location of the subscription 32 * @param {string} url download location of the subscription
33 * @param {string} [title] title of the filter subscription 33 * @param {string} [title] title of the filter subscription
34 * @constructor 34 * @constructor
35 */ 35 */
36 function Subscription(url, title) 36 function Subscription(url, title)
37 { 37 {
38 this.url = url; 38 this.url = url;
39 this.filters = []; 39 this.filters = [];
40 if (title) 40 if (title)
41 this._title = title; 41 this._title = title;
42 Subscription.knownSubscriptions[url] = this; 42 Subscription.knownSubscriptions.set(url, this);
43 } 43 }
44 exports.Subscription = Subscription; 44 exports.Subscription = Subscription;
45 45
46 Subscription.prototype = 46 Subscription.prototype =
47 { 47 {
48 /** 48 /**
49 * Download location of the subscription 49 * Download location of the subscription
50 * @type {string} 50 * @type {string}
51 */ 51 */
52 url: null, 52 url: null,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 toString() 147 toString()
148 { 148 {
149 let buffer = []; 149 let buffer = [];
150 this.serialize(buffer); 150 this.serialize(buffer);
151 return buffer.join("\n"); 151 return buffer.join("\n");
152 } 152 }
153 }; 153 };
154 154
155 /** 155 /**
156 * Cache for known filter subscriptions, maps URL to subscription objects. 156 * Cache for known filter subscriptions, maps URL to subscription objects.
157 * @type {Object} 157 * @type {Map.<string,Subscription>}
158 */ 158 */
159 Subscription.knownSubscriptions = Object.create(null); 159 Subscription.knownSubscriptions = new Map();
160 160
161 /** 161 /**
162 * Returns a subscription from its URL, creates a new one if necessary. 162 * Returns a subscription from its URL, creates a new one if necessary.
163 * @param {string} url 163 * @param {string} url
164 * URL of the subscription 164 * URL of the subscription
165 * @return {Subscription} 165 * @return {Subscription}
166 * subscription or null if the subscription couldn't be created 166 * subscription or null if the subscription couldn't be created
167 */ 167 */
168 Subscription.fromURL = function(url) 168 Subscription.fromURL = function(url)
169 { 169 {
170 if (url in Subscription.knownSubscriptions) 170 let subscription = Subscription.knownSubscriptions.get(url);
Manish Jethani 2018/06/06 11:04:42 No need to call both Map.has and Map.get, only the
171 return Subscription.knownSubscriptions[url]; 171 if (subscription)
172 return subscription;
172 173
173 if (url[0] != "~") 174 if (url[0] != "~")
174 return new DownloadableSubscription(url, null); 175 return new DownloadableSubscription(url, null);
175 return new SpecialSubscription(url); 176 return new SpecialSubscription(url);
176 }; 177 };
177 178
178 /** 179 /**
179 * Deserializes a subscription 180 * Deserializes a subscription
180 * 181 *
181 * @param {Object} obj 182 * @param {Object} obj
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 * Creates a new user-defined filter group. 300 * Creates a new user-defined filter group.
300 * @param {string} [title] title of the new filter group 301 * @param {string} [title] title of the new filter group
301 * @return {SpecialSubscription} 302 * @return {SpecialSubscription}
302 */ 303 */
303 SpecialSubscription.create = function(title) 304 SpecialSubscription.create = function(title)
304 { 305 {
305 let url; 306 let url;
306 do 307 do
307 { 308 {
308 url = "~user~" + Math.round(Math.random() * 1000000); 309 url = "~user~" + Math.round(Math.random() * 1000000);
309 } while (url in Subscription.knownSubscriptions); 310 } while (Subscription.knownSubscriptions.has(url));
310 return new SpecialSubscription(url, title); 311 return new SpecialSubscription(url, title);
311 }; 312 };
312 313
313 /** 314 /**
314 * Creates a new user-defined filter group and adds the given filter to it. 315 * Creates a new user-defined filter group and adds the given filter to it.
315 * This group will act as the default group for this filter type. 316 * This group will act as the default group for this filter type.
316 * @param {Filter} filter 317 * @param {Filter} filter
317 * @return {SpecialSubscription} 318 * @return {SpecialSubscription}
318 */ 319 */
319 SpecialSubscription.createForFilter = function(filter) 320 SpecialSubscription.createForFilter = function(filter)
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 if (this.errors) 566 if (this.errors)
566 buffer.push("errors=" + this.errors); 567 buffer.push("errors=" + this.errors);
567 if (this.version) 568 if (this.version)
568 buffer.push("version=" + this.version); 569 buffer.push("version=" + this.version);
569 if (this.requiredVersion) 570 if (this.requiredVersion)
570 buffer.push("requiredVersion=" + this.requiredVersion); 571 buffer.push("requiredVersion=" + this.requiredVersion);
571 if (this.downloadCount) 572 if (this.downloadCount)
572 buffer.push("downloadCount=" + this.downloadCount); 573 buffer.push("downloadCount=" + this.downloadCount);
573 } 574 }
574 }); 575 });
OLDNEW

Powered by Google App Engine
This is Rietveld