Michael Teeuw 5eed80c28e Squashed commit of the following:
commit e38dd346d9f796807ea71035c18e3533ec245ba6
Author: Michael Teeuw <michael@xonaymedia.nl>
Date:   Sat Apr 2 19:17:30 2016 +0200

    Add the possibility to set the maximum number of days.

commit 6f5c86775b708d19d3798267ffd23e491a1d2c62
Author: Sam Vendittelli <sam.vendittelli@hotmail.com>
Date:   Sat Apr 2 06:27:44 2016 +0100

    Fixed cursor appearing in margin

    Cursor was appearing in the margin so moved `cursor: none` property to html.

commit 576c668d84b34b8ad7a0fd51b146fde60f721682
Author: Domi-G <lessuseguy+githubdomig@gmail.com>
Date:   Fri Apr 1 22:52:32 2016 +0200

    Huge cleanup of white space
2016-04-03 19:52:13 +02:00

112 lines
2.3 KiB
JavaScript

/* Magic Mirror
* Fetcher
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var NewsFetcher = require('./newsfetcher.js');
/* Fetcher
* Responsible for requesting an update on the set interval and broadcasting the data.
*
* attribute url string - URL of the news feed.
* attribute reloadInterval number - Reload interval in milliseconds.
*/
var Fetcher = function(url, reloadInterval) {
var self = this;
var newsFetcher = new NewsFetcher();
if (reloadInterval < 1000) {
reloadInterval = 1000;
}
var reloadTimer = null;
var items = [];
var fetchFailedCallback = function() {};
var itemsReceivedCallback = function() {};
/* private methods */
/* fetchNews()
* Request the new items from the newsFetcher.
*/
var fetchNews = function() {
//console.log('Fetch news.');
clearTimeout(reloadTimer);
reloadTimer = null;
newsFetcher.fetchNews(url, function(fetchedItems) {
items = fetchedItems;
self.broadcastItems();
scheduleTimer();
}, function(error) {
fetchFailedCallback(self, error);
scheduleTimer();
});
};
/* scheduleTimer()
* Schedule the timer for the next update.
*/
var scheduleTimer = function() {
//console.log('Schedule update timer.');
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function() {
fetchNews();
}, reloadInterval);
};
/* public methods */
/* setReloadInterval()
* Update the reload interval, but only if we need to increase the speed.
*
* attribute interval number - Interval for the update in milliseconds.
*/
this.setReloadInterval = function(interval) {
if (interval > 1000 && interval < reloadInterval) {
reloadInterval = interval;
}
};
/* startFetch()
* Initiate fetchNews();
*/
this.startFetch = function() {
fetchNews();
};
/* broadcastItems()
* Broadcast the exsisting items.
*/
this.broadcastItems = function() {
if (items.length <= 0) {
//console.log('No items to broadcast yet.');
return;
}
//console.log('Broadcasting ' + items.length + ' items.');
itemsReceivedCallback(self);
};
this.onReceive = function(callback) {
itemsReceivedCallback = callback;
};
this.onError = function(callback) {
fetchFailedCallback = callback;
};
this.url = function() {
return url;
};
this.items = function() {
return items;
};
};
module.exports = Fetcher;