-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
141 lines (124 loc) · 2.91 KB
/
worker.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Define las funciones necesarias para que funcione
* el worker. Ademas contiene el la funcion del map
* del investigador.
*/
work = this;
sleep = false;
intervalId = null;
result = [ [] ]; //[["0",1],["1",2],["2",3]]
log = function (msg) {
work.postMessage({
type: "log",
args: msg
});
};
/*
investigador_map = function (k, v) {
log("inv in");
var ms = 1000;
var started = new Date().getTime();
while((new Date().getTime() - started) < ms) {
}
emit("llave", v*v);
log("inv in out");
};
*/
/*
investigador_reduce = function (k, vals) {
var total = vals.reduce(function(a, b) {
return parseInt(a) + parseInt(b);
});
return total;
};
*/
emit = function (key, val) {
/* funcion utilizada en investigador_map para agregar valores */
var arr = result[result.length - 1];
arr.push([key, val]);
};
send_result = function () {
/* Envia un mensaje <send_result>
*/
sleep = true;
cola.i = 0;
log("send_result worker");
log(result);
postMessage({
type: "send_result",
args: result
});
result = [ [] ];
};
function Cola () {
/* <Humilde intento de clase en JS :'(>
* Recibe todos los datos que necesita la funcion map.
* Luego los ejecuta en orden realizando llamadas a la
* función map del investigador. Luego de cada llamada
* se duerme un pequeño lapso de tiempo para chequear
* si recivio algun mje "sleep". De ser asi no empieza mas
* tareas.
*/
this.map = null;
this.i = 0;
this.args = [];
this.executing = false;
this._process = function() {
this.executing = true;
if(this.i < this.args.length) {
log("ejecutando map con " + this.args[this.i][0] + " y " + this.args[this.i][1]);
this.map(this.args[this.i][0],this.args[this.i][1]);
if(result[result.length] != 0) {
result.push([]);
}
this.i ++;
} else{
this.executing = false;
throw new Error("<b>Nothing to process!</b>");
}
this.executing = false;
};
this.process = function() {
if(!this.executing && !sleep) {
this._process();
}
};
}
cola = new Cola();
cola.map = investigador_map;
this.onmessage = function(evnt) {
/* Manejador de mensajes
*/
msg = evnt.data;
if(msg.type == "start") {
sleep = false;
log("start recv");
cola.args = msg.args; // array de arrays [["0", 1], ["1", 2], ["2", 3]]
intervalId = setInterval(function(){
try {
cola.process();
}
catch (err) {
log(err.message);
send_result();
}},50);
}
else if(msg.type == "pause") {
sleep = true;
clearInterval(intervalId);
log("pause recv");
setTimeout(function(){
sleep = false;
log("resume recv");
intervalId = setInterval(function(){
try {
cola.process();
}
catch (err) {
log(err.message);
send_result();
}},250);
},msg.sleep_time);
}
};
work.postMessage("termino worker");