diff --git a/src/listener-rss.ts b/src/listener-rss.ts index c57de80..9258ef7 100644 --- a/src/listener-rss.ts +++ b/src/listener-rss.ts @@ -13,11 +13,18 @@ export class ListenerRss { parser: Parser | undefined = undefined; loopRunning: boolean = false; + /** + * @brief constructor + * @param config ListenerRSSInfos interface who's contain the ListenerInfos + */ constructor(config: ListenerInfo) { this.setData(config); this.setParser(); } + /** + * @brief Private function. Is useed to initilize the parser object with the customfields var + */ setParser() { // set parser this.parser = new Parser( @@ -34,6 +41,10 @@ export class ListenerRss { ); // if customfield is set -> let's set the parser with, else let the option empty } + /** + * @brief Private function. Initialized the listener with an ListenerRSSInfos interface + * @param infos ListenerRSSInfos interface who's contain the ListenerInfos + */ setData(infos: ListenerInfo) { // Set data this.name = infos.name; @@ -43,8 +54,11 @@ export class ListenerRss { this.customfields = infos.customfields; } - fetchRSS(): Promise { - // TODO Pas Bien + /** + * @brief use the parseURL function from rss-parser with the objects datas + * @return return a promise with the received data + */ + fetchRSS(): Promise> { if (this.parser !== undefined && this.address !== undefined) { return this.parser.parseURL(this.address).catch((err) => { throw new Error("bad address or no access : " + err); @@ -61,26 +75,27 @@ export class ListenerRss { obj: { [key: string]: any } | undefined, err: Error | undefined ) => void - ) { + ): void { this.loopRunning = true; - const fun = () => { + const fun: () => void = () => { this.fetchRSS() .then((obj: { [key: string]: any }) => callback(obj, undefined)) .catch((err) => callback(undefined, err)); }; - let fun_loop = () => { - fun(); - console.log("while"); - if (this.loopRunning) setTimeout(fun_loop, this.timeloop * 1000); - }; - fun_loop(); + + (async () => { + while (this.loopRunning) { + await fun(); + await new Promise((res) => setTimeout(res, this.timeloop * 1000)); + } + })(); } /** * @brief stop the async function */ - stop() { + stop(): void { this.loopRunning = false; } } diff --git a/tests/index-spec.ts b/tests/index-spec.ts index 36ce5ed..98bab4c 100644 --- a/tests/index-spec.ts +++ b/tests/index-spec.ts @@ -34,8 +34,9 @@ describe("test class RSS: jsonfile", function () { let stubListener: sinon.StubbedInstance; let stubParser: sinon.StubbedInstance; - const mockedRSSOutput: any = { - // TODO any = pas bien + const mockedRSSOutput: Parser.Output<{ + "media:group": { [key: string]: string | [any] }; + }> = { items: [ { title: "my title 00", @@ -96,7 +97,7 @@ describe("test class RSS: jsonfile", function () { } }); stubListener.setParser(); - stubListener.fetchRSS.resolves(stubParser.parseURL(stubListener.address)); + stubListener.fetchRSS.returns(stubParser.parseURL(stubListener.address)); } else throw new Error("myListener need to be initiliaze before the stub"); };