-
Notifications
You must be signed in to change notification settings - Fork 427
/
bySpecies.js
executable file
·37 lines (29 loc) · 1.04 KB
/
bySpecies.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
import executeInSeries from '../utils/executeInSeries';
import wait from '../utils/wait';
export default (userConfig) => () => {
const defaultConfig = {
delay: 10, // delay in milliseconds between each wave
nb: 100, // number of waves to execute (can be overridden in params)
};
const config = { ...defaultConfig, ...userConfig };
let stopped = false;
const bySpeciesStrategy = async (newGremlins) => {
const { nb, delay } = config;
const gremlins = [...newGremlins]; // clone the array to avoid modifying the original
for (let gremlinIndex in gremlins) {
const gremlin = gremlins[gremlinIndex];
for (let i = 0; i < nb; i++) {
await wait(delay);
if (stopped) {
return Promise.resolve();
}
await executeInSeries([gremlin], []);
}
}
return Promise.resolve();
};
bySpeciesStrategy.stop = () => {
stopped = true;
};
return bySpeciesStrategy;
};