-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_counter.js
37 lines (35 loc) · 1000 Bytes
/
worker_counter.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
36
37
let intervalID;
let total_rounds;
let computed_rounds;
let current_round;
function start_loop() {
if (current_round == total_rounds) {
process.send(JSON.stringify({command: "completed"}));
clearInterval(intervalID);
intervalID = null;
} else {
if (current_round < computed_rounds) {
process.send(JSON.stringify({command: "advance_round"}));
current_round++;
};
};
};
process.on("message", message => {
msg = JSON.parse(message);
switch (msg.command) {
case 'computed_rounds':
computed_rounds = msg.computed_rounds;
break;
case 'start':
current_round = msg.current_round;
total_rounds = msg.total_rounds;
if (!intervalID) {
intervalID = setInterval(start_loop, 1000);
};
break;
case 'stop':
clearInterval(intervalID);
intervalID = null;
break;
};
});