forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allTogether.js
71 lines (58 loc) · 1.91 KB
/
allTogether.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Execute all Gremlins species at once ; repeat 10ms after for 100 times
*
* var allTogetherStrategy = gremlins.strategies.allTogether();
* horde.strategy(allTogetherStrategy);
*
* The actual attack duration depends on the number of species in the horde.
*/
define(function(require) {
"use strict";
var executeInSeries = require('../utils/executeInSeries');
var configurable = require('../utils/configurable');
return function() {
/**
* @mixin
*/
var config = {
delay: 10, // delay in milliseconds between each wave
nb: 100 // number of waves to execute (can be overridden in params)
};
var stopped;
var doneCallback;
/**
* @mixes config
*/
function allTogetherStrategy(gremlins, params, done) {
var nb = params && params.nb ? params.nb : config.nb,
horde = this;
stopped = false;
doneCallback = done; // done can also be called by stop()
function executeAllGremlins(callback) {
executeInSeries(gremlins, [], horde, callback);
}
function executeNextWave(i) {
if (stopped) return;
if (i >= nb) return callDone();
executeAllGremlins(function() {
setTimeout(function() {
executeNextWave(++i);
}, config.delay);
});
}
executeNextWave(0);
}
function callDone() {
if (typeof doneCallback === 'function') {
doneCallback();
}
doneCallback = null;
}
allTogetherStrategy.stop = function() {
stopped = true;
setTimeout(callDone, 4);
};
configurable(allTogetherStrategy, config);
return allTogetherStrategy;
};
});