Version without design pattern builder + some new test
This commit is contained in:
@ -1,22 +1,25 @@
|
||||
const Parser = require('rss-parser');
|
||||
const ListenerInfo = require('./Models/ListenerRSSInfos');
|
||||
|
||||
const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min
|
||||
|
||||
class ListenerRss {
|
||||
name = undefined;
|
||||
address = undefined;
|
||||
timeloop = 5 * 60; // time in seconds
|
||||
timeloop = DEFAULT_TIMELOOP; // time in seconds
|
||||
customfields = [];
|
||||
|
||||
// private fields
|
||||
parser = null;
|
||||
obj = null;
|
||||
loopRunning = false;
|
||||
|
||||
constructor(info) {
|
||||
if(info !== undefined) {
|
||||
this.setData(info);
|
||||
this.setParser();
|
||||
}
|
||||
constructor(name, address, timeloop, customfields) {
|
||||
if(name !== undefined && name instanceof ListenerInfo) { // constructor with 1 arg
|
||||
this.setData(name);
|
||||
} else if (address !== undefined && typeof(address) === 'string') { // construct with between 2 and 4 args
|
||||
this.setData(new ListenerInfo(name, address, timeloop, customfields));
|
||||
} else throw new Error('the constructor must have args');
|
||||
this.setParser();
|
||||
}
|
||||
|
||||
setParser() {
|
||||
@ -27,15 +30,15 @@ class ListenerRss {
|
||||
return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
|
||||
})
|
||||
}
|
||||
} : undefined); // if customfield is set -> let's set the parser with, else let the option empty
|
||||
} : {}); // if customfield is set -> let's set the parser with, else let the option empty
|
||||
}
|
||||
|
||||
setData(infos) {
|
||||
// Set data
|
||||
this.name = infos._name === undefined ? infos._address : infos._name; // if name is undefined let's take the address
|
||||
this.name = infos._name;
|
||||
this.address = infos._address;
|
||||
this.timeloop = infos._timeloop;
|
||||
this.customfields = infos._customfields;
|
||||
this.timeloop = infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop;
|
||||
this.customfields = infos._customfields === undefined ? [] : infos._customfields;
|
||||
}
|
||||
|
||||
fetchRSS() {
|
||||
@ -52,8 +55,8 @@ class ListenerRss {
|
||||
|
||||
(async () => {
|
||||
while(this.loopRunning === true) {
|
||||
callback(await this.fetchRSS());
|
||||
await new Promise(res => setTimeout(res, this.timeloop * 1000));
|
||||
this.fetchRSS().then((obj, err) => callback(obj, err))
|
||||
await new Promise(res => setTimeout(res, 2000));
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
Reference in New Issue
Block a user