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

Side by Side Diff: lib/subscriptionClasses.js

Issue 29900557: Issue 7016 - Convert serialization functions into generators (Closed)
Patch Set: Actually Address PS2 Comments Created Oct. 21, 2018, 5:25 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
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 if (value != this._disabled) 118 if (value != this._disabled)
119 { 119 {
120 let oldValue = this._disabled; 120 let oldValue = this._disabled;
121 this._disabled = value; 121 this._disabled = value;
122 filterNotifier.emit("subscription.disabled", this, value, oldValue); 122 filterNotifier.emit("subscription.disabled", this, value, oldValue);
123 } 123 }
124 return this._disabled; 124 return this._disabled;
125 }, 125 },
126 126
127 /** 127 /**
128 * Serializes the subscription to an array of strings for writing 128 * Serializes the subscription to an array of strings for writing
Manish Jethani 2018/10/21 21:51:43 We could remove the "to an array of strings" part
Jon Sonesen 2018/10/22 19:27:47 Acknowledged.
129 * out on the disk. 129 * out on the disk.
130 * @param {string[]} buffer buffer to push the serialization results into 130 * @yields {string}
131 */ 131 */
132 serialize(buffer) 132 *serialize()
133 { 133 {
134 buffer.push("[Subscription]"); 134 let {url, type, _title, _fixedTitle, _disabled} = this;
135 buffer.push("url=" + this.url); 135
136 if (this.type) 136 yield "[Subscription]";
137 buffer.push("type=" + this.type); 137 yield "url=" + url;
138 if (this._title) 138
139 buffer.push("title=" + this._title); 139 if (type)
140 if (this._fixedTitle) 140 yield "type=" + type;
141 buffer.push("fixedTitle=true"); 141 if (_title)
142 if (this._disabled) 142 yield "title=" + _title;
143 buffer.push("disabled=true"); 143 if (_fixedTitle)
144 yield "fixedTitle=true";
145 if (_disabled)
146 yield "disabled=true";
144 }, 147 },
145 148
146 serializeFilters(buffer) 149 *serializeFilters()
147 { 150 {
148 for (let filter of this.filters) 151 for (let filter of this.filters)
Jon Sonesen 2018/10/22 19:32:40 I noticed this method goes against our pattern els
Manish Jethani 2018/10/22 20:18:01 Just for the sake of consistency, I wouldn't mind
Jon Sonesen 2018/10/23 02:04:24 Done.
149 buffer.push(filter.text.replace(/\[/g, "\\[")); 152 yield filter.text.replace(/\[/g, "\\[");
150 }, 153 },
151 154
152 toString() 155 toString()
153 { 156 {
154 let buffer = []; 157 return [...this.serialize()].join("\n");
155 this.serialize(buffer);
156 return buffer.join("\n");
157 } 158 }
158 }; 159 };
159 160
160 /** 161 /**
161 * Cache for known filter subscriptions, maps URL to subscription objects. 162 * Cache for known filter subscriptions, maps URL to subscription objects.
162 * @type {Map.<string,Subscription>} 163 * @type {Map.<string,Subscription>}
163 */ 164 */
164 Subscription.knownSubscriptions = new Map(); 165 Subscription.knownSubscriptions = new Map();
165 166
166 /** 167 /**
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 275 }
275 } 276 }
276 277
277 return false; 278 return false;
278 }, 279 },
279 280
280 /** 281 /**
281 * See Subscription.serialize() 282 * See Subscription.serialize()
282 * @inheritdoc 283 * @inheritdoc
283 */ 284 */
284 serialize(buffer) 285 *serialize()
285 { 286 {
286 Subscription.prototype.serialize.call(this, buffer); 287 let {defaults, _lastDownload} = this;
287 if (this.defaults && this.defaults.length) 288
289 yield* Subscription.prototype.serialize.call(this);
290
291 if (defaults && defaults.length)
288 { 292 {
289 buffer.push("defaults=" + 293 yield "defaults=" +
290 this.defaults.filter( 294 defaults.filter(
291 type => SpecialSubscription.defaultsMap.has(type) 295 type => SpecialSubscription.defaultsMap.has(type)
292 ).join(" ") 296 ).join(" ");
293 );
294 } 297 }
295 if (this._lastDownload) 298 if (_lastDownload)
296 buffer.push("lastDownload=" + this._lastDownload); 299 yield "lastDownload=" + _lastDownload;
297 } 300 }
298 }); 301 });
299 302
300 SpecialSubscription.defaultsMap = new Map([ 303 SpecialSubscription.defaultsMap = new Map([
301 ["whitelist", WhitelistFilter], 304 ["whitelist", WhitelistFilter],
302 ["blocking", BlockingFilter], 305 ["blocking", BlockingFilter],
303 ["elemhide", ElemHideBase] 306 ["elemhide", ElemHideBase]
304 ]); 307 ]);
305 308
306 /** 309 /**
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 this._lastDownload = value; 395 this._lastDownload = value;
393 filterNotifier.emit("subscription.lastDownload", this, value, oldValue); 396 filterNotifier.emit("subscription.lastDownload", this, value, oldValue);
394 } 397 }
395 return this._lastDownload; 398 return this._lastDownload;
396 }, 399 },
397 400
398 /** 401 /**
399 * See Subscription.serialize() 402 * See Subscription.serialize()
400 * @inheritdoc 403 * @inheritdoc
401 */ 404 */
402 serialize(buffer) 405 *serialize()
403 { 406 {
404 Subscription.prototype.serialize.call(this, buffer); 407 let {_homepage, _lastDownload} = this;
405 if (this._homepage) 408
406 buffer.push("homepage=" + this._homepage); 409 yield* Subscription.prototype.serialize.call(this);
407 if (this._lastDownload) 410
408 buffer.push("lastDownload=" + this._lastDownload); 411 if (_homepage)
412 yield "homepage=" + _homepage;
413 if (_lastDownload)
414 yield "lastDownload=" + _lastDownload;
409 } 415 }
410 }); 416 });
411 417
412 /** 418 /**
413 * Class for filter subscriptions updated externally (by other extension) 419 * Class for filter subscriptions updated externally (by other extension)
414 * @param {string} url see {@link Subscription Subscription()} 420 * @param {string} url see {@link Subscription Subscription()}
415 * @param {string} [title] see {@link Subscription Subscription()} 421 * @param {string} [title] see {@link Subscription Subscription()}
416 * @constructor 422 * @constructor
417 * @augments RegularSubscription 423 * @augments RegularSubscription
418 */ 424 */
419 function ExternalSubscription(url, title) 425 function ExternalSubscription(url, title)
420 { 426 {
421 RegularSubscription.call(this, url, title); 427 RegularSubscription.call(this, url, title);
422 } 428 }
423 exports.ExternalSubscription = ExternalSubscription; 429 exports.ExternalSubscription = ExternalSubscription;
424 430
425 ExternalSubscription.prototype = extend(RegularSubscription, { 431 ExternalSubscription.prototype = extend(RegularSubscription, {
426 /** 432 /**
427 * See Subscription.serialize() 433 * See Subscription.serialize()
428 * @inheritdoc 434 * @inheritdoc
429 */ 435 */
430 serialize(buffer) 436 *serialize() // eslint-disable-line require-yield
431 { 437 {
432 throw new Error( 438 throw new Error(
433 "Unexpected call, external subscriptions should not be serialized" 439 "Unexpected call, external subscriptions should not be serialized"
434 ); 440 );
435 } 441 }
436 }); 442 });
437 443
438 /** 444 /**
439 * Class for filter subscriptions updated externally (by other extension) 445 * Class for filter subscriptions updated externally (by other extension)
440 * @param {string} url see {@link Subscription Subscription()} 446 * @param {string} url see {@link Subscription Subscription()}
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 /** 551 /**
546 * Number indicating how often the object was downloaded. 552 * Number indicating how often the object was downloaded.
547 * @type {number} 553 * @type {number}
548 */ 554 */
549 downloadCount: 0, 555 downloadCount: 0,
550 556
551 /** 557 /**
552 * See Subscription.serialize() 558 * See Subscription.serialize()
553 * @inheritdoc 559 * @inheritdoc
554 */ 560 */
555 serialize(buffer) 561 *serialize()
556 { 562 {
557 RegularSubscription.prototype.serialize.call(this, buffer); 563 let {downloadStatus, lastSuccess, lastCheck, expires,
558 if (this.downloadStatus) 564 softExpiration, errors, version, requiredVersion,
559 buffer.push("downloadStatus=" + this.downloadStatus); 565 downloadCount} = this;
560 if (this.lastSuccess) 566
561 buffer.push("lastSuccess=" + this.lastSuccess); 567 yield* RegularSubscription.prototype.serialize.call(this);
562 if (this.lastCheck) 568
563 buffer.push("lastCheck=" + this.lastCheck); 569 if (downloadStatus)
564 if (this.expires) 570 yield "downloadStatus=" + downloadStatus;
565 buffer.push("expires=" + this.expires); 571 if (lastSuccess)
566 if (this.softExpiration) 572 yield "lastSuccess=" + lastSuccess;
567 buffer.push("softExpiration=" + this.softExpiration); 573 if (lastCheck)
568 if (this.errors) 574 yield "lastCheck=" + lastCheck;
569 buffer.push("errors=" + this.errors); 575 if (expires)
570 if (this.version) 576 yield "expires=" + expires;
571 buffer.push("version=" + this.version); 577 if (softExpiration)
572 if (this.requiredVersion) 578 yield "softExpiration=" + softExpiration;
573 buffer.push("requiredVersion=" + this.requiredVersion); 579 if (errors)
574 if (this.downloadCount) 580 yield "errors=" + errors;
575 buffer.push("downloadCount=" + this.downloadCount); 581 if (version)
582 yield "version=" + version;
583 if (requiredVersion)
584 yield "requiredVersion=" + requiredVersion;
585 if (downloadCount)
586 yield "downloadCount=" + downloadCount;
576 } 587 }
577 }); 588 });
OLDNEW
« lib/filterStorage.js ('K') | « lib/filterStorage.js ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld