some refactors
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
			
		||||
import Parser from "rss-parser/index";
 | 
			
		||||
import Parser from "rss-parser";
 | 
			
		||||
import { ListenerRSSInfos as ListenerInfo } from "./Models/ListenerRSSInfos";
 | 
			
		||||
import EventEmitter from "events";
 | 
			
		||||
 | 
			
		||||
@@ -15,7 +15,7 @@ export class ListenerRss extends EventEmitter {
 | 
			
		||||
  customfields?: { [key: string]: string[] | string };
 | 
			
		||||
 | 
			
		||||
  // private fields
 | 
			
		||||
  parser: Parser | undefined = undefined;
 | 
			
		||||
  parser: Parser;
 | 
			
		||||
  loopRunning: boolean = false;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -25,40 +25,28 @@ export class ListenerRss extends EventEmitter {
 | 
			
		||||
  constructor(config: ListenerInfo) {
 | 
			
		||||
    super();
 | 
			
		||||
 | 
			
		||||
    this.setData(config);
 | 
			
		||||
    this.setParser();
 | 
			
		||||
    this.name = config.name;
 | 
			
		||||
    this.address = config.address;
 | 
			
		||||
    this.timeloop =
 | 
			
		||||
      config.timeloop === undefined ? DEFAULT_TIMELOOP : config.timeloop;
 | 
			
		||||
    this.customfields = config.customfields;
 | 
			
		||||
 | 
			
		||||
    this.parser = this.generateParser();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Private function. Is useed to initilize the parser object with the customfields var
 | 
			
		||||
   */
 | 
			
		||||
  setParser() {
 | 
			
		||||
    // set parser
 | 
			
		||||
    this.parser = new Parser(
 | 
			
		||||
      this.customfields !== undefined
 | 
			
		||||
        ? {
 | 
			
		||||
            customFields: {
 | 
			
		||||
              feed: [],
 | 
			
		||||
              item: Object.entries(this.customfields).map(([, value]) => {
 | 
			
		||||
                return Array.isArray(value) ? value[0] : value;
 | 
			
		||||
              }),
 | 
			
		||||
            },
 | 
			
		||||
          }
 | 
			
		||||
        : {}
 | 
			
		||||
    ); // 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;
 | 
			
		||||
    this.address = infos.address;
 | 
			
		||||
    this.timeloop =
 | 
			
		||||
      infos.timeloop === undefined ? DEFAULT_TIMELOOP : infos.timeloop;
 | 
			
		||||
    this.customfields = infos.customfields;
 | 
			
		||||
  generateParser() {
 | 
			
		||||
    const parserConfig = this.customfields && {
 | 
			
		||||
      customFields: {
 | 
			
		||||
        feed: [],
 | 
			
		||||
        item: Object.entries(this.customfields).map(([, value]) => {
 | 
			
		||||
          return Array.isArray(value) ? value[0] : value;
 | 
			
		||||
        }),
 | 
			
		||||
      },
 | 
			
		||||
    };
 | 
			
		||||
    return new Parser(parserConfig);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -66,11 +54,9 @@ export class ListenerRss extends EventEmitter {
 | 
			
		||||
   * @return return a promise with the received data
 | 
			
		||||
   */
 | 
			
		||||
  fetchRSS(): Promise<Parser.Output<any>> {
 | 
			
		||||
    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");
 | 
			
		||||
    return this.parser.parseURL(this.address).catch((err) => {
 | 
			
		||||
      throw new Error("bad address or no access : " + err);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user