const XMLDownloader = require("./xmldownloader");
const XMLParser = require("./xmlparser");
const EntryParser = require("./entryparser");

const EventEmitter = require("events");

// 3rd party dependencies
const CronJob = require("cron").CronJob;

class Realworks extends EventEmitter {
	constructor(options) {
		super();

		this._options = options;

		if(!this._options.user) throw new TypeError("'options.user' is not set");
		if(!this._options.password) throw new TypeError("'options.password' is not set");

		this._cron = this._options.cron || true;
		this._cronPattern = this._options.cronPattern || "0 0 9 * * *";
		this._cronTimezone = this._options.cronTimezone || "Europe/Amsterdam";

		if(!this._options.types) throw new TypeError("'options.types' is not set");
		if(typeof this._options.types == "string") this._options.types = [this._options.types];
		this._types = this._options.types;

		this._downloader = new XMLDownloader(this._options, this);
		this._converter = new XMLParser(this._options, this);
		this._parser = new EntryParser(this._options, this);

		if(this._cron) {
			try {
				this._cronjob = new CronJob(this._cronPattern, this.update, null, true, this._cronTimezone);
			} catch(err) {
				throw new Error("Invalid cron pattern: "+err.message);
			}
		}
	}

	update() {
		return new Promise((resolve, reject) => {
			Promise.all(this._types.map((type) => {
				return this._downloader.download(type)
					.then((xmlData) => {
						return this._converter.parse(xmlData);
					})
					.then((entries) => {
						return this._parser.parse(entries);
					});
			}))
			.then((results) => {
				resolve(results);
			})
			.catch((err) => {
				this.emit("error", err);
				reject(err);
			});
		});
	}
}

module.exports = Realworks;
@@ -1150,19 +1150,112 @@
 rn, 
-this.update
+function () %7B%0A%09%09%09%09%09this.update()%0A%09%09%09%09%09%09.catch((err) =%3E %7B%0A%09%09%09%09%09%09%09this.emit(%22error%22, err);%0A%09%09%09%09%09%09%7D);%0A%09%09%09%09%7D
 , nu
@@ -1757,37 +1757,8 @@
 %3E %7B%0A
-%09%09%09%09this.emit(%22error%22, err);%0A
 %09%09%09%09

Fix issue where update isn't being run