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

Unified Diff: lib/synchronizer.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Patch Set: Addressed Sebastian's initial feedback Created Feb. 21, 2017, 6:12 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/synchronizer.js
diff --git a/lib/synchronizer.js b/lib/synchronizer.js
index acca4711108822976a213d59febd5f5ebf72d196..b45dfd4199dcb023af04910e3c93f87105b8f49d 100644
--- a/lib/synchronizer.js
+++ b/lib/synchronizer.js
@@ -15,6 +15,8 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+"use strict";
+
/**
* @fileOverview Manages synchronization of filter subscriptions.
*/
@@ -22,39 +24,41 @@
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
-var {Downloader, Downloadable,
- MILLIS_IN_SECOND, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
-var {Filter, CommentFilter} = require("filterClasses");
-var {FilterStorage} = require("filterStorage");
-var {FilterNotifier} = require("filterNotifier");
-var {Prefs} = require("prefs");
-var {Subscription, DownloadableSubscription} = require("subscriptionClasses");
-var {Utils} = require("utils");
+let {Downloader, Downloadable,
+ MILLIS_IN_SECOND, MILLIS_IN_MINUTE,
+ MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
+let {Filter, CommentFilter} = require("filterClasses");
+let {FilterStorage} = require("filterStorage");
+let {FilterNotifier} = require("filterNotifier");
+let {Prefs} = require("prefs");
+let {Subscription, DownloadableSubscription} = require("subscriptionClasses");
+let {Utils} = require("utils");
-var INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
-var CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
-var DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY;
+let INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
+let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
+let DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY;
/**
* The object providing actual downloading functionality.
- * @type Downloader
+ * @type {Downloader}
*/
-var downloader = null;
+let downloader = null;
/**
* This object is responsible for downloading filter subscriptions whenever
* necessary.
* @class
*/
-var Synchronizer = exports.Synchronizer =
+let Synchronizer = exports.Synchronizer =
{
/**
* Called on module startup.
*/
- init: function()
+ init()
{
- downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY, CHECK_INTERVAL);
- onShutdown.add(function()
+ downloader = new Downloader(this._getDownloadables.bind(this),
+ INITIAL_DELAY, CHECK_INTERVAL);
+ onShutdown.add(() =>
{
downloader.cancel();
});
@@ -67,20 +71,22 @@ var Synchronizer = exports.Synchronizer =
/**
* Checks whether a subscription is currently being downloaded.
- * @param {String} url URL of the subscription
- * @return {Boolean}
+ * @param {string} url URL of the subscription
+ * @return {boolean}
*/
- isExecuting: function(url)
+ isExecuting(url)
{
return downloader.isDownloading(url);
},
/**
* Starts the download of a subscription.
- * @param {DownloadableSubscription} subscription Subscription to be downloaded
- * @param {Boolean} manual true for a manually started download (should not trigger fallback requests)
+ * @param {DownloadableSubscription} subscription Subscription to be
+ * downloaded
+ * @param {boolean} manual true for a manually started download
+ * (should not trigger fallback requests)
*/
- execute: function(subscription, manual)
+ execute(subscription, manual)
{
downloader.download(this._getDownloadable(subscription, manual));
},
@@ -88,7 +94,7 @@ var Synchronizer = exports.Synchronizer =
/**
* Yields Downloadable instances for all subscriptions that can be downloaded.
*/
- _getDownloadables: function*()
+ *_getDownloadables()
{
if (!Prefs.subscriptions_autoupdate)
return;
@@ -102,8 +108,11 @@ var Synchronizer = exports.Synchronizer =
/**
* Creates a Downloadable instance for a subscription.
+ * @param {Subscription} subscription
+ * @param {boolean} manual
+ * @return {Downloadable}
*/
- _getDownloadable: function(/**Subscription*/ subscription, /**Boolean*/ manual) /**Downloadable*/
+ _getDownloadable(subscription, manual)
{
let result = new Downloadable(subscription.url);
if (subscription.lastDownload != subscription.lastSuccess)
@@ -117,27 +126,34 @@ var Synchronizer = exports.Synchronizer =
return result;
},
- _onExpirationChange: function(downloadable)
+ _onExpirationChange(downloadable)
{
let subscription = Subscription.fromURL(downloadable.url);
- subscription.lastCheck = Math.round(downloadable.lastCheck / MILLIS_IN_SECOND);
- subscription.softExpiration = Math.round(downloadable.softExpiration / MILLIS_IN_SECOND);
- subscription.expires = Math.round(downloadable.hardExpiration / MILLIS_IN_SECOND);
+ subscription.lastCheck = Math.round(
+ downloadable.lastCheck / MILLIS_IN_SECOND
+ );
+ subscription.softExpiration = Math.round(
+ downloadable.softExpiration / MILLIS_IN_SECOND
+ );
+ subscription.expires = Math.round(
+ downloadable.hardExpiration / MILLIS_IN_SECOND
+ );
},
- _onDownloadStarted: function(downloadable)
+ _onDownloadStarted(downloadable)
{
let subscription = Subscription.fromURL(downloadable.url);
FilterNotifier.triggerListeners("subscription.downloading", subscription);
},
- _onDownloadSuccess: function(downloadable, responseText, errorCallback, redirectCallback)
+ _onDownloadSuccess(downloadable, responseText, errorCallback,
+ redirectCallback)
{
let lines = responseText.split(/[\r\n]+/);
- let match = /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i.exec(lines[0]);
- if (!match)
+ let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]);
+ if (!headerMatch)
return errorCallback("synchronize_invalid_data");
- let minVersion = match[1];
+ let minVersion = headerMatch[1];
// Don't remove parameter comments immediately but add them to a list first,
// they need to be considered in the checksum calculation.
@@ -175,8 +191,10 @@ var Synchronizer = exports.Synchronizer =
return redirectCallback(params.redirect);
// Handle redirects
- let subscription = Subscription.fromURL(downloadable.redirectURL || downloadable.url);
- if (downloadable.redirectURL && downloadable.redirectURL != downloadable.url)
+ let subscription = Subscription.fromURL(downloadable.redirectURL ||
+ downloadable.url);
+ if (downloadable.redirectURL &&
+ downloadable.redirectURL != downloadable.url)
{
let oldSubscription = Subscription.fromURL(downloadable.url);
subscription.title = oldSubscription.title;
@@ -194,7 +212,9 @@ var Synchronizer = exports.Synchronizer =
}
// The download actually succeeded
- subscription.lastSuccess = subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND);
+ subscription.lastSuccess = subscription.lastDownload = Math.round(
+ Date.now() / MILLIS_IN_SECOND
+ );
subscription.downloadStatus = "synchronize_ok";
subscription.downloadCount = downloadable.downloadCount;
subscription.errors = 0;
@@ -244,7 +264,10 @@ var Synchronizer = exports.Synchronizer =
}
}
- let [softExpiration, hardExpiration] = downloader.processExpirationInterval(expirationInterval);
+ let [
+ softExpiration,
+ hardExpiration
+ ] = downloader.processExpirationInterval(expirationInterval);
subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
@@ -268,7 +291,8 @@ var Synchronizer = exports.Synchronizer =
return undefined;
},
- _onDownloadError: function(downloadable, downloadURL, error, channelStatus, responseStatus, redirectCallback)
+ _onDownloadError(downloadable, downloadURL, error, channelStatus,
+ responseStatus, redirectCallback)
{
let subscription = Subscription.fromURL(downloadable.url);
subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND);
@@ -279,18 +303,26 @@ var Synchronizer = exports.Synchronizer =
{
subscription.errors++;
- if (redirectCallback && subscription.errors >= Prefs.subscriptions_fallbackerrors && /^https?:\/\//i.test(subscription.url))
+ if (redirectCallback &&
+ subscription.errors >= Prefs.subscriptions_fallbackerrors &&
+ /^https?:\/\//i.test(subscription.url))
{
subscription.errors = 0;
let fallbackURL = Prefs.subscriptions_fallbackurl;
let {addonVersion} = require("info");
- fallbackURL = fallbackURL.replace(/%VERSION%/g, encodeURIComponent(addonVersion));
- fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g, encodeURIComponent(subscription.url));
- fallbackURL = fallbackURL.replace(/%URL%/g, encodeURIComponent(downloadURL));
- fallbackURL = fallbackURL.replace(/%ERROR%/g, encodeURIComponent(error));
- fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g, encodeURIComponent(channelStatus));
- fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g, encodeURIComponent(responseStatus));
+ fallbackURL = fallbackURL.replace(/%VERSION%/g,
+ encodeURIComponent(addonVersion));
+ fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g,
+ encodeURIComponent(subscription.url));
+ fallbackURL = fallbackURL.replace(/%URL%/g,
+ encodeURIComponent(downloadURL));
+ fallbackURL = fallbackURL.replace(/%ERROR%/g,
+ encodeURIComponent(error));
+ fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g,
+ encodeURIComponent(channelStatus));
+ fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g,
+ encodeURIComponent(responseStatus));
let request = new XMLHttpRequest();
request.mozBackgroundRequest = true;
@@ -299,7 +331,7 @@ var Synchronizer = exports.Synchronizer =
request.channel.loadFlags = request.channel.loadFlags |
request.channel.INHIBIT_CACHING |
request.channel.VALIDATE_ALWAYS;
- request.addEventListener("load", function(ev)
+ request.addEventListener("load", ev =>
{
if (onShutdown.done)
return;
@@ -308,17 +340,19 @@ var Synchronizer = exports.Synchronizer =
return;
let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText);
- if (match && match[1] == "301" && match[2] && /^https?:\/\//i.test(match[2])) // Moved permanently
+ if (match && match[1] == "301" && // Moved permanently
Sebastian Noack 2017/02/21 09:19:32 Why so many spaces?
kzar 2017/02/21 10:37:02 Done.
+ match[2] && /^https?:\/\//i.test(match[2]))
redirectCallback(match[2]);
else if (match && match[1] == "410") // Gone
{
- let data = "[Adblock]\n" + subscription.filters.map((f) => f.text).join("\n");
+ let data = "[Adblock]\n" +
+ subscription.filters.map((f) => f.text).join("\n");
Sebastian Noack 2017/02/21 09:19:32 The parentheses around the arrow function's argume
kzar 2017/02/21 10:37:02 Done.
redirectCallback("data:text/plain," + encodeURIComponent(data));
}
}, false);
request.send(null);
}
}
- },
+ }
};
Synchronizer.init();

Powered by Google App Engine
This is Rietveld