-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.js
53 lines (43 loc) · 1.13 KB
/
parallel.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
"use strict";
let BaseNode = require('./basenode');
let Status = require('./status');
let Parallel = function() {
this.status = new Array(this.children.length);
this.initialize(arguments);
}
Parallel.prototype = new BaseNode();
Parallel.prototype.tick = function(tick) {
for (let i = 0; i < this.children.length; i++) {
if (this.status[i] !== Status.SUCCESS && this.status[i] !== Status.FAILURE) {
this.status[i] = this.children[i]._execute(tick);
}
}
if (this._nSucc() >= this.children.length) {
this.status = new Array(this.children.length);
return Status.SUCCESS;
} else if (this._nFail() >= this.children.length) {
this.status = new Array(this.children.length);
return Status.FAILURE;
} else {
return Status.RUNNING;
}
}
Parallel.prototype._nSucc = function() {
let nSucc = 0;
for (let i = 0; i < this.status.length; i++) {
if (this.status[i] === Status.SUCCESS) {
nSucc++;
}
}
return nSucc;
}
Parallel.prototype._nFail = function() {
let nFail = 0;
for (let i = 0; i < this.status.length; i++) {
if (this.status[i] === Status.FAILURE) {
nFail++;
}
}
return nFail;
}
module.exports = Parallel;