WARNING : doesn't work. Refactor JS to TS

This commit is contained in:
Amaury
2021-02-13 13:40:18 +01:00
parent a2281b3602
commit f96a056091
4 changed files with 427 additions and 89 deletions

View File

@ -0,0 +1,46 @@
class ListenerRSSInfos {
_name: string = ""; // name of the listener
_address: string = ""; // feed's address
_timeloop: number | undefined = 5 * 60; // update time RSS feed
_customfields: string[][] | undefined = []; // rss fields custom
constructor(
name: string,
address: string,
timeloop?: number,
customfields?: string[][]
) {
if (name !== undefined && address !== undefined) {
this._name = name;
this._address = address;
this._timeloop = timeloop;
this._customfields = customfields;
} else throw new Error("Bad constructor's args");
}
set timeloop(value) {
this._timeloop = value;
}
set customfields(value) {
this._customfields = value;
}
get name() {
return this._name;
}
get address() {
return this._address;
}
get timeloop() {
return this._timeloop;
}
get customfields() {
return this._customfields;
}
}
module.exports = ListenerRSSInfos;

View File

@ -1,85 +1,8 @@
import Parser from "rss-parser";
import ListenerRss from "./listener-rss.ts";
import ListenerRSSInfo from "./Models/ListenerRSSInfos";
// TODO J'ai des erreurs sur les imports, que je ne comprend pas trop
const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min
class ListenerRss {
name = "";
address = "";
timeloop = DEFAULT_TIMELOOP; // time in seconds
customfields = [];
// private fields
parser: Parser;
loopRunning = false;
constructor(
name: String,
address: String,
timeloop: number,
customfields: String[][]
) {
this.parser = new Parser();
// 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() {
// // set parser
// this.parser = new Parser(
// this.customfields !== undefined
// ? {
// customFields: {
// item: this.customfields.map((elt) => {
// return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
// }),
// },
// }
// : {}
// ); // if customfield is set -> let's set the parser with, else let the option empty
// }
// setData({name: String, address: String, timeloop: number = DEFAULT_TIMELOOP}) {
// // Set data
// this.name = name;
// this.address = address;
// this.timeloop =
// this.timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop;
// this.customfields = infos._customfields === undefined ? [] : infos._customfields;
// }
// fetchRSS() {
// return this.parser.parseURL(this.address).catch((err) => {
// throw new Error("bad address or no access : " + err);
// });
// }
// /**
// * @brief call the callback function each looptime
// * @param callback function who's going to be called with the latest get
// */
// start(callback) {
// this.loopRunning = true;
// (async () => {
// while (this.loopRunning === true) {
// this.fetchRSS().then((obj, err) => callback(obj, err));
// await new Promise((res) => setTimeout(res, 2000));
// }
// })();
// }
// /**
// * @brief stop the async function
// */
// stop() {
// this.loopRunning = false;
// }
}
module.exports = ListenerRss;
module.exports = {
ListenerRss: ListenerRss,
ListenerRSSInfo: ListenerRSSInfo,
};

97
src/listener-rss.ts Normal file
View File

@ -0,0 +1,97 @@
import Parser from "rss-parser";
import { ListenerRSSInfo as ListenerInfo } from "./Models/ListenerRSSInfos";
const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min
class ListenerRss {
name = undefined;
address = undefined;
timeloop = DEFAULT_TIMELOOP; // time in seconds
customfields = [];
// private fields
parser: Parser | undefined = undefined;
loopRunning: boolean = false;
constructor(
name: string | ListenerInfo,
address?: string,
timeloop?: number,
customfields?: string[][]
) {
if (name !== undefined && name instanceof ListenerInfo) {
// constructor with 1 arg
this.setData(name);
} else if (address !== undefined) {
// 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() {
// set parser
this.parser = new Parser(
this.customfields !== undefined
? {
customFields: {
item: this.customfields.map((elt) => {
return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
}),
},
}
: {}
); // if customfield is set -> let's set the parser with, else let the option empty
}
setData(infos: ListenerInfo) {
// Set data
this.name = infos._name;
this.address = infos._address;
this.timeloop =
infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop;
this.customfields =
infos._customfields === undefined ? [] : infos._customfields;
}
fetchRSS(): any {
// TODO Pas Bien
if (this.parser !== undefined && this.address !== undefined) {
return this.parser.parseURL(this.address).catch((err) => {
throw new Error("bad address or no access : " + err);
});
} else throw new Error("listener must be first initialized");
}
/**
* @brief call the callback function each looptime
* @param callback function who's going to be called with the latest get
*/
start(callback: any) {
// TODO any = Pas bien !!
/**
* Un des soucis qu'on a c'est sur le typage de l'objet de retour. le problème étant que la nature de l'obj de
* retour ne peut pas etre connue puisque il depend des custom fields qui sont definit par l'utilisateur. L'idée
* pourrait etre de creer une classe generique (cf le type CustomFields du ficher index.d.ts du package rss-parser).
* Après quelques recherches ca doit etre la manière ""correct"" de faire. Entre autre avec l'utilisation des mots
* clés keyof U ou encore Array<keyof U>. Je vais continuer a gratter dans cette direction perso.
*/
this.loopRunning = true;
(async () => {
while (this.loopRunning) {
this.fetchRSS().then((obj: any, err: any) => callback(obj, err)); // TODO Erreur a la compile
await new Promise((res) => setTimeout(res, 2000));
}
})();
}
/**
* @brief stop the async function
*/
stop() {
this.loopRunning = false;
}
}
module.exports = ListenerRss;