mirror of
https://github.com/TheodoreKrypton/tgfs
synced 2024-11-22 15:25:22 +00:00
shorten wait time for grouping requests
This commit is contained in:
parent
2a8fbb8b74
commit
4f82c9141f
@ -23,13 +23,14 @@ class MessageBroker {
|
|||||||
protected buffer: Array<{
|
protected buffer: Array<{
|
||||||
ids: number[];
|
ids: number[];
|
||||||
resolve: (result: unknown) => void;
|
resolve: (result: unknown) => void;
|
||||||
|
reject: (error: unknown) => void;
|
||||||
}> = [],
|
}> = [],
|
||||||
protected timeout: NodeJS.Timeout = null,
|
protected timeout: NodeJS.Timeout = null,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getMessagesByIds(ids: number[]) {
|
async getMessagesByIds(ids: number[]) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.buffer.push({ ids, resolve });
|
this.buffer.push({ ids, resolve, reject });
|
||||||
if (this.timeout) {
|
if (this.timeout) {
|
||||||
clearTimeout(this.timeout);
|
clearTimeout(this.timeout);
|
||||||
}
|
}
|
||||||
@ -37,9 +38,14 @@ class MessageBroker {
|
|||||||
let buffer = [];
|
let buffer = [];
|
||||||
[buffer, this.buffer] = [[...this.buffer], []];
|
[buffer, this.buffer] = [[...this.buffer], []];
|
||||||
const ids = [...new Set(buffer.map((item) => item.ids).flat())];
|
const ids = [...new Set(buffer.map((item) => item.ids).flat())];
|
||||||
const messages = await this.client.getMessages(this.privateChannelId, {
|
|
||||||
|
try {
|
||||||
|
const messages = await this.client.getMessages(
|
||||||
|
this.privateChannelId,
|
||||||
|
{
|
||||||
ids,
|
ids,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
const messageMap = new Map();
|
const messageMap = new Map();
|
||||||
messages.forEach((message) => {
|
messages.forEach((message) => {
|
||||||
messageMap.set(message.id, message);
|
messageMap.set(message.id, message);
|
||||||
@ -48,7 +54,12 @@ class MessageBroker {
|
|||||||
const result = item.ids.map((id: number) => messageMap.get(id));
|
const result = item.ids.map((id: number) => messageMap.get(id));
|
||||||
item.resolve(result);
|
item.resolve(result);
|
||||||
});
|
});
|
||||||
}, 500);
|
} catch (err) {
|
||||||
|
buffer.forEach((item) => {
|
||||||
|
item.reject(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user