From 4f82c9141ff468b01f375fa31be577119494dc09 Mon Sep 17 00:00:00 2001 From: Wheat Carrier Date: Sun, 12 Nov 2023 09:00:59 +0800 Subject: [PATCH] shorten wait time for grouping requests --- src/api/client/message-api.ts | 37 +++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/api/client/message-api.ts b/src/api/client/message-api.ts index b940151..c192aab 100644 --- a/src/api/client/message-api.ts +++ b/src/api/client/message-api.ts @@ -23,13 +23,14 @@ class MessageBroker { protected buffer: Array<{ ids: number[]; resolve: (result: unknown) => void; + reject: (error: unknown) => void; }> = [], protected timeout: NodeJS.Timeout = null, ) {} async getMessagesByIds(ids: number[]) { return new Promise((resolve, reject) => { - this.buffer.push({ ids, resolve }); + this.buffer.push({ ids, resolve, reject }); if (this.timeout) { clearTimeout(this.timeout); } @@ -37,18 +38,28 @@ class MessageBroker { let buffer = []; [buffer, this.buffer] = [[...this.buffer], []]; const ids = [...new Set(buffer.map((item) => item.ids).flat())]; - const messages = await this.client.getMessages(this.privateChannelId, { - ids, - }); - const messageMap = new Map(); - messages.forEach((message) => { - messageMap.set(message.id, message); - }); - buffer.forEach((item) => { - const result = item.ids.map((id: number) => messageMap.get(id)); - item.resolve(result); - }); - }, 500); + + try { + const messages = await this.client.getMessages( + this.privateChannelId, + { + ids, + }, + ); + const messageMap = new Map(); + messages.forEach((message) => { + messageMap.set(message.id, message); + }); + buffer.forEach((item) => { + const result = item.ids.map((id: number) => messageMap.get(id)); + item.resolve(result); + }); + } catch (err) { + buffer.forEach((item) => { + item.reject(err); + }); + } + }, 100); }); } }