2013-10-15 04:38:07 +02:00
|
|
|
Miniflux.Feed = (function() {
|
|
|
|
|
|
|
|
// List of feeds currently updating
|
|
|
|
var queue = [];
|
|
|
|
|
|
|
|
// Number of concurrent requests when updating all feeds
|
|
|
|
var queue_length = 5;
|
|
|
|
|
2017-01-03 01:21:15 +01:00
|
|
|
var updateInterval = null;
|
2017-01-03 02:13:12 +01:00
|
|
|
var feeds = [];
|
2017-01-03 01:21:15 +01:00
|
|
|
|
2013-10-15 04:38:07 +02:00
|
|
|
return {
|
2014-12-31 13:43:05 +01:00
|
|
|
Update: function(feed, callback) {
|
|
|
|
var itemsCounter = feed.querySelector("span.items-count");
|
|
|
|
if (! itemsCounter) return;
|
|
|
|
|
2016-07-31 00:21:01 +02:00
|
|
|
var feed_id = feed.getAttribute("data-feed-id");
|
2014-12-31 13:43:05 +01:00
|
|
|
|
|
|
|
var heading = feed.querySelector("h2:first-of-type");
|
|
|
|
heading.className = "loading-icon";
|
2013-10-15 04:38:07 +02:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.onload = function() {
|
2014-12-31 13:43:05 +01:00
|
|
|
heading.className = "";
|
2015-01-14 19:44:41 +01:00
|
|
|
feed.removeAttribute("data-feed-error");
|
2014-12-31 13:43:05 +01:00
|
|
|
|
|
|
|
var lastChecked = feed.querySelector(".feed-last-checked");
|
2017-01-04 02:31:12 +01:00
|
|
|
if (lastChecked) {
|
|
|
|
lastChecked.innerHTML = lastChecked.getAttribute("data-after-update");
|
|
|
|
}
|
2014-12-31 13:43:05 +01:00
|
|
|
|
2014-12-02 16:29:00 +01:00
|
|
|
var response = JSON.parse(this.responseText);
|
2017-01-03 01:21:15 +01:00
|
|
|
if (response.result) {
|
|
|
|
itemsCounter.innerHTML = response.items_count.items_unread + "/" + response.items_count.items_total;
|
2014-12-31 13:43:05 +01:00
|
|
|
} else {
|
2015-01-14 19:44:41 +01:00
|
|
|
feed.setAttribute("data-feed-error", "1");
|
2017-01-04 02:31:12 +01:00
|
|
|
|
|
|
|
var errorMessageElement = feed.querySelector(".feed-parsing-error");
|
|
|
|
if (errorMessageElement) {
|
|
|
|
errorMessageElement.innerHTML = response.feed.parsing_error_message;
|
|
|
|
}
|
2014-12-31 13:43:05 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:58:46 +01:00
|
|
|
if (callback) {
|
|
|
|
callback(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Miniflux.Item.CheckForUpdates();
|
|
|
|
}
|
2013-10-15 04:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
request.open("POST", "?action=refresh-feed&feed_id=" + feed_id, true);
|
|
|
|
request.send();
|
|
|
|
},
|
2015-08-02 18:59:14 +02:00
|
|
|
UpdateAll: function(nb_concurrent_requests) {
|
2017-01-03 02:13:12 +01:00
|
|
|
feeds = Array.prototype.slice.call(document.querySelectorAll("article:not([data-feed-disabled])"));
|
2013-10-15 04:38:07 +02:00
|
|
|
|
2015-08-02 18:59:14 +02:00
|
|
|
// Check if a custom amount of concurrent requests was defined
|
|
|
|
if (nb_concurrent_requests) {
|
|
|
|
queue_length = nb_concurrent_requests;
|
2015-08-02 02:23:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:13:12 +01:00
|
|
|
updateInterval = setInterval(function () {
|
2013-10-15 04:38:07 +02:00
|
|
|
while (feeds.length > 0 && queue.length < queue_length) {
|
2014-12-31 13:43:05 +01:00
|
|
|
var feed = feeds.shift();
|
2015-01-27 19:01:21 +01:00
|
|
|
queue.push(parseInt(feed.getAttribute('data-feed-id'), 10));
|
2017-01-03 01:21:15 +01:00
|
|
|
Miniflux.Feed.Update(feed, Miniflux.Feed.OnFeedUpdated);
|
2013-10-15 04:38:07 +02:00
|
|
|
}
|
|
|
|
}, 100);
|
2017-01-03 01:21:15 +01:00
|
|
|
},
|
|
|
|
OnFeedUpdated: function(response) {
|
|
|
|
var index = queue.indexOf(response.feed_id);
|
|
|
|
if (index >= 0) queue.splice(index, 1);
|
|
|
|
|
|
|
|
if (feeds.length === 0 && queue.length === 0) {
|
|
|
|
clearInterval(updateInterval);
|
|
|
|
Miniflux.Item.CheckForUpdates();
|
|
|
|
}
|
2013-10-15 04:38:07 +02:00
|
|
|
}
|
|
|
|
};
|
2016-07-31 00:21:01 +02:00
|
|
|
})();
|