addings files + ressources tests

This commit is contained in:
2021-04-06 12:00:37 +02:00
parent 47dbc46ef5
commit 3afaee06e1
13 changed files with 2577 additions and 60 deletions

View File

@ -1,19 +0,0 @@
import { ListenerRss, ListenerRSSInfos } from "listener-rss/src/";
/**
* Permit to manage a ListenerRSS array, data storage and event aggregation
*/
export class GestionListener {
listenerArray: ListenerRss[] = [];
GestionListener(path?: string): void {
if (path) {
// load a file or a bdd
} else {
// keep the class empty
}
}
addNewListener(info: ListenerRSSInfos): void {
this.listenerArray.concat(new ListenerRss(info));
}
}

View File

@ -1 +0,0 @@
export * from "./gestion-listener";

98
src/manage-listener.ts Normal file
View File

@ -0,0 +1,98 @@
import EventEmitter from "events";
import { ListenerRss, ListenerRSSInfos } from "listener-rss";
interface ManageListenerInfo {
timeloop?: number;
path?: string;
}
/**
* Permit to manage a ListenerRSS array, data storage and event aggregation
*/
export class ManageListener extends EventEmitter {
listenerArray: ListenerRss[] = [];
looprunning: boolean = false;
// manage aggregation
timeloop?: number;
buffUpdates: any[] = [];
buffNewEntries: any[] = [];
constructor(info?: ManageListenerInfo) {
super();
if (info) {
if (info.path) {
const fs = require("fs");
const tmp = fs.readFileSync(info.path);
const configs: ListenerRSSInfos[] = JSON.parse(tmp);
configs.forEach((config) => this.addNewListener(config));
}
if (info.timeloop) {
this.timeloop = info.timeloop;
this.buffUpdates = [];
}
}
}
settingEvents(newListener: ListenerRss): void {
if (this.timeloop) {
newListener.on("update", (obj) => this.buffUpdates.push(obj));
newListener.on("newEntries", (obj) => this.buffNewEntries.push(obj));
// /!\ todo /!\ threat error with aggregation
// newListener.on("error", (err) => this.emit("error", err));
} else {
newListener.on("update", (obj) => this.emit("update", obj));
newListener.on("newEntries", (obj) => this.emit("newEntries", obj));
newListener.on("error", (err) => this.emit("error", err));
}
}
addNewListener(info: ListenerRSSInfos): void {
const newListener = new ListenerRss(info);
this.listenerArray.push(newListener);
this.settingEvents(newListener);
}
save(path: string): void {
if (path.endsWith(".json")) {
const fs = require("fs");
fs.writeFileSync(
path,
JSON.stringify(
this.listenerArray.map((listener) => listener.getProperty())
)
);
}
}
startAll(): void {
if (!this.looprunning) {
this.looprunning = true;
this.listenerArray.forEach((listener) => listener.start());
if (this.timeloop) {
const fun: () => void = async () => {
this.emit("update", this.buffUpdates);
this.emit("newEntries", this.buffNewEntries);
};
(async () => {
while (this.looprunning) {
await fun();
await new Promise((res) =>
setTimeout(res, this.timeloop ? this.timeloop * 1000 : 0)
);
}
})();
}
}
}
stopAll(): void {
if (this.looprunning) {
this.looprunning = false;
this.listenerArray.forEach((listener) => listener.stop());
}
}
}