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: Address PS2 Comments Created Oct. 12, 2018, 3:46 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 yield "[Subscription]";
135 buffer.push("url=" + this.url); 135 let {url, type, _title, _fixedTitle, _disabled} = this;
Manish Jethani 2018/10/14 20:05:37 Let's place this line first before any yield state
Jon Sonesen 2018/10/21 06:18:23 Done.
Manish Jethani 2018/10/21 13:08:58 Sorry if I wasn't clear, it should be the first li
136 yield "url=" + url;
137
136 if (this.type) 138 if (this.type)
Manish Jethani 2018/10/14 20:05:37 Similarly, we should remove the `this.` prefix her
Jon Sonesen 2018/10/21 06:18:23 Done.
Manish Jethani 2018/10/21 13:08:58 What about `this.type` and `this._title`?
137 buffer.push("type=" + this.type); 139 yield "type=" + type;
138 if (this._title) 140 if (this._title)
139 buffer.push("title=" + this._title); 141 yield "title=" + _title;
140 if (this._fixedTitle) 142 if (_fixedTitle)
141 buffer.push("fixedTitle=true"); 143 yield "fixedTitle=true";
142 if (this._disabled) 144 if (_disabled)
143 buffer.push("disabled=true"); 145 yield "disabled=true";
144 }, 146 },
145 147
146 serializeFilters(buffer) 148 *serializeFilters()
147 { 149 {
148 for (let filter of this.filters) 150 for (let filter of this.filters)
149 buffer.push(filter.text.replace(/\[/g, "\\[")); 151 yield filter.text.replace(/\[/g, "\\[");
150 }, 152 },
151 153
152 toString() 154 toString()
153 { 155 {
154 let buffer = []; 156 return [...this.serialize()].join("\n");
155 this.serialize(buffer);
156 return buffer.join("\n");
157 } 157 }
158 }; 158 };
159 159
160 /** 160 /**
161 * Cache for known filter subscriptions, maps URL to subscription objects. 161 * Cache for known filter subscriptions, maps URL to subscription objects.
162 * @type {Map.<string,Subscription>} 162 * @type {Map.<string,Subscription>}
163 */ 163 */
164 Subscription.knownSubscriptions = new Map(); 164 Subscription.knownSubscriptions = new Map();
165 165
166 /** 166 /**
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 274 }
275 } 275 }
276 276
277 return false; 277 return false;
278 }, 278 },
279 279
280 /** 280 /**
281 * See Subscription.serialize() 281 * See Subscription.serialize()
282 * @inheritdoc 282 * @inheritdoc
283 */ 283 */
284 serialize(buffer) 284 *serialize()
285 { 285 {
286 Subscription.prototype.serialize.call(this, buffer); 286 let {defaults, _lastDownload} = this;
287 if (this.defaults && this.defaults.length) 287 yield* Subscription.prototype.serialize.call(this);
288 if (defaults && defaults.length)
288 { 289 {
289 buffer.push("defaults=" + 290 yield "defaults=" +
290 this.defaults.filter( 291 defaults.filter(
291 type => SpecialSubscription.defaultsMap.has(type) 292 type => SpecialSubscription.defaultsMap.has(type)
292 ).join(" ") 293 ).join(" ")
293 ); 294 ;
294 } 295 }
295 if (this._lastDownload) 296 if (_lastDownload)
296 buffer.push("lastDownload=" + this._lastDownload); 297 yield "lastDownload=" + _lastDownload;
297 } 298 }
298 }); 299 });
299 300
300 SpecialSubscription.defaultsMap = new Map([ 301 SpecialSubscription.defaultsMap = new Map([
301 ["whitelist", WhitelistFilter], 302 ["whitelist", WhitelistFilter],
302 ["blocking", BlockingFilter], 303 ["blocking", BlockingFilter],
303 ["elemhide", ElemHideBase] 304 ["elemhide", ElemHideBase]
304 ]); 305 ]);
305 306
306 /** 307 /**
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 this._lastDownload = value; 393 this._lastDownload = value;
393 filterNotifier.emit("subscription.lastDownload", this, value, oldValue); 394 filterNotifier.emit("subscription.lastDownload", this, value, oldValue);
394 } 395 }
395 return this._lastDownload; 396 return this._lastDownload;
396 }, 397 },
397 398
398 /** 399 /**
399 * See Subscription.serialize() 400 * See Subscription.serialize()
400 * @inheritdoc 401 * @inheritdoc
401 */ 402 */
402 serialize(buffer) 403 *serialize()
403 { 404 {
404 Subscription.prototype.serialize.call(this, buffer); 405 let {_homepage, _lastDownload} = this;
405 if (this._homepage) 406 yield* Subscription.prototype.serialize.call(this);
406 buffer.push("homepage=" + this._homepage); 407 if (_homepage)
407 if (this._lastDownload) 408 yield "homepage=" + _homepage;
408 buffer.push("lastDownload=" + this._lastDownload); 409 if (_lastDownload)
410 yield "lastDownload=" + _lastDownload;
409 } 411 }
410 }); 412 });
411 413
412 /** 414 /**
413 * Class for filter subscriptions updated externally (by other extension) 415 * Class for filter subscriptions updated externally (by other extension)
414 * @param {string} url see {@link Subscription Subscription()} 416 * @param {string} url see {@link Subscription Subscription()}
415 * @param {string} [title] see {@link Subscription Subscription()} 417 * @param {string} [title] see {@link Subscription Subscription()}
416 * @constructor 418 * @constructor
417 * @augments RegularSubscription 419 * @augments RegularSubscription
418 */ 420 */
419 function ExternalSubscription(url, title) 421 function ExternalSubscription(url, title)
420 { 422 {
421 RegularSubscription.call(this, url, title); 423 RegularSubscription.call(this, url, title);
422 } 424 }
423 exports.ExternalSubscription = ExternalSubscription; 425 exports.ExternalSubscription = ExternalSubscription;
424 426
425 ExternalSubscription.prototype = extend(RegularSubscription, { 427 ExternalSubscription.prototype = extend(RegularSubscription, {
426 /** 428 /**
427 * See Subscription.serialize() 429 * See Subscription.serialize()
428 * @inheritdoc 430 * @inheritdoc
429 */ 431 */
430 serialize(buffer) 432 *serialize() // eslint-disable-line require-yield
431 { 433 {
432 throw new Error( 434 throw new Error(
433 "Unexpected call, external subscriptions should not be serialized" 435 "Unexpected call, external subscriptions should not be serialized"
434 ); 436 );
435 } 437 }
436 }); 438 });
437 439
438 /** 440 /**
439 * Class for filter subscriptions updated externally (by other extension) 441 * Class for filter subscriptions updated externally (by other extension)
440 * @param {string} url see {@link Subscription Subscription()} 442 * @param {string} url see {@link Subscription Subscription()}
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 /** 547 /**
546 * Number indicating how often the object was downloaded. 548 * Number indicating how often the object was downloaded.
547 * @type {number} 549 * @type {number}
548 */ 550 */
549 downloadCount: 0, 551 downloadCount: 0,
550 552
551 /** 553 /**
552 * See Subscription.serialize() 554 * See Subscription.serialize()
553 * @inheritdoc 555 * @inheritdoc
554 */ 556 */
555 serialize(buffer) 557 *serialize()
556 { 558 {
557 RegularSubscription.prototype.serialize.call(this, buffer); 559 yield* RegularSubscription.prototype.serialize.call(this);
558 if (this.downloadStatus) 560
559 buffer.push("downloadStatus=" + this.downloadStatus); 561 let {downloadStatus, lastSuccess, lastCheck, expires,
Manish Jethani 2018/10/14 20:05:36 Let's move this line to the top of the function bo
Jon Sonesen 2018/10/21 06:18:24 Done.
560 if (this.lastSuccess) 562 softExpiration, errors, version, requiredVersion,
561 buffer.push("lastSuccess=" + this.lastSuccess); 563 downloadCount} = this;
562 if (this.lastCheck) 564
563 buffer.push("lastCheck=" + this.lastCheck); 565 if (downloadStatus)
564 if (this.expires) 566 yield "downloadStatus=" + downloadStatus;
565 buffer.push("expires=" + this.expires); 567 if (lastSuccess)
566 if (this.softExpiration) 568 yield "lastSuccess=" + lastSuccess;
567 buffer.push("softExpiration=" + this.softExpiration); 569 if (lastCheck)
568 if (this.errors) 570 yield "lastCheck=" + lastCheck;
569 buffer.push("errors=" + this.errors); 571 if (expires)
570 if (this.version) 572 yield "expires=" + expires;
571 buffer.push("version=" + this.version); 573 if (softExpiration)
572 if (this.requiredVersion) 574 yield "softExpiration=" + softExpiration;
573 buffer.push("requiredVersion=" + this.requiredVersion); 575 if (errors)
574 if (this.downloadCount) 576 yield "errors=" + errors;
575 buffer.push("downloadCount=" + this.downloadCount); 577 if (version)
578 yield "version=" + version;
579 if (requiredVersion)
580 yield "requiredVersion=" + requiredVersion;
581 if (downloadCount)
582 yield "downloadCount=" + downloadCount;
576 } 583 }
577 }); 584 });
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterStorage.js ('k') | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld