Cleanup node_helper to look more like the one from newsfeed module

This commit is contained in:
rejas 2020-12-23 20:15:04 +01:00
parent 0b37ed072c
commit bc60ae21c4

View File

@ -38,41 +38,48 @@ module.exports = NodeHelper.create({
* @param {string} identifier ID of the module * @param {string} identifier ID of the module
*/ */
createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert, identifier) { createFetcher: function (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert, identifier) {
var self = this;
if (!validUrl.isUri(url)) { if (!validUrl.isUri(url)) {
self.sendSocketNotification("INCORRECT_URL", { id: identifier, url: url }); this.sendSocketNotification("INCORRECT_URL", { id: identifier, url: url });
return; return;
} }
var fetcher; let fetcher;
if (typeof self.fetchers[identifier + url] === "undefined") { if (typeof this.fetchers[identifier + url] === "undefined") {
Log.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval); Log.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert); fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
fetcher.onReceive(function (fetcher) { fetcher.onReceive((fetcher) => {
self.sendSocketNotification("CALENDAR_EVENTS", { this.broadcastEvents(fetcher, identifier);
id: identifier,
url: fetcher.url(),
events: fetcher.events()
});
}); });
fetcher.onError(function (fetcher, error) { fetcher.onError((fetcher, error) => {
Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error); Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url(), error);
self.sendSocketNotification("FETCH_ERROR", { this.sendSocketNotification("FETCH_ERROR", {
id: identifier, id: identifier,
url: fetcher.url(), url: fetcher.url(),
error: error error: error
}); });
}); });
self.fetchers[identifier + url] = fetcher; this.fetchers[identifier + url] = fetcher;
} else { } else {
Log.log("Use existing calendar fetcher for url: " + url); Log.log("Use existing calendar fetcher for url: " + url);
fetcher = self.fetchers[identifier + url]; fetcher = this.fetchers[identifier + url];
fetcher.setReloadInterval(fetchInterval);
fetcher.broadcastEvents();
} }
fetcher.startFetch(); fetcher.startFetch();
},
/**
*
*/
broadcastEvents: function (fetcher, identifier) {
this.sendSocketNotification("CALENDAR_EVENTS", {
id: identifier,
url: fetcher.url(),
events: fetcher.events()
});
} }
}); });