-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkerGroup.js
52 lines (47 loc) · 1.15 KB
/
WorkerGroup.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
function WorkerGroup(fileName, n) {
var workers = this.workers = [];
while (n--) {
workers.push(new Worker(fileName));
}
}
WorkerGroup.prototype = {
map: function(callback) {
var workers = this.workers;
var configs = this.configs = [];
for (var i = 0, l = workers.length; i < l; i++) {
configs.push(callback(i));
}
},
reduce: function(opt) {
var fn = opt.reduceFn,
workers = this.workers,
configs = this.configs,
l = workers.length,
acum = opt.initialValue,
message = function (e) {
if (e.data.type === "data"){
l--;
if (acum === undefined) {
acum = e.data.data;
} else {
acum = fn(acum, e.data.data);
}
if (l == 0) {
opt.onComplete(acum);
}
}
else if (e.data.type==="log") {
msg_num +=1;
if (msg_num % 1 === 0) {
console.log(e.data.data);
}
}
};
for (var i = 0, ln = l; i < ln; i++) {
var w = workers[i];
w.onmessage = message;
w.postMessage(configs[i]);
}
}
};
var msg_num = 0;