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

Side by Side Diff: lib/filterClasses.js

Issue 29871555: Issue 6916 - Avoid Set object for filters with only one subscription (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add type check to addSubscription Created Sept. 1, 2018, 2:43 p.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 | « no previous file | 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 25 matching lines...) Expand all
36 * 36 *
37 * @param {string} text string representation of the filter 37 * @param {string} text string representation of the filter
38 * @constructor 38 * @constructor
39 */ 39 */
40 function Filter(text) 40 function Filter(text)
41 { 41 {
42 this.text = text; 42 this.text = text;
43 43
44 /** 44 /**
45 * Subscriptions to which this filter belongs. 45 * Subscriptions to which this filter belongs.
46 * @type {Set.<Subscription>} 46 * @type {(Subscription|Set.<Subscription>)?}
47 * @private 47 * @private
48 */ 48 */
49 this._subscriptions = new Set(); 49 this._subscriptions = null;
50 } 50 }
51 exports.Filter = Filter; 51 exports.Filter = Filter;
52 52
53 Filter.prototype = 53 Filter.prototype =
54 { 54 {
55 /** 55 /**
56 * String representation of the filter 56 * String representation of the filter
57 * @type {string} 57 * @type {string}
58 */ 58 */
59 text: null, 59 text: null,
60 60
61 /** 61 /**
62 * Filter type as a string, e.g. "blocking". 62 * Filter type as a string, e.g. "blocking".
63 * @type {string} 63 * @type {string}
64 */ 64 */
65 get type() 65 get type()
66 { 66 {
67 throw new Error("Please define filter type in the subclass"); 67 throw new Error("Please define filter type in the subclass");
68 }, 68 },
69 69
70 /** 70 /**
71 * Yields subscriptions to which the filter belongs. 71 * Yields subscriptions to which the filter belongs.
72 * @yields {Subscription} 72 * @yields {Subscription}
Jon Sonesen 2018/09/02 17:22:50 Should this be {(Subscription|Set.<Subscription>)?
Manish Jethani 2018/09/03 19:03:42 No, the function only ever yields a single Subscri
73 */ 73 */
74 *subscriptions() 74 *subscriptions()
75 { 75 {
76 yield* this._subscriptions; 76 if (this._subscriptions)
77 {
78 if (this._subscriptions instanceof Set)
79 yield* this._subscriptions;
80 else
81 yield this._subscriptions;
82 }
77 }, 83 },
78 84
79 /** 85 /**
80 * The number of subscriptions to which the filter belongs. 86 * The number of subscriptions to which the filter belongs.
81 * @type {number} 87 * @type {number}
82 */ 88 */
83 get subscriptionCount() 89 get subscriptionCount()
84 { 90 {
85 return this._subscriptions.size; 91 if (this._subscriptions instanceof Set)
92 return this._subscriptions.size;
93
94 return this._subscriptions ? 1 : 0;
86 }, 95 },
87 96
88 /** 97 /**
89 * Adds a subscription to the set of subscriptions to which the filter 98 * Adds a subscription to the set of subscriptions to which the filter
90 * belongs. 99 * belongs.
91 * @param {Subscription} subscription 100 * @param {Subscription} subscription
92 */ 101 */
93 addSubscription(subscription) 102 addSubscription(subscription)
94 { 103 {
95 this._subscriptions.add(subscription); 104 // Since we use truthy checks in our logic, we must avoid adding a
105 // subscription that isn't a non-null object.
Jon Sonesen 2018/09/02 17:22:50 the double negative in the comment is sort of conf
Manish Jethani 2018/09/03 19:03:42 The non-null part is a bit of a distraction: the p
106 if (subscription === null || typeof subscription != "object")
107 return;
108
109 if (this._subscriptions)
110 {
111 if (this._subscriptions instanceof Set)
112 this._subscriptions.add(subscription);
113 else if (subscription != this._subscriptions)
114 this._subscriptions = new Set([this._subscriptions, subscription]);
115 }
116 else
117 {
118 this._subscriptions = subscription;
119 }
96 }, 120 },
97 121
98 /** 122 /**
99 * Removes a subscription from the set of subscriptions to which the filter 123 * Removes a subscription from the set of subscriptions to which the filter
100 * belongs. 124 * belongs.
101 * @param {Subscription} subscription 125 * @param {Subscription} subscription
102 */ 126 */
103 removeSubscription(subscription) 127 removeSubscription(subscription)
104 { 128 {
105 this._subscriptions.delete(subscription); 129 if (this._subscriptions)
130 {
131 if (this._subscriptions instanceof Set)
132 {
133 this._subscriptions.delete(subscription);
134
135 if (this._subscriptions.size == 1)
136 this._subscriptions = [...this._subscriptions][0];
Jon Sonesen 2018/09/02 17:22:50 Just asking, not so much a comment but this line
Manish Jethani 2018/09/03 19:03:42 Yes, that's exactly what it does. We can't do `thi
137 }
138 else if (subscription == this._subscriptions)
139 {
140 this._subscriptions = null;
141 }
142 }
106 }, 143 },
107 144
108 /** 145 /**
109 * Serializes the filter to an array of strings for writing out on the disk. 146 * Serializes the filter to an array of strings for writing out on the disk.
110 * @param {string[]} buffer buffer to push the serialization results into 147 * @param {string[]} buffer buffer to push the serialization results into
111 */ 148 */
112 serialize(buffer) 149 serialize(buffer)
113 { 150 {
114 buffer.push("[Filter]"); 151 buffer.push("[Filter]");
115 buffer.push("text=" + this.text); 152 buffer.push("text=" + this.text);
(...skipping 1099 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 1252
1216 /** 1253 /**
1217 * Script that should be executed 1254 * Script that should be executed
1218 * @type {string} 1255 * @type {string}
1219 */ 1256 */
1220 get script() 1257 get script()
1221 { 1258 {
1222 return this.body; 1259 return this.body;
1223 } 1260 }
1224 }); 1261 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld