37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
|
import { splitEvery } from 'ramda';
|
||
|
import { parentPort } from 'worker_threads';
|
||
|
import { Client } from './client.js';
|
||
|
|
||
|
const clients = [];
|
||
|
|
||
|
parentPort.on('message', message => {
|
||
|
switch(message.type) {
|
||
|
case 'testOnce':
|
||
|
clients.forEach(worker => {
|
||
|
if (worker.index < message.limit) {
|
||
|
worker.testOnce();
|
||
|
}
|
||
|
});
|
||
|
break;
|
||
|
case 'generateWorkers':
|
||
|
clients.splice(
|
||
|
0,
|
||
|
clients.length,
|
||
|
...new Array(message.end - message.start).fill(undefined).map((_, i) => new Client(i + message.start)),
|
||
|
);
|
||
|
parentPort.postMessage({ type: 'workersGenerated' });
|
||
|
break;
|
||
|
case 'joinRoom':
|
||
|
(async () => {
|
||
|
for (const group of splitEvery(4)(clients)) {
|
||
|
await Promise.all(group.map(async client => {
|
||
|
await client.joinRoom();
|
||
|
parentPort.postMessage({ type: 'oneWorkerJoined', index: client.index });
|
||
|
}));
|
||
|
}
|
||
|
parentPort.postMessage({ type: 'workersJoined' });
|
||
|
})().catch(err => {
|
||
|
parentPort.postMessage({ type: 'joinRoomFailed', error: err });
|
||
|
})
|
||
|
}
|
||
|
});
|