add events + betters stubs

This commit is contained in:
2021-02-28 16:37:27 +01:00
parent 7d404c3ea9
commit eacf90b161
2 changed files with 54 additions and 52 deletions

View File

@ -1,9 +1,14 @@
import Parser from "rss-parser/index";
import { ListenerRSSInfos as ListenerInfo } from "./Models/ListenerRSSInfos";
import EventEmitter from "events";
const DEFAULT_TIMELOOP: number = 5 * 60; // default timeloop is 5 min
export class ListenerRss {
/**
* Emit 'update' when he's making a fetch during the start fun
* Emit 'update_err' when the fetch has an issue
*/
export class ListenerRss extends EventEmitter {
name: string = "";
address: string = "";
timeloop: number = DEFAULT_TIMELOOP; // time in seconds
@ -18,6 +23,8 @@ export class ListenerRss {
* @param config ListenerRSSInfos interface who's contain the ListenerInfos
*/
constructor(config: ListenerInfo) {
super();
this.setData(config);
this.setParser();
}
@ -70,18 +77,13 @@ export class ListenerRss {
* @brief call the callback function each looptime
* @param callback function who's going to be called with the latest get
*/
start(
callback: (
obj: { [key: string]: any } | undefined,
err: Error | undefined
) => void
): void {
start(): void {
this.loopRunning = true;
const fun: () => void = () => {
this.fetchRSS()
.then((obj: { [key: string]: any }) => callback(obj, undefined))
.catch((err) => callback(undefined, err));
.then((obj: { [key: string]: any }) => this.emit("update", obj))
.catch((err) => this.emit("update_err", err));
};
(async () => {