forked from Outils-PeerTube/listener-rss-aggregator
Added a Dependency injection model to load the DB asynchronously during the constructor
This commit is contained in:
@ -7,32 +7,65 @@ import { SqliteTools } from "./sqlite-tools";
|
||||
* Permit to manage a ListenerRSS array, data storage and event aggregation
|
||||
*/
|
||||
export class ListenerRssAggregator extends EventEmitter {
|
||||
private listenerArray: Map<string, ListenerRss> = new Map(); // renomer listenerMap
|
||||
private listenerMap: Map<string, ListenerRss>;
|
||||
private looprunning: boolean = false;
|
||||
|
||||
private sqliteDb: SqliteTools;
|
||||
|
||||
constructor(path: string) {
|
||||
constructor(args: {
|
||||
sqliteDB: SqliteTools;
|
||||
configInstantiated: Map<string, ListenerRss>;
|
||||
}) {
|
||||
super();
|
||||
this.sqliteDb = new SqliteTools(path);
|
||||
this.load();
|
||||
|
||||
this.sqliteDb = args.sqliteDB;
|
||||
this.listenerMap = args.configInstantiated;
|
||||
}
|
||||
|
||||
static async instantiateAggregator(
|
||||
path: string
|
||||
): Promise<{
|
||||
sqliteDB: SqliteTools;
|
||||
configInstantiated: Map<string, ListenerRss>;
|
||||
}> {
|
||||
const sqliteDB = new SqliteTools(path);
|
||||
const configInstantiated = await this.loadDb(sqliteDB);
|
||||
return { sqliteDB, configInstantiated };
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief [private don't use it] create a sqlite instance for the specified path and load the data
|
||||
*/
|
||||
private static async loadDb(
|
||||
sqliteDb: SqliteTools
|
||||
): Promise<Map<string, ListenerRss>> {
|
||||
const config = await this.extractConfig(sqliteDb);
|
||||
|
||||
const configInstantiated = new Map<string, ListenerRss>();
|
||||
|
||||
for (const obj of config)
|
||||
configInstantiated.set(obj.address, new ListenerRss(obj));
|
||||
|
||||
return configInstantiated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief [private don't use it] load the actual config inside the sqliteTools instance specified
|
||||
*/
|
||||
private static async extractConfig(
|
||||
sqliteDb: SqliteTools
|
||||
): Promise<ListenerRSSInfos[]> {
|
||||
await sqliteDb.ensureTableExists();
|
||||
return await sqliteDb.fetchAll();
|
||||
}
|
||||
|
||||
async getAllConfigs(): Promise<ListenerRSSInfos[]> {
|
||||
return await this.sqliteDb.fetchAll();
|
||||
}
|
||||
|
||||
private async load() {
|
||||
const configs = await this.getAllConfigs();
|
||||
|
||||
configs.forEach((config) => {
|
||||
this.addNewListener(config);
|
||||
});
|
||||
return await ListenerRssAggregator.extractConfig(this.sqliteDb);
|
||||
}
|
||||
|
||||
private addNewListener(info: ListenerRSSInfos): ListenerRss {
|
||||
const newListener = new ListenerRss(info);
|
||||
this.listenerArray.set(newListener.address, newListener);
|
||||
this.listenerMap.set(newListener.address, newListener);
|
||||
|
||||
newListener.on("update", (obj) => this.emit("update", obj));
|
||||
newListener.on("newEntries", (obj) => this.emit("newEntries", obj));
|
||||
@ -42,7 +75,7 @@ export class ListenerRssAggregator extends EventEmitter {
|
||||
}
|
||||
|
||||
private removeOldListener(adr: string): void {
|
||||
const oldListener = this.listenerArray.get(adr);
|
||||
const oldListener = this.listenerMap.get(adr);
|
||||
|
||||
if (!oldListener) return;
|
||||
|
||||
@ -51,18 +84,18 @@ export class ListenerRssAggregator extends EventEmitter {
|
||||
.removeAllListeners("update")
|
||||
.removeAllListeners("newEntries")
|
||||
.removeAllListeners("error");
|
||||
this.listenerArray.delete(adr);
|
||||
this.listenerMap.delete(adr);
|
||||
}
|
||||
|
||||
async registerListener(info: ListenerRSSInfos) {
|
||||
if (this.listenerArray.has(info.address)) return;
|
||||
if (this.listenerMap.has(info.address)) return;
|
||||
|
||||
const listener = this.addNewListener(info);
|
||||
await this.sqliteDb.insertListener(listener);
|
||||
}
|
||||
|
||||
async unregisterListener(adr: string): Promise<void> {
|
||||
if (!this.listenerArray.has(adr)) return;
|
||||
if (!this.listenerMap.has(adr)) return;
|
||||
|
||||
this.removeOldListener(adr);
|
||||
await this.sqliteDb.deleteListener(adr);
|
||||
@ -73,26 +106,28 @@ export class ListenerRssAggregator extends EventEmitter {
|
||||
|
||||
for (const newItem of expectedConfig.filter(
|
||||
(item) => !actualConfig.includes(item)
|
||||
))
|
||||
)) {
|
||||
this.registerListener(newItem);
|
||||
}
|
||||
|
||||
for (const oldItem of actualConfig.filter(
|
||||
(item) => !expectedConfig.includes(item)
|
||||
))
|
||||
)) {
|
||||
this.unregisterListener(oldItem.address);
|
||||
}
|
||||
}
|
||||
|
||||
startAll(): void {
|
||||
if (this.looprunning) return;
|
||||
|
||||
this.looprunning = true;
|
||||
for (const item of this.listenerArray.values()) item.start();
|
||||
for (const item of this.listenerMap.values()) item.start();
|
||||
}
|
||||
|
||||
stopAll(): void {
|
||||
if (!this.looprunning) return;
|
||||
|
||||
this.looprunning = false;
|
||||
for (const item of this.listenerArray.values()) item.stop();
|
||||
for (const item of this.listenerMap.values()) item.stop();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user