From 04f80c58339c1fafce822fac1bd2ee9a62bb8a05 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sun, 8 Jan 2023 06:36:12 +0300 Subject: [PATCH] [NNTPClient] Refactor getNewsGroupList method --- src/nntp.ts | 68 ++++++++++++++++++++++++++++----------------------- tsconfig.json | 5 ++-- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/nntp.ts b/src/nntp.ts index 0372a10..2c6a208 100644 --- a/src/nntp.ts +++ b/src/nntp.ts @@ -15,7 +15,7 @@ export class NNTPClient { constructor(url: string) { this.ws = new WebsocketBuilder(url) - .onMessage((ins, evt) => { + .onMessage((_, evt) => { if ((evt.data as string).startsWith("201")) { console.debug("skipping welcome message"); } @@ -52,7 +52,8 @@ export class NNTPClient { private async sendCommand( command: string, - args: string[] + args: string[], + prepareResponse = false // remove unnecessary things in lines array ): Promise { const cmd = { request: { command: command, args: args } as CommandRequest, @@ -67,43 +68,50 @@ export class NNTPClient { } const result = await cmd.response.promise; + if (prepareResponse) { + result.lines.shift(); + result.lines.pop(); + } return result; } - private async getNewsGroupList(): Promise { - const l: GroupInfo[] = []; + private completeGroupInfo(obj: Partial): GroupInfo { + return Object.assign( + { + name: "", + description: "", + lowWater: -1, + highWater: -1, + }, + obj + ); + } + + public async getNewsGroupList(): Promise { const groupMap: Record> = {}; + const [newsgroupsResponse, activeResponse]: CommandResponse[] = + await Promise.all([ + this.sendCommand("LIST", ["NEWSGROUPS"], true), + this.sendCommand("LIST", ["ACTIVE"], true), + ]); - await this.sendCommand("LIST", ["NEWSGROUPS"]).then((value) => { - value.lines.shift(); - value.lines.pop(); - value.lines.forEach((elem) => { - const firstSpace = elem.indexOf(" "); - const name = elem.substring(0, firstSpace); - groupMap[name] = { description: elem.substring(firstSpace + 1) }; - }); + newsgroupsResponse.lines.forEach((val: string) => { + const firstSpace = val.indexOf(" "); + const name = val.substring(0, firstSpace); + groupMap[name] = { description: val.substring(firstSpace + 1) }; }); - await this.sendCommand("LIST", ["ACTIVE"]).then((value) => { - value.lines.shift(); - value.lines.pop(); - value.lines.forEach((elem) => { - const splitted = elem.split(" "); - const [name, high, low] = splitted; - groupMap[name].highWater = Number(high); - groupMap[name].lowWater = Number(low); - }); + activeResponse.lines.forEach((val: string) => { + const splitted = val.split(" "); + const [name, high, low]: string[] = splitted; + groupMap[name].highWater = Number(high); + groupMap[name].lowWater = Number(low); }); - Object.keys(groupMap).forEach((key) => { - l.push({ - name: groupMap[key].name!, - description: groupMap[key].description!, - lowWater: groupMap[key].lowWater!, - highWater: groupMap[key].highWater!, - }); - }); + const result = Object.entries(groupMap).map(([, obj]) => + this.completeGroupInfo(obj) + ); - return l; + return result; } } diff --git a/tsconfig.json b/tsconfig.json index 61952ee..1ca4c6b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,12 +5,13 @@ "baseUrl": ".", "paths": { "@/*": ["./src/*"] - } + }, + "lib": ["es2021"] }, "references": [ { "path": "./tsconfig.vite-config.json" } - ] + ], }