1 | import { FUN } from '@typen/enum-data-types';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | async function promiseLobby(freeAgentPool, clients) {
|
12 |
|
13 | const service = typeof this === FUN ? this : null;
|
14 | const operationPool = [],
|
15 | postOperationPool = new Set();
|
16 |
|
17 | for (const client of clients) {
|
18 | if (!freeAgentPool.length) await Promise.race(postOperationPool);
|
19 | const busyAgent = freeAgentPool.shift();
|
20 | const operation = Promise.resolve().then(service ? service.bind(busyAgent, client) : async () => await busyAgent(client));
|
21 | operationPool.push(operation);
|
22 | const postOperation = operation.then(() => {
|
23 | postOperationPool.delete(postOperation);
|
24 | freeAgentPool.push(busyAgent);
|
25 | return busyAgent;
|
26 | });
|
27 | postOperationPool.add(postOperation);
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | }
|
34 |
|
35 | return Promise.all(operationPool);
|
36 | }
|
37 |
|
38 | export { promiseLobby };
|