From 01392a2c20ef072cac7c12a1472ccc8feb7d20ba Mon Sep 17 00:00:00 2001 From: Amaury Joly Date: Mon, 8 Mar 2021 09:26:38 +0100 Subject: [PATCH] add newEntries event --- src/listener-rss.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/listener-rss.ts b/src/listener-rss.ts index 23f1c07..d2c1bca 100644 --- a/src/listener-rss.ts +++ b/src/listener-rss.ts @@ -17,6 +17,7 @@ export class ListenerRss extends EventEmitter { // private fields parser: Parser; loopRunning: boolean = false; + lastEntriesLinks: string[] = []; /** * @brief constructor @@ -66,7 +67,22 @@ export class ListenerRss extends EventEmitter { const fun: () => void = () => { this.fetchRSS() - .then((obj: { [key: string]: any }) => this.emit("update", obj)) + .then((obj: { [key: string]: any }) => { + this.emit("update", obj); + const updatedEntriesLinks = obj.items.map( + (item: { link: string }) => item.link + ); + + const newEntries = obj.items.filter( + (item: { link: string }) => + !this.lastEntriesLinks.includes(item.link) + ); + + if (this.lastEntriesLinks.length !== 0 && newEntries.length !== 0) { + this.emit("newEntries", newEntries); + } + this.lastEntriesLinks = updatedEntriesLinks; + }) .catch((err) => this.emit("error", err)); };