-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared-queue.js
35 lines (32 loc) · 1.08 KB
/
shared-queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const MAX_CONCURRENT_CALLS = 10;
let currentCalls = 0;
export const queue = [];
function updateQueuePositions() {
// Iterate over the queue and update each user's position
console.log("Updating queue positions");
for (let i = 0; i < queue.length; i++) {
const { message, interaction } = queue[i];
console.log(`Updating position for ${interaction.user.id} at ${i}`);
message.edit(
`<@${interaction.user.id}>, your request is now #${i + 1} in the queue.`
);
}
}
async function processTask(task, user, message, interaction) {
console.log("Processing task for user", user);
// Simulate an async task with a delay of 2 seconds
const results = await task(user, message);
currentCalls--;
processQueue();
updateQueuePositions();
return results;
}
export function processQueue() {
console.log("Processing queue");
console.log("Current calls", currentCalls);
while (currentCalls < MAX_CONCURRENT_CALLS && queue.length > 0) {
const { task, user, message, interaction } = queue.shift();
currentCalls++;
processTask(task, user, message, interaction);
}
}